ios 使用 MPMoviePlayerController 而不是 UIWebView 播放 YouTube 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1779511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:37:49  来源:igfitidea点击:

Play YouTube videos with MPMoviePlayerController instead of UIWebView

iosvideostreamingyoutubempmovieplayercontroller

提问by jmurphy

I'm trying to stream some youTube videos using the MPMoviePlayerController but I'm having some problems. The code I'm using is pretty simple and I can play .m4v videos by passing a URL to initWithContentURL. When I launch the movie player the player comes up but just goes away after about 20 seconds. When I try it in the simulator I get an alert view that says the server is not configured correctly. Is there an argument I need to pass with the URL to get a specific type of video feed from google?

我正在尝试使用 MPMoviePlayerController 流式传输一些 YouTube 视频,但我遇到了一些问题。我使用的代码非常简单,我可以通过将 URL 传递给 initWithContentURL 来播放 .m4v 视频。当我启动电影播放器​​时,播放器出现但在大约 20 秒后消失。当我在模拟器中尝试时,我收到一个警告视图,指出服务器配置不正确。是否需要通过 URL 传递参数才能从谷歌获取特定类型的视频源?

NSURL *videoURL = [NSURL URLWithString:@"http://www.youtube.com/v/HGd9qAfpZio&hl=en_US&fs=1&"];
MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

[moviePlayer play];

I've also tried the following URL's http://www.youtube.com/watch?v=HGd9qAfpZio

我也试过以下网址的 http://www.youtube.com/watch?v=HGd9qAfpZio

I have also seen the argument &format=1 and tried to add that to the end of both of the strings but no luck.

我也看到了参数 &format=1 并试图将它添加到两个字符串的末尾,但没有运气。

采纳答案by Bryan Henry

The only way to have a youtube video play inside your own app is to create a UIWebViewwith the embed tag from Youtube for the movie you want to play as the UIWebView'scontent. UIWebViewwill detect that the embedded object is a Youtube link, and the web view's content will be the youtube preview for the video. When your user clicks the preview, the video will be shown in an MPMoviePlayerController. This is the technique described at the link that Muxecoid provided (how to play youtube videos within an application), and this is (as far as I know of) the only way to have a youtube video play within an application. You can still have the Youtube application launch by passing the youtube URL to -[UIApplication openURL:], but of course this closes your own application which is often undesirable.

在您自己的应用程序中播放 youtube 视频的唯一方法是UIWebView使用 Youtube 中的 embed 标签为要作为UIWebView's内容播放的电影创建一个。UIWebView将检测到嵌入的对象是 Youtube 链接,网络视图的内容将是视频的 youtube 预览。当您的用户单击预览时,视频将显示在 MPMoviePlayerController 中。这是 Muxecoid 提供的链接中描述的技术(如何在应用程序中播放 youtube 视频),这是(据我所知)在应用程序中播放 youtube 视频的唯一方法。您仍然可以通过将 youtube URL 传递到 来启动 Youtube 应用程序-[UIApplication openURL:],但这当然会关闭您自己的应用程序,这通常是不受欢迎的。

Unfortunately, there's no way to directly play a youtube video with MPMoviePlayerControllerbecause youtube does not expose direct links to the video files.

不幸的是,没有办法直接播放 youtube 视频,MPMoviePlayerController因为 youtube 没有公开视频文件的直接链接。

回答by Jimit

If you are using Code:

如果您使用的是代码:

- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {  
     NSString* embedHTML = @"\ 
        <html><head>\ 
     <style type=\"text/css\">\ 
     body {\ 
     background-color: transparent;\ 
     color: white;\ 
    }\ 
   </style>\ 
    </head><body style=\"margin:0\">\ 
       <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \ 
    width=\"%0.0f\" height=\"%0.0f\"></embed>\ 
       </body></html>";  
   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];  
  }

Source:

来源:

Just refer this link

只需参考此链接

Make sure that you test it on device and not on the simulator. Since simulator will always display question mark with blue box (it doesn't have quick-time player).

确保您在设备上而不是在模拟器上测试它。由于模拟器将始终显示带蓝色框的问号(它没有快速播放器)。

回答by hellozimi

As I wrote above in my comment I had to do this in a project I was working on. I've solved this problem and submitted the code on github.

正如我在上面的评论中所写的那样,我必须在我正在从事的项目中做到这一点。我已经解决了这个问题并在github上提交了代码。

You can check the source and fork it here.

您可以在此处查看源代码并对其进行分叉。

It basically takes a youtube url and gets all the iOS compatible video urls.

它基本上需要一个 youtube 网址并获取所有与 iOS 兼容的视频网址。

Hope this is to any help!
Cheers

希望这对任何帮助!
干杯

回答by TPoschel

I just stumbled across someone who was able to place a YouTubevideo inside an MPMoviePlayerController. It does appear possible now.

我只是偶然发现了一个能够将YouTube视频放入MPMoviePlayerController. 现在看来确实有可能。

LBYouTubeView

LBYouTubeView

回答by D_D

To play you tube videos you need to extract the url before passing the url into MPMoviePlayer. You can't play it directly.

要播放您的管视频,您需要在将 url 传递到MPMoviePlayer. 不能直接播放。

My github repo has a demo implementation of this.

我的 github repo 有一个演示实现。

See:

看:

https://github.com/DpzAtMicRO/IOSYoutubePlayer

https://github.com/DpzAtMicRO/IOSYoutubePlayer

Try it, this makes it possible to load and play youtube video directly in MPMoviePlayerlike any other video and is pretty good approach too.

试试吧,这使得MPMoviePlayer像任何其他视频一样直接加载和播放 YouTube 视频成为可能,这也是一个很好的方法。

EDIT: Make sure that you go through the Readme.md well before using this in your project

编辑:在您的项目中使用它之前,请确保您仔细阅读了 Readme.md

回答by jjxtra

It's as simple as taking the src element of the iframe for the new embed code and calling

就像将 iframe 的 src 元素作为新的嵌入代码并调用一样简单

[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];

回答by arango_86

Check the following link. I think it will help you.

检查以下链接。我认为它会帮助你。

https://mobiletechfeast.blogspot.in/2014/08/youtube-player-for-ios.html

https://mobiletechfeast.blogspot.in/2014/08/youtube-player-for-ios.html

回答by Ganpat

There is an official way to embed YouTube video in iOS application which is provided by Google.

谷歌提供了一种在 iOS 应用程序中嵌入 YouTube 视频的官方方法。

Install library via cocoapods: pod "youtube-ios-player-helper"

通过 cocoapods 安装库:pod "youtube-ios-player-helper"

import by: #import “YTPlayerView.h”Use UIViewand set class to YTPlayerViewor create object of YTPlayerViewprogramatically.

导入方式:以编程方式#import “YTPlayerView.h”使用UIView并将类设置为YTPlayerView或创建对象YTPlayerView

Load video: [self.playerView loadWithVideoId:@"M7lc1UVf-VE"];

加载视频: [self.playerView loadWithVideoId:@"M7lc1UVf-VE"];

There is also playVideoand stopVideomethods available. For more info visit this Link

还有playVideostopVideo方法可用。有关更多信息,请访问 此链接