由于MPMoviePlayerController中的错误,无法暂停循环电影

时间:2013-10-29 19:04:37

标签: ios mpmovieplayercontroller nsnotifications

我设置了这样的电影播放器​​:

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:@"whatever.mp4"];
    self.moviePlayer.controlStyle = MPMovieControlStyleNone;
    self.moviePlayer.shouldAutoplay = YES;
    [self.moviePlayer prepareToPlay];
    self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
    self.moviePlayer.view.frame = self.container.bounds;
    self.moviePlayer.view.userInteractionEnabled = NO;
    [self.container addSubview:self.moviePlayer.view];

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(moviePlayBackDidFinish:) name: MPMoviePlayerPlaybackStateDidChangeNotification
                                               object: self.moviePlayer];

通知是必要的,以保持播放器循环,因为repeatMode几乎没用(视频将重复一次或两次,可能几次取决于中国大米的价格,然后停止)。因此,要保持视频循环,您必须执行此操作:

- (void)moviePlayBackDidFinish:(NSNotification *)note {
    if (note.object == self.moviePlayer) {
        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded) {
            [self.moviePlayer play];
        }
    }
}

现在的问题是我需要能够暂停视频。出于某种原因,打电话给

    [self.moviePlayer pause];

导致通知因为== MPMovieFinishReasonPlaybackEnded被触发,即使文档明确指出:

Constants
MPMovieFinishReasonPlaybackEnded
The end of the movie was reached.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.

电影的结尾未到达。我刚叫暂停:因此通知被触发,因此电影再次播放,否定暂停动作。

所以你可以看到问题所在。如何成功暂停循环视频?

1 个答案:

答案 0 :(得分:0)

在你的班级中创建一个BOOL并将其初始化为NO,

@property (nonatomic, assign) BOOL isTriggeredByPause;

之前打电话

[self.moviePlayer pause];

设置其值,

self.isTriggeredByPause = YES;

在你的方法中检查它,

 (void)moviePlayBackDidFinish:(NSNotification *)note {
if (note.object == self.moviePlayer) {
    NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
    if (reason == MPMovieFinishReasonPlaybackEnded) {
if(!self.isTriggeredByPause)
{
    [self.moviePlayer play];
}
}
}
}

在手动播放时修改self.isTriggeredByPause的值,否则循环将无效。