点击完成后MPMoviePlayerController不会关闭视图?

时间:2013-12-04 02:40:19

标签: notifications mpmovieplayercontroller

我使用MPMoviePlayerController播放来自互联网的视频。

player = [player initWithContentURL:[NSURL URLWithString:videoURL]];
player.view.frame = CGRectMake(0, 0, videoView.frame.size.width,  videoView.frame.size.height - 20);
[player setControlStyle:MPMovieControlStyleEmbedded];
player.scalingMode = MPMovieScalingModeAspectFit;
[player prepareToPlay];
player.shouldAutoplay = NO;
[videoView addSubview:player.view];

我在点击全屏按钮(2箭头按钮)后通知我,我被导航到全尺寸视频屏幕。我无法通过触摸完成按钮来恢复屏幕。我甚至使用NSNotification但无法解决问题。这是通知代码:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieEventFullscreenHandler:)
                                                 name:MPMoviePlayerWillEnterFullscreenNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieEventFullscreenHandler:)
                                                 name:MPMoviePlayerDidEnterFullscreenNotification
                                               object:nil];
}

- (void)movieEventFullscreenHandler:(NSNotification*)notification {
    [player setFullscreen:NO animated:NO];
    [player setControlStyle:MPMovieControlStyleEmbedded];
}

如何通过触摸完成按钮来关闭该视频屏幕?谢谢你们。

2 个答案:

答案 0 :(得分:2)

您可以使用MPMoviePlayerPlaybackDidFinishNotification的通知来观察完成按钮。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playObserver:)name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

然后,做出戒烟的判断。

- (void) playObserver:(NSNotification *)notification
{
MPMoviePlayerController* player = moviePlayerView.moviePlayer;
if (player == [notification object]) {

    if (_invalidVideoCount > MOVIE_TRY_TIMES) {
        [self dismissViewController];
    }

    _invalidVideoCount++;
    int reason = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];

    //Whether continuous playback
    if (![SINGLETON_CALL(SystemInfoManager) boolValueForKey:UserContinuousPlayEnableKey]) {
        [self playFinishWithForce:YES];
        return;
    }

    switch (reason) {
        case MPMovieFinishReasonUserExited:
            [self playFinishWithForce:YES];
            break;
        case MPMovieFinishReasonPlaybackError:
            [self playFinishWithForce:YES];
            break;
        case MPMovieFinishReasonPlaybackEnded:
            movieTryTimes = 0;
            [self playFinishWithForce:NO];
            break;
        default:
            break;
    }
}

}

和最后一次。

- (void)playFinishWithForce:(BOOL)force
{
  FileInfoItem *item = ARRAY_OBJECT_AT_INDEX(_playlist, _currentIndex);
  BOOL quit = force || !item;
  if (quit) {
      [self dismissViewController];
  } else {
      [self playMovieWithItem:item];
  }        
}

您也可以使用MPMoviePlayerPlaybackStateDidChangeNotification的通知来制作任何observe.something详细信息,请参阅MPMoviePlayerController.h或https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/Reference/Reference.html

答案 1 :(得分:0)

我发现了这个问题。那是我在viewWillDisAppear中放置[播放器停止]所以无法处理通知。我通过将其更改为[播放器暂停]来修复。我很感激你的任何帮助。