Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Embed a Local Video File Into Html to play in UIWebView?

I can play video files in WebView , but the videos are playing automatically without the user interaction. So now I am following different approach to achieve that.

    NSString* embedHTML = @"\
        <html><head>\
        <style type=\"text/css\">\
        body {\
        background-color: transparent;\
        color: white;\
        }\
        </style>\
        </head><body style=\"margin:0\">\
        <embed id=\"yt\" src=\"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4\" type=\"application/x-shockwave-mp4\" \
        width=\"%0.0f\" height=\"%0.0f\"></embed>\
        </body></html>";


 _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 412.0)];

    [_webView setOpaque:NO];
    NSString *html = [NSString stringWithFormat:embedHTML, _webView.frame.size.width, _webView.frame.size.height];
    [_webView loadHTMLString:html baseURL:nil];

This way , I can play video file from the internet. But I want to play the files from the Local memory.

Even if tried to pass the local file path variable, the video is unable to play. Can anyone tell me how to embed my local file into the html so that I can play in webview as like the internet video ?

CASE 1 : Video automatically playing in WebView but play button is still in top. CASE 2 : Online Video URL is embedded as shown above and the video can be played. But I want to pass the local video file path instead of online video url

like image 863
Perseus Avatar asked Oct 22 '22 21:10

Perseus


2 Answers

you can't run files from local memory if you are using UIWebView. If you want your video to not play automatically, you have to enter code into your HTML file (I'm assuming the HTML files you are displaying are your own) to disable auto play for the video.

If you are using object, here is a sample code for you to follow

<object width="160" height="144">
<param name="autoplay" value="false">

<embed src="sample.mov" width="160" height="144"
autoplay="false" controller="false"
</embed>

</object> 
like image 159
Yang Jie Domodomo Avatar answered Nov 01 '22 11:11

Yang Jie Domodomo


if you added .mp4 or compatible other videos to your application's bundle, you can reach its path like this;

[[NSBundle mainBundle] pathForResource:@"blalba" ofType:@"mp4"];

it gives you the url string.

If your videos are not in your bundle, either copied from the bundle or downloaded from the internet you can get the directory like this;

NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *tileDirectory = [documentdir stringByAppendingPathComponent:@"xxxx/Tiles"];
NSLog(@"Tile Directory: %@", tileDirectory);

After getting the url of video, you can put it to your WebView. But it will play in iphone's default Video Player i think.. good luck..

like image 33
relower Avatar answered Nov 01 '22 09:11

relower