Rishabh Tayal
Rishabh Tayal

Reputation: 496

UIWebView load local video data

I am trying to load a Video in UIWebView. Video is stored in local file system. Here is the code snippet I am using for loading the video.

{
   NSString* filePAth = [[NSBundle mainBundle] pathForResource:@"sample_iPod" ofType:@"m4v"];
   NSLog(@"%@",filePAth);
   NSData* data = [NSData dataWithContentsOfFile:filePAth];
   [_webView loadData:data MIMEType:@"video/x-m4v" textEncodingName:@"UTF-8" baseURL:nil];
}

This is the error I get in console:

Error in Webview loading: Error Domain=WebKitErrorDomain Code=204 "Plug-in handled load" UserInfo=0x1f8b3c70 {NSErrorFailingURLStringKey=applewebdata://3F0047AD-E6CF-4EAD-A4C3-DA6E1C0BD603, WebKitErrorMIMETypeKey=video/x-m4v, NSErrorFailingURLKey=applewebdata://3F0047AD-E6CF-4EAD-A4C3-DA6E1C0BD603, NSLocalizedDescription=Plug-in handled load}

However, The same code works for loading all other file type for eg: PNG, DOC et.c.

Please help...!!!

Upvotes: 6

Views: 3125

Answers (2)

malex
malex

Reputation: 10096

You need to convert your data to base64 string and use HTML capability of Safari with video tag

NSString *base64String = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

NSString *str = [NSString stringWithFormat:@" \
                     <div style=\"height:100%%;\"> \
                     <p style=\"position: absolute; top: 50%%; left: 50%%; transform: translate(-50%%, -50%%)\"> \
                     <video controls> \
                     <source type=\"%@\" src=\"data:%@;base64,%@\"> \
                     </video></p></div>", contentType, contentType, base64String];

[webView loadHTMLString:str baseURL:nil];

Here data is your raw video data, contentType is contentType string.

Upvotes: 1

Daniel
Daniel

Reputation: 23359

You should load local videos and other videos whenever possible with MPMoviePlayerController - (Documentation) - If playing the video is all you need and want and you are developing a native app.

Upvotes: 5

Related Questions