【问题标题】:Play embedded video in UIWebView with HTML5使用 HTML5 在 UIWebView 中播放嵌入的视频
【发布时间】:2012-06-22 09:01:27
【问题描述】:

我实际上是在尝试使用 HTLM5 将本地视频播放到 UIWebView 中。

我实际上是这样做的:

NSString *embedHTML = [NSString stringWithFormat:@"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
< src=\"%@\"  \
</embed>\
</body></html>", [[NSBundle mainBundle] pathForResource:@"ash(1).mov" ofType:nil]];



[webView setOpaque:NO];
NSString *html = [NSString stringWithFormat:embedHTML, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];

我的故事板上有一个名为 webView 的 UIWebView,具有弱/非原子属性

@property (weak, nonatomic) IBOutlet UIWebView *webView;

当我启动应用程序时,什么也没有发生:屏幕仍然是白色的并且没有视频正在播放。

【问题讨论】:

    标签: iphone cocoa html ios5 uiwebview


    【解决方案1】:

    只需将基本网址更改为

    NSString *videoPath =[[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"mov"];
    
    NSString *htmlString=[NSString stringWithFormat:@"<body> <video src=\"%@\" controls autoplay height=\"400\" width=\"300\" > </video> </body>", videoPath];
    
    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    [xwebview loadHTMLString:htmlString baseURL:baseURL];
    

    【讨论】:

      【解决方案2】:

      使用这个:

      NSString *embedHTML = [NSString stringWithFormat:@"<html><style></style></head><body><video src=\"%@ controls autoplay height=\"400 width=\"320></video><ul></body></html>",  [[NSBundle mainBundle] pathForResource:@"ash(1)" ofType:@"mov"]];
      
      [webView setOpaque:NO];
      //NSString *html = [NSString stringWithFormat:embedHTML, webView.frame.size.width,  webView.frame.size.height];
       NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"ash(1)" ofType:@"mov"]];
      [webView loadHTMLString:embedHTML baseURL:url];
      

      更多参考this如何使用html5播放视频

      【讨论】:

      • 上面的代码和我做的完全一样,只是显示了一个白屏:(我已经看到了这个链接,它对 HTML5 本身非常有用,但不能真正通过UIWebView。
      • 检查编辑的答案,你的 [[NSBundle mainBundle] pathForResource:@"ash(1)" ofType:@"mov"] 是正确的
      • 没什么变化 :( 是的,视频的路径是有效的,我用NSLog(@"%@", [[NSBundle mainBundle] pathForResource:@"ash(1)" ofType:@"mov"]);检查了它,它返回了2012-06-20 12:47:10.620 testVideoUIWebView[5440:707] /var/mobile/Applications/EC8114CF-C55C-4062-B7C5-25FA9DDFC9F0/testVideoUIWebView.app/ash(1).mov
      猜你喜欢
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2013-04-14
      • 2018-01-16
      相关资源
      最近更新 更多