如何在iPhone中播放Youtubevideo

我想在应用程序中在iPhone中播放Youtubevideo。 我尝试使用MPMoviePlayerController,但没有加载video。 它正在播放我从video集中加载的video,但不播放video。 我也尝试通过将它嵌入UIWebView来播放Youtubevideo,但这也是徒劳的。 任何人都可以建议我采取一种方法。 我正在IOS 4.2中测试它

这是我使用的代码。

mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; if ([mp respondsToSelector:@selector(loadState)]) { // Set movie player layout [mp setControlStyle:MPMovieControlStyleFullscreen]; [mp setFullscreen:YES]; // May help to reduce latency [mp 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]; } // Register to receive a notification when the movie has finished playing. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

在iOS上播放YouTubevideo的唯一方法是使用默认的YouTube播放器播放它们。 您应该将video嵌入到uiwebview中,当您点击/点击它时,默认播放器将打开,请参阅此代码以在uiwebview中嵌入video –

 - (void)embedYouTube:(NSString*)url frame:(CGRect)frame { NSString* embedHTML = @"\ \ \ \ \ "; NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height]; if(videoView == nil) { videoView = [[UIWebView alloc] initWithFrame:frame]; [self.view addSubview:videoView]; } [videoView loadHTMLString:html baseURL:nil]; }