如何在iPhone中的应用程序内播放youtube / vimeovideo

我正在build立一个应用程序,我想要从像Youtube,Vimeo,直接url的URL播放video。 我正在使用AVPlayer自定义播放器从video剪辑的直接url(如www.abc/play.mp4)播放video。 但后来我遇到了一个很大的问题,玩YouTube和VIMEOvideo。 search了很多后,我发现这些链接,它说,不使用UIWebview我不能播放Youtube链接:

使用MPMoviePlayerController而不是UIWebView播放YouTubevideo

在没有UIWebView的情况下从YouTube链接播放video

所以我只是使用这个代码:

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]]; 

现在,当我点击youtube / Vimeovideo链接从视图播放video与默认播放器即quicktime播放器。 它没有在UIWebview框架内运行video。 但我想在屏幕的前半部分显示video,即我的UIWebview框架。 那可能吗?

在我的应用程序,我可以看到这个:

当点击红色的播放button时,我可以在quicktime播放器中以全屏方式看到video,如下所示:

在这里输入图像说明在这里输入图像说明

但我想在同一个webView框架中显示video,而不是通过快速播放器。 它应该像这样玩:

在这里输入图像说明

MusicTube和PlayTube也一样 。

或者有没有其他方法可以实现? 任何帮助将不胜感激。 提前致谢。

在这里输入图像说明在这里输入图像说明

我只是为了这个而使用了这门课 。

您在UIViewController看到的video可以播放当前的大小。

这是我用过的唯一代码:

 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]; 

对于Vimeo播放器,你可以检查这个链接https://github.com/lilfaf/YTVimeoExtractor它播放真正的播放器中的video,如果你想在uiwebview中运行video,这里是该代码为https://stackoverflow.com / a / 15918011/1865424 。

在“urlStr”中传递YouTubevideo的url。

 //In .h file UIWebView *videoView; // In .m file videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 385)]; [self embedYouTube :urlStr frame:CGRectMake(0, 0, 320, 385)]; [self.view addSubview:videoView]; // methos to embed URL in HTML & load it to UIWebView - (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]; } 

Courtsey: – http://nanostuffs.com/Blog/?p=641

希望这会帮助你。 如果这不能帮助你,请查看这些链接:

http://blog.softwareispoetry.com/2010/03/how-to-play-youtube-videos-in-your.html

https://gist.github.com/darkredz/5334409

http://maniacdev.com/2012/02/open-source-library-for-easily-playing-a-youtube-video-in-an-mpmovieplayer

如果你想玩youtube,这里有一个链接到youtube播放器项目在github上,这真的很有帮助。 是的,可以在你的uiwebview中播放它,只要给webview的url并告诉它加载,它不应该在默认播放器中打开,据我所知至less。

我不知道Vimeo,但是对于youtubevideo,您可以使用HCYouTubeParser获取YouTubevideo的mp4url,然后根据需要在AVPlayer或MpMoviePlayerVC上播放。

我在我的项目中使用了YouTube和VIMEO,我分享我的编码,这将是非常有希望的

