如何使用 MPMoviePlayerController 播放 youtube 视频?

How to play youtube video using MPMoviePlayerController?

我想播放 youtube url 的视频。 我正在使用以下代码,但它不起作用。

-(void)playVideoFromURL
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%s","https://www.youtube.com/embed/96ReVjMAXEE?autoplay=1&vq=small"]];
    self.videoController = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.videoController setControlStyle:MPMovieControlStyleNone];
    self.videoController.repeatMode=MPMovieRepeatModeOne;
    self.videoController.fullscreen=YES;
    self.videoController.scalingMode=MPMovieScalingModeFill;
    self.videoController.view.frame=CGRectMake(0,0,self.videoplayview.frame.size.width, self.videoplayview.frame.size.height);
    [self.videoplayview addSubview:self.videoController.view];
    [self.videoController play];
}

您无法在 MPMoviePlayerController 中播放 YouTube 视频 URL。为此你必须使用

youtube-iso-player-helper - 但是你不能在 youtube-iso-player-helper

中播放私人视频 URL

XCDYoutubeKit - 这违反了 YouTube 条款和服务。

遗憾的是,无法使用 MPMoviePlayerController 直接播放 youtube 视频,因为 youtube 不会直接向视频文件公开 links。

在您自己的应用程序中播放 youtube 视频的唯一方法是创建一个 UIWebView,其中包含来自 Youtube 的嵌入标记,用于您要播放的电影作为 UIWebView 的内容。 UIWebView 将检测到嵌入的对象是 Youtube link,并且 Web 视图的内容将是视频的 youtube 预览。当您的用户单击预览时,视频将显示在 MPMoviePlayerController 中。