我有一个 UITableView,它列出了磁盘上的电影文件。对于每个单元格行,为每个可见行分配一个工作实例,用于为电影文件生成缩略图并获取其在行中显示的持续时间。
对于工作类中的每个 MPMoviePlayerController 实例,我正在监听来自电影播放器的 MPMovieDurationAvailableNotification 事件。由于某种原因,这个事件似乎只能从其中一个工作实例中分派(或者至少我只能捕获它)。这是初始化和侦听器代码。内联有一些评论。
- (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];
}
我究竟做错了什么?我想如果我将 object: 参数设置为 MPMoviePlayerController 的实例,它将只允许我获取该实例的事件。但是,我似乎只收到了最后一次发送的通知。