XCDYouTubePlayer,其初始化方式与使用MPMoviePlayerController相同。如何用这个播放多个视频(制作播放列表)。在MpMoviePlayerController中。我可以在播放电影时设置ContentUrl属性,然后在另一个电影结束时启动新电影。我怎么能在XCDYoutubePlayer中做到这一点呢?
XCDYouTubeVideoPlayerViewController *videoPlayerViewController =
[[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier: [NSString stringWithFormat:@"%@", [[playlists valueForKey:@"link"] objectAtIndex:rowNow]]];
[self presentMoviePlayerViewControllerAnimated: videoPlayerViewController];
答案 0 :(得分:0)
以下是我对XCDYouTubeKit {/ 3}}的回复的副本。
首先,您需要取消注册MPMoviePlayerPlaybackDidFinishNotification
,以便播放器不会在视频结束时自动关闭。
static void *MoviePlayerContentURLContext = &MoviePlayerContentURLContext;
- (IBAction) play:(id)sender
{
self.videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:firstVideoIdentifier];
[[NSNotificationCenter defaultCenter] removeObserver:self.videoPlayerViewController name:MPMoviePlayerPlaybackDidFinishNotification object:self.videoPlayerViewController.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.videoPlayerViewController.moviePlayer];
[self.videoPlayerViewController addObserver:self forKeyPath:@"moviePlayer.contentURL" options:(NSKeyValueObservingOptions)0 context:MoviePlayerContentURLContext];
[self presentMoviePlayerViewControllerAnimated:self.videoPlayerViewController];
}
然后您需要自己处理通知。管理 hasNextVideo 和 nextVideoIdentifier 对您有意义。
- (void) moviePlayerPlaybackDidFinish:(NSNotification *)notification
{
MPMovieFinishReason finishReason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (finishReason == MPMovieFinishReasonPlaybackEnded && [self hasNextVideo])
self.videoPlayerViewController.videoIdentifier = [self nextVideoIdentifier];
else
[self dismissMoviePlayerViewControllerAnimated];
}
您还需要将自己注册为电影播放器contentURL
的观察者,以自动开始播放下一个视频。
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == MoviePlayerContentURLContext)
[self.videoPlayerViewController.moviePlayer play];
else
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}