0

尝试制作电影循环,但 MPMoviePlayerPlaybackDidFinishNotification 在视频结束时永远不会调用。

这是我的代码:

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"dent4" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.moviePlayerController];

    [self.moviePlayerController.view setFrame:CGRectMake(playButton.frame.origin.x,
                                                    playButton.frame.origin.y,
                                                    playButton.frame.size.width,
                                                    playButton.frame.size.height)];

    self.moviePlayerController.view.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    self.moviePlayerController.shouldAutoplay = YES;
    self.moviePlayerController.initialPlaybackTime = 0;
    [self.moviePlayerController setFullscreen:NO];
    self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
    self.moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
    self.moviePlayerController.repeatMode = MPMovieRepeatModeOne;
    [self.view addSubview:self.moviePlayerController.view];
    [self.moviePlayerController prepareToPlay];
    [self.moviePlayerController play];

视频正确播放一次,但不触发结束事件。

- (void)moviePlaybackComplete:(NSNotification *)note
{
    NSLog(@"END");
    if (note.object == self.moviePlayerController) {
        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded) {
            NSLog(@"REPLAY");
            [self.moviePlayerController prepareToPlay];
            [self.moviePlayerController play];

        }
    }

}

另一件事奇怪的视频在模拟器上正确循环(但 moviePlaybackComplete 从不调用)但在设备上只播放一次。

任何人都可以帮助我吗?我已经看到一些帖子在全屏上说它没有被调用,但我不在全屏模式下。

4

3 回答 3

4

尝试

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController.moviePlayer];
于 2012-12-07T20:54:06.127 回答
0

您必须将其设置为moviePlayer 而不是moviePlayerController。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController.moviePlayer];
于 2013-03-18T22:26:56.290 回答
0

试试这个

MPMoviePlayerViewController *mp;

1)在viewDidLoad中添加观察者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.mp.moviePlayer];

2)movieFinished回调方法

-(void)movieFinishedCallback:(NSNotification *) notification
{
    NSNumber *reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason integerValue])
    {
        case MPMovieFinishReasonPlaybackEnded:

            NSLog(@"Movieplayer is stopped");
            break;

        case MPMovieFinishReasonPlaybackError:

            NSLog(@"An error was encountered during playback");
            break;

        case MPMovieFinishReasonUserExited:

            NSLog(@"userExtied");
            break;

        default:
            break;
    }
}

3) 删除 ViewWillDisappear 中的观察者

 [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.mp.moviePlayer];
于 2014-11-12T05:55:19.357 回答