我正在构建一个混合网络应用程序和本机 iOS 应用程序,并希望显示已在本地设备上录制并存储在 NSDocumentDirectory 中以显示在 UIWebView 中的视频。
目前,根据以下代码,这似乎不起作用 - 该文件存在,但我认为从 HTML 中对文件的引用可能不正确。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *videoPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",fileName]];
BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:videoPath];
if(fileExist) {
NSLog(@"File Exist in path");
} else {
NSLog(@"NO File Exist");
}
videoPath = [videoPath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
videoPath = [videoPath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString *htmlString = [NSString stringWithFormat:@"<video width=\"320\" height=\"240\" controls><source src=\"%@\" type=\"video/mp4\"></video>", [NSString stringWithFormat:@"file://%@/",videoPath]];
NSString *js = [NSString stringWithFormat:@"document.getElementById('videoList').innerHTML = '%@'", htmlString];
NSString *result = [self.livingWebView stringByEvaluatingJavaScriptFromString:js];
NSLog(@"Result: %@", result);
The output of the JavaScript injection is:
Result: <video width="320" height="240" controls><source src="file:////var//mobile//Applications//76FE06FB-39D3-4658-8B52-AFE9DBE0C524//Documents//23-07-2013-16-54-48.mp4/" type="video/mp4"></video>
任何帮助表示赞赏。