转自:http://www.xuebuyuan.com/2052817.html
如有侵犯,请来信oiken@qq.com
在UIWebView中,现在可以加载html5的video标签,可以直接在UIWebView中播放视频了,不在需要外在的播放器等,
但是在我的项目中,出现了一个问题,那就是希望点击多少集的按钮之后,直接播放视频而不是显示一个播放按钮从而进一步点击播放按钮播放,
想来能看到这篇博客的都是能够理解我上面说的意思,下面是两行代码的实现:
UIWebView *webViewIntro = [[UIWebViewalloc] initWithFrame:CGRectMake(0,0, 0,0)];
webViewIntro.allowsInlineMediaPlayback =YES;
webViewIntro.mediaPlaybackRequiresUserAction = NO;
如上所示,在需要直接播放视频的地方这样拼接链接地址:
NSString *strVideoAddress = [NSString stringWithFormat:
@"<html><body><video tabindex='0' controls='controls' autoplay='autoplay'><source src='%@'></video></body></html>",
URLAddress];
上面代码中主要的就是autoplay=‘autoplay’,这个可以让视频自动播放,但是最上面的两个UIWebView的设置是一定要写的,不然不会自动播放
-------------
我是这样做的:在UIViewcontroller 的 viewDidLoad 函数里面:
NSString *strHtml = [NSString stringWithFormat:
@"<!DOCTYPE hmtl>\n \
<html>\n \
<head>\n \
<title>%@</title>\n \
</head>\n \
<body>\n \
<video width=\"%d\" height=\"%d\" controls=\"controls\" autoplay=\"autoplay\">\n \
<source src=\"%@\">\n \
</video>\n \
</body>\n \
</html>", dicAssetDetails[@"title"], (int)self.view.frame.size.width - 10, (int)self.view.frame.size.height - 20, videoUrl];
//这里放size因为按done退出的时候,播放节目会缩一下,为了缩得中间位置而设置size
NSData *data = [strHtml dataUsingEncoding:NSUTF8StringEncoding];
[self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoPlayToEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoEnterFullScreen:) name:UIWindowDidBecomeVisibleNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoExitFullScreen:) name:UIWindowDidBecomeHiddenNotification object:nil];
三个对应的函数:
#pragma mark - Notification Functions
- (void)VideoPlayToEnd:(id)sender {
}
- (void)VideoEnterFullScreen:(id)sender {
}
- (void)VideoExitFullScreen:(id)sender {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self dismissViewControllerAnimated:YES completion:nil];
}