MPMoviePlayerController stops iPod playback and does not restart

I have an iPhone app with a short intro video. If the user launches the application while his iPod is playing music, the music will stop during video playback (regardless of whether there is sound on it) and the sound will remain permanently stopped after playing the video. Apple seems to indicate that you can solve this problem with AudioSession tricks: https://web.archive.org/web/20100819124854/http://developer.apple.com:80/iPhone/library/documentation/Audio /Conceptual/AudioSessionProgrammingGuide/WorkingWithOpenALiPodMusicandMovies/WorkingWithOpenALiPodMusicandMovies.html

But their suggestions here simply do not work; it looks like MPMoviePlayerController is redefining the audio session configuration for its own purposes. Ideally, I would mix the sound from the movie with the iPod audio or maybe use the mute, but even restarting the music could be an acceptable solution.

+5
source share
3 answers

I found a great solution for this. In the .h file, you must create a BOOL named wasPlaying. Before playing a video, you ask your iPod if it is playing.

if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying)
{
    NSLog(@"Music was playing, lets put YES to the bool");
    wasPlaying = YES;
}

Then, after you tell the movie player, you call the following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishedPlaying) name:MPMoviePlayerPlaybackDidFinishNotification object: moviePlayer];

And after that, in the finishedPlaying method:

if (wasPlaying ==YES)
{
    NSLog(@"Music was playing, lets play music again");
    [[MPMusicPlayerController iPodMusicPlayer] play];
}

It worked great for me!

+2
source

, , , :

 NSError *audioSessionError = nil; 
 AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
 [audioSession setCategory:AVAudioSessionCategoryAmbient 
                          error:&audioSessionError] == YES)

, -, iPod :

AudioSessionInitialize (NULL, NULL, NULL, NULL);
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError |= AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(allowMixing), &allowMixing);
AudioSessionSetActive(YES);
+2

() . , . , .

, , , MPMoviePlayerController ( -). , , , MPMoviePlayerController, , .

+1

All Articles