ios 在 Uiwebview 中显示视频不在设备全屏中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17922017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:33:03  来源:igfitidea点击:

Display video inside the Uiwebview not in device full screen

iphoneiosios5ios6ios4

提问by Naveen kumar

I want to display video inside the UIWebview. When we click on the video it plays in device full screen.

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

How do we play video inside the UIWebview? Please note that these video files are hosted on youtube or on our server. they are not bundled along with app

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

采纳答案by Paras Joshi

To play video in UIWebViewuse below code....

要播放正在UIWebView使用的视频,请使用以下代码....

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

UPDATE:

更新:

use this below code to display video in UIWebViewfrom video URL...

使用以下代码UIWebView从视频 URL 中显示视频...

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

If you want to display in small screen only and without webview then use bellow code..

如果您只想在小屏幕中显示而没有 webview 则使用波纹管代码..

MPMoviePlayerController *videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[player setControlStyle:MPMovieControlStyleNone];

player.view.frame = CGRectMake(0, 0, 150, 150);

[self.view addSubview:videoPlayer.view];

回答by iNeal

I think you should try this property of UIWebViewclass.

我认为你应该试试这个UIWebView类的属性。

allowsInlineMediaPlayback

A Boolean value that determines whether HTML5 videos play inline or use the native full-screen controller.

允许内联媒体播放

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

回答by Nate Flink

I had the same requirement, to play video inline inside a UIWebView. Additionally, I needed the video to play immediately without waiting for the user to press the play button.

我有同样的要求,在 UIWebView 内内联播放视频。此外,我需要立即播放视频,而无需等待用户按下播放按钮。

To accomplish this I added this property to the UIWebView:

为此,我将此属性添加到 UIWebView:

webView.allowsInlineMediaPlayback = YES;

Then, in combination it is also required to add a "webkit-playsinline" attribute be included within the HTML5 video element on the web page that is providing the video source url.

然后,还需要在提供视频源 url 的网页上的 HTML5 视频元素中添加“webkit-playsinline”属性。

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

See the apple doc: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/index.html#//apple_ref/doc/uid/TP40006950-CH3-SW32

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

Here is my complete Obj-C example to create a full screen UIWebView. Add this to a viewDidLoad method of a UIViewController:

这是我创建全屏 UIWebView 的完整 Obj-C 示例。将此添加到 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]];