在Uiwebview中显示video不在设备全屏幕中

我想在UIWebview中显示video。 当我们点击video时,它会在设备上全屏播放。

我们如何在UIWebview中播放video? 请注意,这些video文件托pipe在YouTube或我们的服务器上。 他们不与应用程序捆绑在一起

UIWebView使用下面的代码播放video….

 NSString *strVedio = @"<video controls> <source src=\"YourVideo.mp4\"> </video>"; NSString *path = [[NSBundle mainBundle] pathForResource:@"YourVideo" ofType:@"mp4"]; [Webview loadHTMLString:strVedio baseURL:[NSURL fileURLWithPath:path]]; 

更新:

使用下面的代码来显示videoURL中的UIWebViewvideo…

  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 *strHtml = [NSString stringWithFormat:embedHTML, yourURLString, width,height];//set width and height which you want [webView loadHTMLString:strHtml baseURL:nil]; [self.view addSubview:webView]; 

如果你想显示在只有小屏幕,没有webview然后使用波纹pipe代码..

 MPMoviePlayerController *videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]]; [player setControlStyle:MPMovieControlStyleNone]; player.view.frame = CGRectMake(0, 0, 150, 150); [self.view addSubview:videoPlayer.view]; 

我想你应该尝试UIWebView类的这个属性。

allowsInlineMediaPlayback

一个布尔值,用于确定HTML5video是内联播放还是使用本机全屏控制器。

我有相同的要求,在UIWebView内播放video。 此外,我需要立即播放video,而无需等待用户按播放button。

为了实现这个,我把这个属性添加到了UIWebView:

 webView.allowsInlineMediaPlayback = YES; 

然后,在组合中,还需要在提供video源URL的网页上的HTML5video元素中添加“webkit-playsinline”属性。

 <video src="assets/myMovie.m4v" webkit-playsinline></video> 

请参阅apple文档: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/index.html#//apple_ref/doc/uid/TP40006950-CH3-SW32

这里是我完整的Obj-C示例来创build一个全屏UIWebView。 将其添加到UIViewController的viewDidLoad方法中:

 NSURL *websiteUrl = [NSURL URLWithString:@"http://example.com/myMovie.m4v"]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl]; UIWebView * webView = [[UIWebView alloc] init]; webView.allowsInlineMediaPlayback = YES; webView.mediaPlaybackRequiresUserAction = NO; webView.opaque = NO; webView.backgroundColor = [UIColor clearColor]; [webView setTranslatesAutoresizingMaskIntoConstraints:NO]; [webView loadRequest:urlRequest]; [self.view addSubview:webView]; // Width constraint [self.view addConstraint:[NSLayoutConstraint constraintWithItem:webView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]]; // Height constraint [self.view addConstraint:[NSLayoutConstraint constraintWithItem:webView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]]; // Center horizontally [self.view addConstraint:[NSLayoutConstraint constraintWithItem:webView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]]; // Center vertically [self.view addConstraint:[NSLayoutConstraint constraintWithItem:webView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];