0

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

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.

电影的结尾没有到达。我只是叫暂停:。所以通知被触发,因此电影再次播放,否定了暂停动作。

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

4

1 回答 1

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 的值,否则循环将不起作用。

于 2013-10-29T19:24:28.720 回答