简体   繁体   English

使用HTML5在UIWebView中播放嵌入式视频

[英]Play embedded video in UIWebView with HTML5

I'm actually trying to play a local video into an UIWebView, with HTLM5. 我实际上正在尝试使用HTLM5将本地视频播放到UIWebView中。

What I do actually is this : 我实际上是这样的:

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

I have on my storyboard an UIWebView named webView with weak / nonatomic property 我的故事板上有一个名为webView的UIWebView,具有弱/非原子属性

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

When I launch the app, nothing happen : the screen still white and no video is playing. 当我启动该应用程序时,什么也没发生:屏幕仍然是白色,没有视频在播放。

Use this: 用这个:

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

Refer more this to how to play video using html5 更多的,请参阅如何使用播放HTML5视频

just change base url as 只需将基本网址更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM