5

注意:请参阅底部的更新。


我有一个应用程序可以从列表中逐个播放视频。所以,为了测试这个功能,我创建了一个只有一个视图控制器的简单应用程序。在实现这个视图控制器之前,我参考了这个博客。视图控制器被命名TNViewController,其实现如下:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface TNViewController : UIViewController {
  @private
    NSMutableArray *_videoArray;
    int _currentVideo;

    MPMoviePlayerController *_moviePlayer;
    NSURL *_movieUrl;
}

@end

它的实现是:

#import "TNViewController.h"

@implementation TNViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
    [self.view setFrame:CGRectMake(0, 0, 480, 320)];

    [self initVideos];
    [self initPlayer];
}

- (void) initVideos {
    _videoArray = [[NSMutableArray alloc] init];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sintel_trailer" ofType:@"mp4" inDirectory:nil];
    [_videoArray addObject:path];
    path = [[NSBundle mainBundle] pathForResource:@"elephants_dream_trailer" ofType:@"mp4" inDirectory:nil];
    [_videoArray addObject:path];
    path = [[NSBundle mainBundle] pathForResource:@"big_buck_bunny_trailer" ofType:@"mp4" inDirectory:nil];
    [_videoArray addObject:path];

    _currentVideo = -1;
}

- (NSString*) nextVideo {
    _currentVideo++;
    if (_currentVideo >= _videoArray.count) {
        _currentVideo = 0;
    }
    return [_videoArray objectAtIndex:_currentVideo];
}

- (void) initPlayer {
    _moviePlayer = [[MPMoviePlayerController alloc]init];

    [self readyPlayer];
    [self.view addSubview:_moviePlayer.view];

    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:_moviePlayer];
}

- (void) readyPlayer {
    _movieUrl    = [NSURL fileURLWithPath:[self nextVideo]];
    [_movieUrl retain];

    _moviePlayer.contentURL = _movieUrl;

    // For 3.2 devices and above
    if ([_moviePlayer respondsToSelector:@selector(loadState)]) {
        // Set movie player layout
        [_moviePlayer setControlStyle:MPMovieControlStyleNone];
        [_moviePlayer setFullscreen:YES];

        // May help to reduce latency
        [_moviePlayer prepareToPlay];

        // Register that the load state changed (movie is ready)
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePlayerLoadStateChanged:) 
                                                     name:MPMoviePlayerLoadStateDidChangeNotification 
                                                   object:nil];
    } else {
        // Register to receive a notification when the movie is in memory and ready to play.
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePreloadDidFinish:) 
                                                     name:MPMoviePlayerContentPreloadDidFinishNotification 
                                                   object:nil];
    }
}

/*---------------------------------------------------------------------------
 * For 3.1.x devices
 *--------------------------------------------------------------------------*/
- (void) moviePreloadDidFinish:(NSNotification*)notification {
    // Remove observer
    [[NSNotificationCenter  defaultCenter]  removeObserver:self 
                                                      name:MPMoviePlayerContentPreloadDidFinishNotification 
                                                    object:nil];

    // Play the movie
    [_moviePlayer play];
}

/*---------------------------------------------------------------------------
 * For 3.2 and 4.x devices
 *--------------------------------------------------------------------------*/
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
    NSLog(@"moviePlayerLoadStateChanged");
    // Unless state is unknown, start playback
    if ([_moviePlayer loadState] != MPMovieLoadStateUnknown) {
        // Remove observer
        [[NSNotificationCenter defaultCenter]  removeObserver:self
                                                         name:MPMoviePlayerLoadStateDidChangeNotification 
                                                       object:nil];

        // Set frame of movie player
        [[_moviePlayer view] setFrame:CGRectMake(0, 0, 480, 320)];
        // Play the movie
        [_moviePlayer play];
    }
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {    
    NSLog(@"playback finished...");
    NSLog(@"End Playback Time: %f", _moviePlayer.endPlaybackTime);
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (reason == MPMovieFinishReasonPlaybackEnded) {
        NSLog(@"Reason: movie finished playing");
    }else if (reason == MPMovieFinishReasonUserExited) {
        NSLog(@"Reason: user hit done button");
    }else if (reason == MPMovieFinishReasonPlaybackError) {
        NSLog(@"Reason: error");
    }

    [self playNextVideo];
}

- (void) playNextVideo {
    NSString *filePath = [self nextVideo];

    [_movieUrl release];
    _movieUrl = [NSURL fileURLWithPath:filePath];    
    [_movieUrl retain];

    _moviePlayer.contentURL = _movieUrl;
    [_moviePlayer play];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void) dealloc {
    [_moviePlayer release];
    [_movieUrl release];
    [_videoArray release];

    [super dealloc];
}

