为什么MPMovieDurationAvailableNotification只为我的MPMoviePlayerController的多个实例调度一次?

时间:2011-02-03 17:55:31

标签: iphone objective-c events ios grand-central-dispatch

我有一个UITableView,它列出了磁盘上的电影文件。对于每个单元格行,都为每个可见行分配了一个工作器实例,用于为影片文件生成缩略图并使其持续时间显示在该行中。

对于worker类中的每个MPMoviePlayerController实例,我都会从电影播放器​​中侦听MPMovieDurationAvailableNotification事件。由于某种原因,这个事件似乎只是从一个工作者实例发送(或者至少我只能捕获它)。这是init和listener代码。内联有一些评论。

- (id) initWithRequestAsset:(RequestAsset *)asset {
if (self = [super init]) {
    self.requestAsset = asset;
    self.moviePlayer = [MPMoviePlayerController alloc];
    [self setupMoviePlayerListeners];
    [self.moviePlayer initWithContentURL:self.requestAsset.urlPath];
    self.moviePlayer.shouldAutoplay = NO;

    //  I've also tried to retain the moviePlayer, to no avail
    [self.moviePlayer release];
}
return self;

}

- (void) setupMoviePlayerListeners {
//
// If the object: is set to nil then Im able to catch three notifications, but they are all from last instance of the MPMoviePlayerController
//
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(onMovieDurationAvailable:)
                                             name:MPMovieDurationAvailableNotification
                                           object:self.moviePlayer];

}

- (void) onMovieDurationAvailable:(NSNotification *)notification {
NSLog(@"duration received notification");

self.requestAsset.duration = [[notification object] duration];

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMovieDurationAvailableNotification object:self.moviePlayer];

}

我做错了什么?我想如果我将对象:参数设置为MPMoviePlayerController的实例,它将允许我只获取该实例的事件。但是,我似乎只收到了最后一次发送的通知。

1 个答案:

答案 0 :(得分:1)

您只能拥有1个活动的MPMoviePlayerController实例。您可以创建多个,但一次只能运行1个。

参见(约2个屏幕): http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/Reference/Reference.html

“注意:虽然您可以创建多个MPMoviePlayerController对象并在界面中显示其视图,但一次只能有一个电影播放器​​可以播放其电影。”