我正在尝试在iOS 4.3的iPhone上播放一个简单的视频,并且正在解决关闭视频播放器的问题。下面是我设置的代码......我正在收听三个不同的通知事件,以便在电影播放器及其父视图退出时发出信号,但这些事件似乎都没有被触发。我的回调方法永远不会被调用。
NSString* moviePath = [[NSBundle mainBundle] pathForResource:@"sample1" ofType:@"m4v"];
NSLog(@"Here is the movie path: %@", moviePath);
NSURL* movieUrl = [NSURL fileURLWithPath:moviePath];
NSLog(@"Will now try to play movie at the following url: %@", movieUrl);
self.mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.mpvc.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinished:) name:MPMoviePlayerDidExitFullscreenNotification object:self.mpvc.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinished:) name:MPMoviePlayerWillExitFullscreenNotification object:self.mpvc.moviePlayer];
[[self.mpvc moviePlayer] prepareToPlay];
[[self.mpvc moviePlayer] setShouldAutoplay:YES];
[[self.mpvc moviePlayer] setFullscreen:YES];
[self.mpvc.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview:self.mpvc.view];
答案 0 :(得分:0)
您确定self.mpvc.moviePlayer是发送通知的有效对象。
你确定MPMoviePlayerPlaybackDidFinishNotification MPMoviePlayerDidExitFullscreenNotification MPMoviePlayerWillExitFullscreenNotification相同的通知名称..
以下是NSNotification的示例代码/示例
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(manageHistory:) name:@"historyLoaded" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"historyLoaded"
object:nil userInfo:jsonReturn];
- (void) manageHistory: (NSNotification *) historyData{
NSDictionary* _dict = historyData.userInfo;
NSLog(@"Your information embedded to dictionary obj %@",_dict);
}
答案 1 :(得分:0)
你可能在注册电影播放器回调时犯了一个错误。对象参数应为self.mpvc
而不是self.mpvc.moviePlayer
。所以尝试用这些代替你的3行:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.mpvc];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinished:) name:MPMoviePlayerDidExitFullscreenNotification object:self.mpvc];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinished:) name:MPMoviePlayerWillExitFullscreenNotification object:self.mpvc];