MPMoviePlayer 流式传输时自动暂停
MPMoviePlayer automatically pauses when streaming
我正在使用 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
给出的 url 中的视频。我尝试添加 MPMoviePlayerPlaybackStateDidChangeNotification
以检查状态更改为 MPMoviePlaybackStatePaused
,它也工作正常。还检查了 MPMovieLoadState
和 MPMovieLoadStatePlaythroughOK
使用 MPMoviePlayerLoadStateDidChangeNotification
问题是在暂停后不会自动获得 MPMovieLoadStatePlaythroughOK
。This link
此外,除了 MPMoviePlayerPlaybackStateDidChangeNotification
之外,还有什么方法可以让暂停按钮点击,因为通知也会因其他原因而被触发。
我在 MPMoviePlayerViewController
中的默认 Play/Pause 按钮上方添加了一个透明按钮,如下所示。
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];
}
}
我正在使用 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
给出的 url 中的视频。我尝试添加 MPMoviePlayerPlaybackStateDidChangeNotification
以检查状态更改为 MPMoviePlaybackStatePaused
,它也工作正常。还检查了 MPMovieLoadState
和 MPMovieLoadStatePlaythroughOK
使用 MPMoviePlayerLoadStateDidChangeNotification
问题是在暂停后不会自动获得 MPMovieLoadStatePlaythroughOK
。This link
此外,除了 MPMoviePlayerPlaybackStateDidChangeNotification
之外,还有什么方法可以让暂停按钮点击,因为通知也会因其他原因而被触发。
我在 MPMoviePlayerViewController
中的默认 Play/Pause 按钮上方添加了一个透明按钮,如下所示。
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];
}
}