没有自己的流媒体服务器,又想在app中用原生视频播放器MPMoviePlayerController将视频插入到界面,在界面可以直接播放,或者全屏播放,可以通过以下方法实现
先创建原生播放器
_moviePlayer = [[MPMoviePlayerController alloc] init];_moviePlayer.controlStyle = MPMovieControlStyleEmbedded;_moviePlayer.shouldAutoplay = NO;_moviePlayer.view.frame = CGRectMake(0, 0, HX_SCREEN_WIDTH, 200);[self.scrollView addSubview:_moviePlayer.view];
然后请求优酷等视频网页加载数据
NSURL *fileURL = [NSURL URLWithString:<span style="background-color: rgb(240, 240, 240);">@"你要提取流媒体地址的优酷等网页"</span>];NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];UIWebView *webView = [[UIWebView alloc]init];webView.delegate = self;[webView loadRequest:request];
然后通过实现UIWebView的委托方法:
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{NSString *lJs = @"document.documentElement.innerHTML";NSString *lHtml = [webView stringByEvaluatingJavaScriptFromString:lJs];NSLog(@"html内容:%@",lHtml);
//上面只是打印信息供参考,下面才是提取流媒体// NSString *lJs2 = @"(document.getElementsByTagName(\"video\")[0]).getElementsByTagName(\"source\")[0].src"; //qiyiNSString *lJs2 = @"(document.getElementsByTagName(\"video\")[0]).src"; // youku,tudou,ku6 ,souhuNSString *lm3u8 = [webView stringByEvaluatingJavaScriptFromString:lJs2];NSLog(@"video source:%@",lm3u8);// 加载到原生播放器播放NSURL *movieUrl = [NSURL URLWithString:lm3u8];_moviePlayer.contentURL = movieUrl;[_moviePlayer prepareToPlay];
}
说明:webView要add到你的视图中去,否则它是永远不会去加载请求的;