在我看来controller.m

 //Button action - (IBAction)importAudioClip:(id)sender { flag = 0; customActionSheet = [[UIActionSheet alloc]initWithTitle:@"Select Audio/Video from:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"YouTube",@"Vimeo", nil]; [customActionSheet showInView:self.view]; } #pragma ActionSheet Delegate Methods -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"CLiekc button is %i",buttonIndex); if([actionSheet.title isEqualToString:@"Select Audio/Video from:"]) { if (buttonIndex == 0) { videoStatus=0; webView.hidden = NO; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com"]]]; NSLog(@"Taking from Youtube"); } else if (buttonIndex == 1) { videoStatus=1; UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Vimeo" message:@"Please enter Vimeo Username" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; alertView.tag=123; alertView.alertViewStyle = UIAlertViewStylePlainTextInput; [alertView show]; } } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag==123) { if (buttonIndex==1) { templateText=[[UITextField alloc]init]; templateText = [alertView textFieldAtIndex:0]; templateText.autocapitalizationType=UITextAutocapitalizationTypeWords; templateText.delegate=self; if ([templateText.text length]!=0) { NSString *str=[templateText.text capitalizedString]; NSLog(@"Str== %@",str); [self getVimeoDetails:str]; } } } } -(void)getVimeoDetails:(NSString*)userName { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://vimeo.com/api/v2/%@/videos.json",userName]]]; [request setHTTPMethod:@"GET"]; NSError *err; NSURLResponse *response; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding]; NSLog(@"The value is==%@",resSrt); vimeoDetailsArray =(NSArray*) [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err]; NSLog(@"jsonObject== %i",[vimeoDetailsArray count]); NSString *theReply = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding: NSASCIIStringEncoding]; NSLog(@"the reply == %@",theReply); if(response) { if (vimeoDetailsArray==NULL) { NSLog(@"its Null"); NSLog(@"null response== %@",response); // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert!!!" message:[NSString stringWithFormat:@"%@",theReply] delegate:self // cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; // [alert show]; // [self performSelector:@selector(showAlertMessage:theReply withTitle:@"Alert!!!") withObject:nil afterDelay:5]; vimeoVideoTable.hidden=YES; [self showAlertMessage:[NSString stringWithFormat:@"%@",theReply] withTitle:@"Alert!!!"]; } else { [self createTableView]; vimeoVideoTable.hidden=NO; [vimeoVideoTable reloadData]; NSLog(@"got response== %@",response); } } else { NSLog(@"faield to connect"); } if ([responseData length] == 0 && err == nil) { NSLog(@"Nothing was downloaded."); } else if (err != nil){ if ([[err description] rangeOfString:@"The Internet connection appears to be offline"].location != NSNotFound) { NSLog(@"string does not contain "); UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert!!!" message:@"Please Check your Internet Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } NSLog(@"Error = %@", err); } } -(void)createTableView { if (vimeoVideoTable !=nil) { [vimeoVideoTable removeFromSuperview]; vimeoVideoTable=nil; } vimeoVideoTable=[[UITableView alloc]initWithFrame:CGRectMake(10, 20, 300, self.view.bounds.size.height-100)]; [vimeoVideoTable setDelegate:self]; [vimeoVideoTable setHidden:YES]; [vimeoVideoTable setDataSource:self]; [self.view addSubview:vimeoVideoTable]; //[vimeoVideoTable reloadData]; } 

我绝对build议youtube_ios_player_helper ,“赞助” 谷歌 。

 import youtube_ios_player_helper class YTViewController: YTPlayerViewDelegate { @IBOutlet weak var player: YTPlayerView! // MARK: - Public properties - var videoID = "k_okcNVZqqI" // MARK: - View life cycle - override func viewDidLoad() { super.viewDidLoad() self.player.delegate = self } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.load() self.player.fadeOut() } /** Play video with the relative youtube identifier. */ func load() { Utils.showHUD(self.view) let options = ["playsinline" : 1] self.player.loadWithVideoId(self.videoID, playerVars: options) } /** Stop video playing. */ func stop() { } // MARK: - YOUTUBE video player delegate - func playerViewDidBecomeReady(playerView: YTPlayerView) { self.player.playVideo() } func playerView(playerView: YTPlayerView, didChangeToState state: YTPlayerState) { switch state { case .Playing: self.player.fadeIn(duration: 0.5) Utils.hideHUD(self.view) print("Started playback") break; case .Paused: print("Paused playback") break; default: break; } } } 

用这个

 NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ; [html appendString:@"<html><head>"]; [html appendString:@"<style type=\"text/css\">"]; [html appendString:@"body {"]; [html appendString:@"background-color: transparent;"]; [html appendString:@"color: white;"]; [html appendString:@"}"]; [html appendString:@"</style>"]; [html appendString:@"</head><body style=\"margin:0\">"]; [html appendString:@"<iframe src=\"//player.vimeo.com/video/84403700?autoplay=1&amp;loop=1\" width=\"1024\" height=\"768\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>"]; [html appendString:@"</body></html>"]; [viewWeb loadHTMLString:html baseURL:urlMovie];