1

我有一部短片可以在我的视图背景中循环播放。我使用 MPMoviePlayerController 播放电影。repeatMode 设置为 MPMovieRepeatModeOne,这在 iPad 2、3 和模拟器上运行良好。然而,在 iPad 1 上,影片循环播放一次并在第二次播放后立即停止。该项目是 iOS 5 w/o ARC(从 GM 测试到 5.1.1)。

- (void)loadVideo {
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"movieFileName.m4v" ofType:nil];
    self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:urlStr]];
    self.videoPlayer.controlStyle = MPMovieControlStyleNone;
    self.videoPlayer.scalingMode = MPMovieScalingModeFill;
    self.videoPlayer.repeatMode = MPMovieRepeatModeOne;
    self.videoPlayer.view.userInteractionEnabled = NO;
    [self.videoPlayer.view setFrame:self.movieContainer.bounds];
    [self.movieContainer addSubview:self.videoPlayer.view];
}

如何让电影在 iPad 1 上循环播放?

4

1 回答 1

1

尝试了很多之后,我终于找到了解决这个问题的方法:

在注册更改播放状态 MPMoviePlayerPlaybackStateDidChangeNotification 的通知后,影片会无限循环,并且在 iPad 1 上第二次播放后不会停止。请记住,此行为不会发生在 iPad 2、3 或模拟器上。

为通知执行的选择器不能为空。只需分配一个布尔值或其他东西。上面的扩展代码将是:

- (void)loadVideo {
    // Create the controller
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"movieFileName.m4v" ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

    // Configure the controller
    self.videoPlayer.controlStyle = MPMovieControlStyleNone;
    self.videoPlayer.scalingMode = MPMovieScalingModeFill;
    self.videoPlayer.repeatMode = MPMovieRepeatModeOne;
    self.videoPlayer.view.userInteractionEnabled = NO;
    [self.videoPlayer.view setFrame:self.movieContainer.bounds];

    // Register for notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerNotification:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:videoPlayer];
    self.listeningToMoviePlayerNotifications = YES;

    // Add its view to the hierarchy
    [self.movieContainer addSubview:self.videoPlayer.view];
}

- (void)moviePlayerNotification:(NSDictionary *)userInfo {
    // Do anything here, for example re-assign the listeningToMoviePlayerNotification-BOOL
    self.listeningToMoviePlayerNotifications = YES;
}
于 2012-08-01T17:36:50.643 回答