如何在iPhone中的应用程序中播放youtube / vimeo视频 [英] How to play youtube/vimeo video within the application in iPhone

查看:163
本文介绍了如何在iPhone中的应用程序中播放youtube / vimeo视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,我想从Youtube,Vimeo,Direct url等网址播放视频。我正在使用AVPlayer制作自定义播放器,从视频剪辑的直接网址(如www.abc/play.mp4)播放视频。但是后来我遇到了一个很大的问题来玩youtube& vimeo视频。在搜索了很多之后,我发现这些链接所说的不使用UIWebview我无法播放Youtube链接:



和。



您在 UIViewController 中看到的视频可以其当前大小播放。



这是我用过的唯一代码:

  UIView * videoContainerView = [[UIView alloc] initWithFrame :CGRectMake(0,50,320,200)]; 
[self.view addSubview:videoContainerView];
XCDYouTubeVideoPlayerViewController * videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@_ OBlgSz8sSM];
[videoPlayerViewController presentInView:videoContainerView];
[videoPlayerViewController.movi​​ePlayer play];


I am building an application where I want to play video from url like Youtube, Vimeo, Direct url. I am making custom player using AVPlayer to play a video from a direct url of video clip(like www.abc/play.mp4). But later I faced a huge issue to play youtube & vimeo video. And after searching a lot I found these link where it says without using UIWebview I can't play a Youtube link:

Play YouTube videos with MPMoviePlayerController instead of UIWebView

Playing video from youtube link without UIWebView

So I just used this code:

NSString *youTubeVideoHTML = @"<html><head><style>body{margin:0;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:\'100%%\', height:'200px', videoId:\'%@\', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";

NSString *html = [NSString stringWithFormat:youTubeVideoHTML, videoId];
self.embedded_player_view.mediaPlaybackRequiresUserAction = NO;

[self.embedded_player_view loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];

Now when I click on youtube/Vimeo video link from tableview its playing the video with the default player i.e. quicktime player. Its not running the video within the UIWebview frame itself. But I want to show the video in the 1st half of the screen i.e my UIWebview frame. Is that possible?

In my app I can see this:

when clicking on red play button I can see the video in full screen in quicktime player like this:

But I want to show the video within the same webView frame not via quick time player. It should play like this:

Same does in MusicTube and PlayTube.

Or is there any other way to achieve the same? Any help will be appreciated. Thanks in advance.

解决方案

I've used this class just for that.

The video you see inside the UIViewController is playable in its current size.

That's the only code I've used:

UIView *videoContainerView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 200)];
    [self.view addSubview:videoContainerView];        
    XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"_OBlgSz8sSM"];
    [videoPlayerViewController presentInView:videoContainerView];
    [videoPlayerViewController.moviePlayer play];

这篇关于如何在iPhone中的应用程序中播放youtube / vimeo视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