【问题标题】:Play YouTube videos in iPhone app without using UIWebView?在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView?
【发布时间】:2012-02-22 00:39:48
【问题描述】:

我想通过我的 iPhone 应用播放 YouTube 视频。我必须尝试使用​​以下代码在我的 iPhone 应用中播放 YouTube 视频,

[self playVideo:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 70, 280, 250)];

- (void)playVideo:(NSString *)urlString 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, urlString, frame.size.width, frame.size.height];
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
    [videoView loadHTMLString:html baseURL:nil];
    [self.view addSubview:videoView];
    [videoView release];
    NSLog(@"%@",html);
}

但是,这段代码对我不起作用。我也试过MPMoviePlayerController,代码是,

NSURL *fileURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
[self.view addSubview:moviePlayerController.view];  
moviePlayerController.fullscreen = YES;  
[moviePlayerController play];

这对我也不起作用。谁能告诉我我在哪里做错并建议我任何想法?我想在 MPMoviewPlayerController 中播放 YouTube 视频,而不是使用 UIWebView。提前致谢。

【问题讨论】:

标签: iphone ios uiwebview youtube mpmovieplayercontroller


【解决方案1】:

Yuvaraj.M 先生,您的上述代码是正确的。请在您的 iPhone/iPod touch 设备中运行该项目。 youtube 视频可能不支持/无法在模拟器中使用。请检查一下。谢谢。

【讨论】:

  • Gopinath 先生感谢您的回答。我会检查你的答案。谢谢。
【解决方案2】:

不要使用您正在使用的 HTML 嵌入代码,而是转到 YouTube 网站,找到一个视频,使用分享按钮,然后使用嵌入按钮来获取一些嵌入 HTML 示例。使用该模式嵌入视频,而不是您当前使用的 Flash 版本。

【讨论】:

    【解决方案3】:

    只需使用XCDYouTubeVideoPlayerViewController。无需UIWebView,即可将 youtube 视频无缝添加到您的应用中。

    它使用渐进式下载 + MoviePlayerAVFoundation 框架。只需为其提供一个 youtube 视频 ID,您就可以开始使用了。

    它支持全屏和非全屏以及在iOS模拟器上运行!!!

    例子:

    XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
    [self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
    

    【讨论】:

    • 我使用 pod 添加这个库,以及我想使用哪个头文件。
    • @R.Mohan 应该是 #import &lt;XCDYouTubeKit/XCDYouTubeKit.h&gt;
    【解决方案4】:

    现在有一个YTPlayerView 类可用于YouTube Data API (v3)

    看看Using the YouTube Helper Library to embed YouTube videos in your iOS application

    它提供了有关如何安装帮助程序库以及将 YTPlayerView 合并到情节提要中的全面说明。

    【讨论】:

    • Lib 仅使用 UIWebView
    【解决方案5】:

    在 swift 4.1 中播放 youtube 视频

    添加 Pod pod 'XCDYouTubeKit'

    在 ViewController 中导入 XCDYouTubeKit

    func playYoutubeVideo(){
    
    let strUrl = "https://www.youtube.com/watch?v=9m_K2Yg7wGQ"
    if let range = strUrl.range(of: "=") {
            let strIdentifier = strUrl.substring(from: range.upperBound)
            print("Identifier:\(strIdentifier)")
            let videoPlayerViewController = 
            XCDYouTubeVideoPlayerViewController(videoIdentifier: strIdentifier)
            videoPlayerViewController.present(in: viewYTVideo)
            videoPlayerViewController.moviePlayer.play()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 2012-04-05
      • 2010-12-19
      • 2012-03-17
      • 2016-11-30
      • 2011-04-25
      • 2011-05-05
      • 2011-08-27
      • 1970-01-01
      相关资源
      最近更新 更多