@end

现在,我的问题是通知MPMoviePlayerPlaybackDidFinishNotification被调用了两次。从上面的代码可以看出,我在viewDidLoad(in initPlayercalled from viewDidLoad)中只注册了一次。这是日志输出:

2012-07-02 12:29:17.661 DemoApp[1191:ef03] moviePlayerLoadStateChanged
2012-07-02 12:30:11.470 DemoApp[1191:ef03] playback finished...
2012-07-02 12:30:11.471 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:30:11.472 DemoApp[1191:ef03] Reason: movie finished playing
2012-07-02 12:30:11.474 DemoApp[1191:ef03] playback finished...
2012-07-02 12:30:11.475 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:30:11.476 DemoApp[1191:ef03] Reason: movie finished playing

2012-07-02 12:31:03.821 DemoApp[1191:ef03] playback finished...
2012-07-02 12:31:03.822 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:31:03.824 DemoApp[1191:ef03] Reason: movie finished playing
2012-07-02 12:31:03.826 DemoApp[1191:ef03] playback finished...
2012-07-02 12:31:03.827 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:31:03.827 DemoApp[1191:ef03] Reason: movie finished playing

如您所见,播放完成被调用了两次。这会导致从队列中跳过一个视频。(其实在原项目出现问题时,nextVideo预先从服务器缓存了一段视频,如果缓存中存在,则返回缓存视频的路径,否则返回nil)。在这里,首先sintel_trailer.mp4播放。在它完成播放后,它会播放 而elephants_dream_trailer.mp4不是big_buck_bunny_trailer.mp4。也就是说,它会循环播放在其间跳过的视频。那么,是什么导致MPMoviePlayerPlaybackDidFinishNotification调用两次呢?我正在为此工作两天,仍然没有运气。任何想法?

更新 1:

目前我正在使用moviePlayBackDidFinish:如下回调中的开关并且正在工作:

if (!_playNextVideo) {
    _playNextVideo = YES;
    return;
}
_playNextVideo = NO;
// code to play video....

但我仍然想知道是什么导致回调被调用两次。我觉得当前的 switch 解决方案就像一个 hack,并且喜欢删除它。

更新 2:

直到现在,我一直在用 iPhone 4.3 模拟器尝试这个。但是,当我用 iPhone 5.0 模拟器和 iPhone 5.1 模拟器尝试相同的程序时,它没有任何问题。也就是说,电影播放完毕后只发送一个回调。这使得我的小技巧(更新 1)变得无用(它解决了 4.3 中的问题,但在 5.0 和 5.1 中产生了问题)。我正在使用在 MacOSX Lion - 10.7.4 上运行的 Xcode 4.3.2。您对如何解决此问题有任何想法吗?为什么在 4.3 上有两个回调?

更新 3:

我查明线路导致问题。它在playNextVideo方法中。线路原因问题是_moviePlayer.contentURL = _movieUrl;。在第一个回调中更改它会导致MPMoviePlayerPlaybackDidFinishNotification再次发送。但是,它只发生在 iPhone 4.3 模拟器中。任何想法?

更新 4:

不过,我对这种奇怪的行为一无所知。所以,我现在正在使用类似于 UPDATE 1 中的时间技巧,如下所示moviePlayBackDidFinish:

NSTimeInterval currentCallback = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval difference      = currentCallback - _lastCallback;
_lastCallback                  = currentCallback;
if (difference < 5.0) {
    return;
}
// code to play video....
4

2 回答 2

4

我有同样的问题。并以这种方式解决了它:

我创建了一种跳过视频的新方法

- (void) skipVideo {
    [_moviePlayer stop];
}

停止播放器skipVideo将导致MPMovieFinishReasonPlaybackEnded通知(在模拟器和设备上)。现在设置播放器的contentUrl时,不会MPMovieFinishReasonPlaybackEnded引起其他通知,所以moviePlayBackDidFinish只调用一次;

在播放下一个视频(在playNextVideo)之前,你必须打电话

[_moviePlayer prepareToPlay];

这对我来说很好!

于 2012-10-22T14:23:09.240 回答
1

您可以为下一首曲目创建新播放器:

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: _movieUrl];
if (player)
{
    [self setMoviePlayer:player];
}
[self.moviePlayer play];

代替

self.moviePlayer.contentURL = _movieUrl;

通知MPMoviePlayerPlaybackDidFinishNotification将调用一次。

于 2012-11-11T08:38:11.290 回答