将视频嵌入到从应用程序的 Documents 文件夹加载到 UIWebView 中的 html 文件中
我有一个名为 videoplay.html
的 html 文件,其中包含以下内容
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>
This is demo html file for playing movie file embedded.
</p>
<p>
<video controls>
<source src="myvideoname.mov">
</video>
</p>
</body>
</html>
,同时使用以下代码 (从应用程序包加载)
它加载 html 内容并显示电影并能够播放电影文件。
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"videoplay" ofType:@"html"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:content baseURL:baseURL];
使用以下代码(从应用程序的文档文件夹加载)
它加载html内容,但显示黑色部分而不是电影文件。
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0]
NSString *pathOfHTmlFile = [documentsDir stringByAppendingPathComponent:@"videoplay.html"];
NSString *content = [NSString stringWithContentsOfFile:pathOfHTmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:content baseURL:[NSURL URLWithString:documentsDir]];
上面的代码中缺少什么导致我的代码无法工作?有什么想法吗?
我希望我的应用程序具有 UIFileSharingEnabled 功能,该功能使用户能够将视频放入文档文件夹本身,这样我的视频文件将仅位于文档文件夹中。
赏金更新 我发现了如下内容。但它仍然只适用于较小的视频(大约小于 50 MB),如果视频较大(大约大于 100 MB),则它不起作用。
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
NSString *pathOfHTmlFile = [documentsDir stringByAppendingPathComponent:@"videoplay.html"];
NSString *content = [NSString stringWithContentsOfFile:pathOfHTmlFile encoding:NSUTF8StringEncoding error:nil];
documentsDir = [documentsDir stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
documentsDir = [documentsDir stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//", documentsDir]];
NSLog(@"Baseurl--->%@",baseURL);
[self.webView loadHTMLString:content baseURL:baseURL];
由于我必须在我的项目中实现这一点,所以没有其他方法可以为我做。
I have an html file named videoplay.html
with following content
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>
This is demo html file for playing movie file embedded.
</p>
<p>
<video controls>
<source src="myvideoname.mov">
</video>
</p>
</body>
</html>
while Using following code (Loading from application bundle)
It loads html content and shows movie and able to play movie file.
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"videoplay" ofType:@"html"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:content baseURL:baseURL];
Using following code (Loading from Document folder of application)
It loads html content but shows black portion instead of movie file.
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0]
NSString *pathOfHTmlFile = [documentsDir stringByAppendingPathComponent:@"videoplay.html"];
NSString *content = [NSString stringWithContentsOfFile:pathOfHTmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:content baseURL:[NSURL URLWithString:documentsDir]];
What am I missing in above code which cause my code not to work ? Any Idea ?
I want my application with feature UIFileSharingEnabled which enables user to put videos in the document folder itself so my video files would be in documents folder only.
Update for Bounty
I found something as follows. But still it only works for smaller videos(less than 50 MB approx) if videos are larger (greater than 100 MB approx) it is not working.
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
NSString *pathOfHTmlFile = [documentsDir stringByAppendingPathComponent:@"videoplay.html"];
NSString *content = [NSString stringWithContentsOfFile:pathOfHTmlFile encoding:NSUTF8StringEncoding error:nil];
documentsDir = [documentsDir stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
documentsDir = [documentsDir stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//", documentsDir]];
NSLog(@"Baseurl--->%@",baseURL);
[self.webView loadHTMLString:content baseURL:baseURL];
As I must need to implement this in my project there is no other way to do for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
经过大量的努力和 R&DI 发现,
在
Simulator 3.2
和具有 iOS 4.3.3 版本的设备上,使用以下代码,如果 HTML 和视频文件是,则可以正常工作在同一位置。
仍然在
Simulator 4.2
下它不起作用。它显示黑色部分而不是视频。然而无法找到 Simulator 4.2 的解决方案,但它可以在设备上运行,所以我猜代码绝对没问题。将其作为答案发布,以便任何人做同样的事情可能有用。
After lots of struggling and R&D I found that,
Under
Simulator 3.2
and ondevice with iOS 4.3.3
version, using following code it is working if HTML and Video files are at the same location.Still under
Simulator 4.2
it does not work. Instead of video it shows black portion. Yet unable to find the solution forSimulator 4.2
but it works on device so I guess code is absolutely fine.Posting this as answer so that if any one doing the same may be useful.
使用 HTML5 ?
如果所有内容都在一个组中而不是一个文件夹中,则它应该找到其他视频并工作。
Use HTML5 ?
If everything is in a group and not a folder, it should find the other video and work.
为什么使用 HTML 文件来播放视频而不使用 MPMoviePlayerViewController?
这对你不起作用吗?
Why use an HTML file to play the video and not use MPMoviePlayerViewController?
Doesn't this work for you?
让本地内容显示在 UIWebView 中的一个技巧是使用替换缓存。 Matt Gallagher 在他的博客上提供了一些示例代码。
基本上,您可以为电影分配一个 URL,例如“http://www.example.com/movie.mp4”,您可以在 HTML 中使用该 URL 来引用该电影。加载电影时,替代缓存不会访问 URL,而是在本地获取数据。
AFAIK 这适用于所有 iOS 版本。
One trick to get local content to show up in
UIWebView
is to use a substitution cache. Matt Gallagher has some sample code up on his blog.Basically you assign an URL to the movie, say "http://www.example.com/movie.mp4", which you use in your HTML to refer to the movie. When the movie is loaded, instead of going to the URL, the substitution cache fetches the data locally.
AFAIK this works with all iOS versions.
我有类似的问题。名为 index.html 的 html 文件,位于名为 assets 的 Documents 目录的子文件夹中。
Documents/assets/index.html
请注意,它引用了 asset 目录中的其他文件。即,bundle.js 和 styles.css。
已接受答案的代码示例的以下变体允许 UIWebView 加载 index.html 并使其引用的 js 和 css 文件仍然有效:
I had a similar issue. An html file called index.html in a sub-folder of the Documents directory called assets.
Documents/assets/index.html
Note that it's referencing other files in the assets directory. Namely, bundle.js and styles.css.
The following variation on the accepted answer's code example allowed the UIWebView to load index.html and have the js and css files it referenced still work: