MPMoviePlayerController is not running in the background

I have looked at the MPMoviePlayerController multitasking issue and MPMovieplayerController is not playing the following url in the background , plus many other similar questions. None of these solutions worked for me.

I, like many others, are trying to play a list of deleted files in the background. In this case, I am playing YouTube video files, parse and play with XCDYouTubeKit . The set XCDYouTubeViewController

processes the YouTube video ID (the sequence of characters after the "watch =" in each YouTube URL) and the change contentURL

. I have an array of videos in a singlelet PlayQueue

and it plays each one sequentially correctly when the app is in the foreground. But when it's in the background, the next song is loaded, but it pauses immediately. It plays fine if the user clicks play in the Control Center or Lock screen, but I was hoping for a smooth transition between songs.

I have this method that plays a video (self.moviePlayer is this XCDYouTubeViewController

, and self.moviePlayer.moviePlayer is MPMoviePlayerController

):

- (void)playVideoWithIdenfifier:(NSString*)identifier
{   
    if ([self checkReachability]) // this simply checks if there an internet connection
    {
        if (!self.moviePlayer)
        {
            self.moviePlayer = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:identifier];
            self.moviePlayer.moviePlayer.view.userInteractionEnabled = NO;
            self.moviePlayer.moviePlayer.backgroundPlaybackEnabled = YES;
            [self.moviePlayer presentInView:self.playerContainer];
            [self.moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleNone];
        }
        else self.moviePlayer.videoIdentifier = identifier

        NSError *activationError = nil;
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:&activationError];
        [audioSession setActive:YES error:&activationError];

        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
        [self becomeFirstResponder];

        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(sliderUpdate) userInfo:nil repeats:YES];

        self.moviePlayer.moviePlayer.shouldAutoplay = YES;
        [self.moviePlayer.moviePlayer play];

        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:self.imageView.image];

        [songInfo setObject:self.song.title forKey:MPMediaItemPropertyTitle];
        [songInfo setObject:[NSString stringWithFormat:@"%@ - Playing from umusio",self.song.album] forKey:MPMediaItemPropertyAlbumTitle];
        [songInfo setObject:self.song.artist forKey:MPMediaItemPropertyArtist];
        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
        [songInfo setObject:[NSNumber numberWithFloat:self.moviePlayer.moviePlayer.currentPlaybackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
        [songInfo setObject:[NSNumber numberWithFloat:self.moviePlayer.moviePlayer.duration] forKey:MPMediaItemPropertyPlaybackDuration];
        [songInfo setObject:[NSNumber numberWithInt:1] forKey:MPNowPlayingInfoPropertyPlaybackRate];
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

        [self.playButton setImage:[UIImage imageNamed:@"BeamMusicPlayerController.bundle/images/pause.png"] forState:UIControlStateNormal];
        [self.playButton setImage:[UIImage imageNamed:@"BeamMusicPlayerController.bundle/images/pause.png"] forState:UIControlStateSelected];
    }
    else [[[UIAlertView alloc] initWithTitle:@"error" message:@"reachability check failed" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show];
}

      

When the video ends, it calls this method, which changes the property of the current song, and calls checkSongID, which binds to Parse and maps iTunesSong to the YouTube video ID. From there, checkSongID calls the previous method playVideoWithIdentifier:

and passes the video ID it got from Parse. I won't include it here checkSongID

, this is not a problem as it works great in the foreground and doesn't interact with moviePlayer at all.

- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
    iTunesSong* nextSong = [[PlayQueue sharedPlayQueue] nextSong]; // iTunesSong is a custom class that I made.

    self.song = nextSong;
    [self checkSongID];

}

      

This all works great in the foreground, any ideas why it stops immediately in the background?

+3


source to share





All Articles