Search code examples
iosobjective-cvideo-streamingmpmovieplayercontrollermpmovieplayer

MPMoviePlayer automatically pauses when streaming


I am using MPMoviePlayerViewController to play video stream from the server, everything else working smooth. My problem is that when I play the Video it plays automatically and goes to the pause state automatically if no playable content available, and not playing after getting the content loaded.

movieController = [[MPMoviePlayerViewController alloc] init];
movieController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[movieController.moviePlayer setContentURL:mediaURL];
[movieController.moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:nil];
[movieController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[movieController.moviePlayer setFullscreen:YES];
[movieController.moviePlayer setShouldAutoplay:YES];
[movieController.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated:movieController];

This is my code used to play the video from the url given with mediaURL. I have tried to add MPMoviePlayerPlaybackStateDidChangeNotification to check state changed to MPMoviePlaybackStatePaused, its also working fine. Also checked MPMovieLoadState with MPMovieLoadStatePlaythroughOK using MPMoviePlayerLoadStateDidChangeNotification the issue is that not get the MPMovieLoadStatePlaythroughOK after pause automatically.This link

Also Is there any way to get pause button tap other than MPMoviePlayerPlaybackStateDidChangeNotification, because notification get fired for other reasons also.


Solution

  • I've added a transparent button above the default Play/Pause button in MPMoviePlayerViewController as follows.

    CGRect frame = CGRectZero;
    CGFloat width = 100, height= 50;
    frame.origin.x = CGRectGetHeight(movieController.moviePlayer.view.frame)/2 - (width/2);
    frame.origin.y = CGRectGetWidth(movieController.moviePlayer.view.frame) - height;
    frame.size = CGSizeMake(width, height);
    UIButton* playButton = [[UIButton alloc] initWithFrame:frame];
    playButton.backgroundColor = [UIColor clearColor];
    [playButton addTarget:self action:@selector(playButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [movieController.moviePlayer.view addSubview:playButton];
    
    
    - (void) playButtonTapped:(UIButton*) sender {
    if (APP_DELEGATE.movieController.moviePlayer.playbackState == 1) {
        [APP_DELEGATE.movieController.moviePlayer pause];        
    }
    else if (APP_DELEGATE.movieController.moviePlayer.playbackState == 2)
    {
        [APP_DELEGATE.movieController.moviePlayer play];        
    }
    }