流式传输时,MPMoviePlayer会自动暂停

时间:2016-01-22 13:04:12

标签: ios objective-c video-streaming mpmovieplayercontroller mpmovieplayer

我正在使用MPMoviePlayerViewController播放来自服务器的视频流,其他一切工作顺利。我的问题是,当我播放视频时,它会自动播放,如果没有可播放的内容,则自动进入暂停状态,并且在加载内容后不播放。

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];

这是我用来播放mediaURL给出的网址视频的代码。我试图添加MPMoviePlayerPlaybackStateDidChangeNotification来检查状态已更改为MPMoviePlaybackStatePaused,它也正常工作。同时使用MPMovieLoadState使用MPMovieLoadStatePlaythroughOK检查MPMoviePlayerLoadStateDidChangeNotification,问题是自动暂停后无法获得MPMovieLoadStatePlaythroughOKThis link

除了MPMoviePlayerPlaybackStateDidChangeNotification之外,是否有任何方法可以获得暂停按钮,因为其他原因也会触发通知。

1 个答案:

答案 0 :(得分:0)

我在MPMoviePlayerViewController的默认播放/暂停按钮上方添加了一个透明按钮,如下所示。

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];        
}
}