Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://devforums.apple.com/message/264397" rel="noreferrer">Relevant discussion</a></p> <p>SHORT ANSWER:</p> <p>You need this code in either your first view controller's init or viewDidLoad method:</p> <pre><code>[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; </code></pre> <hr> <p>LONG ANSWER W/ SAMPLE:</p> <p>Here is my example. Like you, I began with an app that would play music in the background but could never continue playing after the first clip ended. I made a copy of the original Music.mp3 and named it Music2.mp3. My intention was to play Music2.mp3 as soon as Music.mp3 ended (audioPlayerDidFinishPlaying:). I goofed around with the background tasks for awhile until I got this working WITHOUT the background task:</p> <pre><code>-(id)init{ self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil]; if(self){ //Need this to play background playlist [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; //MUSIC CLIP //Sets up the first song... NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Music" ofType:@"mp3"]; if(musicPath){ NSURL *musicURL = [NSURL fileURLWithPath:musicPath]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil]; [audioPlayer setDelegate:self]; } } return self; } -(IBAction)playAudioFile:(id)sender{ if([audioPlayer isPlaying]){ //Stop playing audio and change test of button [audioPlayer stop]; [sender setTitle:@"Play Audio File" forState:UIControlStateNormal]; } else{ //Start playing audio and change text of button so //user can tap to stop playback [audioPlayer play]; [sender setTitle:@"Stop Audio File" forState:UIControlStateNormal]; } } -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ [audioButton setTitle:@"Play Audio File" forState:UIControlStateNormal]; [playRecordingButton setTitle:@"Play Rec File" forState:UIControlStateNormal]; //PLAY THE SECOND SONG NSString *musicPath2 = [[NSBundle mainBundle] pathForResource:@"Music2" ofType:@"mp3"]; if(musicPath2){ NSURL *musicURL2 = [NSURL fileURLWithPath:musicPath2]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL2 error:nil]; [audioPlayer setDelegate:self]; NSLog(@"Play it again: \n%@", musicPath2); [audioPlayer play]; } } </code></pre> <p>The end result is that my app is now playing Music2.mp3 on a continuous loop, even if the app is in the background.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload