iphone - 将视频嵌入到从应用程序的文档文件夹加载到 UIWebView 中的 html 文件中

标签 iphone ipad uiwebview movie embedded-video

我有一个名为 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];

因为我必须在我的项目中实现它,所以没有其他方法可以帮我做。

最佳答案

经过大量的努力和研发,我发现,

Simulator 3.2iOS 4.3.3 设备 版本下,如果 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];

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

仍然在 Simulator 4.2 下它不起作用。它显示黑色部分而不是视频。然而无法找到 Simulator 4.2 的解决方案,但它可以在设备上运行,所以我猜代码绝对没问题。

将此作为答案发布,这样如果有人做同样的事情可能会有用。

关于iphone - 将视频嵌入到从应用程序的文档文件夹加载到 UIWebView 中的 html 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6759818/

相关文章:

iPhone:CoreData,按 2 列排序

ios - 在场景之间传递数据 (SpriteKit)

ios - UIDocumentInteractionController - 读取文档时出错

html - Ipad 上带有文本的 css 过渡

ios - UITableViewController 中的 UISearchController - 行不可选择且 TableView 显示为灰色

ios - Swift:检测 UIWebView 中的当前 URL

iphone - CFBundleLocalizations info.plist - 如何放置多种语言

iphone - 无法在 iOS 6 上使用 Opera Mini 的本地代理

objective-c - 根据内容在 UITableViewCell 中调整 UIWebView 的大小

iOS - SDK - 如何在 UIWebView 中打开大型主题文件?