MPMoviePlayerController: Didn't get sound when we stop MPMoviePlayerController

70 views Asked by At

I am using MPMoviePlayerController to play a video into my application. Below is my code for playing video.

    **NSURL *url=[[NSURL alloc] initFileURLWithPath:urlPath];        
    if(moviePlayer)
    {
        [moviePlayer.view removeFromSuperview];
        moviePlayer = nil;
    }

    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    moviePlayer.repeatMode = MPMovieRepeatModeOne;
    moviePlayer.currentPlaybackTime = 0.0;
    moviePlayer.view.frame = _viewMoviePlayer.frame;
    moviePlayer.controlStyle    =       MPMovieControlStyleNone;
    moviePlayer.shouldAutoplay = YES;
    [_viewMoviePlayer addSubview:moviePlayer.view];
    [moviePlayer prepareToPlay];
    [moviePlayer play];
    [self.view sendSubviewToBack:_viewMoviePlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];**

When viewWillDisappear called, then I just stop moviePlayer and remove it from view. But my default music player is running but didn't sound for it. When I closed my application, it will.

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [moviePlayer stop];
    [moviePlayer.view removeFromSuperview];
    moviePlayer = nil;

    [_viewMoviePlayer removeFromSuperview];
    _viewMoviePlayer = nil;
}

Can anyone tell which is going wrong in my code? So I am release the moviePlayer then it will play default music as it should. when I check current sound for Music then its ohk and also its show song is running in Music app. But didn't get sound.

1

There are 1 answers

0
Nidhi Patel On

I faced with same issue. I just created one separate controller. Below is my code for same:

-(void)playVideo
{
   isPlay = FALSE;
   if([[MPMusicPlayerController systemMusicPlayer] playbackState] == MPMoviePlaybackStatePlaying)
       isPlay = TRUE;

   NSString *urlPath = [[NSBundle mainBundle]
                        pathForResource:@"all"
                        ofType:@"mp4"];


   NSURL *url=[[NSURL alloc] initFileURLWithPath:urlPath];

   if(player)
       [player pause];

   player = nil;

   if(avPlayerLayer)
       [avPlayerLayer removeFromSuperlayer];

   AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
   player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

   avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

   avPlayerLayer.frame = self.view.frame;
   [self.view.layer addSublayer:avPlayerLayer];
   [player play];
  }

-(void)removeMoviePlayer
{
   if(isPlay)
       [[MPMusicPlayerController systemMusicPlayer] play];
   NSError *deactivationError = nil;
   BOOL success = [[AVAudioSession sharedInstance] setActive:NO error:&deactivationError];
   if (!success) {
       NSLog(@"%@", [deactivationError localizedDescription]);
   }    
}

Hope it will helpful to you... :)