5

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

Perseus
  • 1,546
  • 4
  • 30
  • 55
  • Please have a look at my [answer][1] here. Hope it helps. [1]:http://stackoverflow.com/questions/6759818/embed-video-in-html-file-loaded-in-uiwebview-from-documents-folder-of-applicatio/6964742#6964742 – Janak Nirmal May 31 '12 at 09:57

2 Answers2

2

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> 
Yang Jie Domodomo
  • 685
  • 1
  • 8
  • 25
  • No. You have mistaken me. First I tried with only the WebView which is playing the video automatically. Then I planned to use the Html – Perseus May 30 '12 at 10:04
  • I want to play the videos in the WebView and that too the videos should start playing after the user clicks the play button. – Perseus May 30 '12 at 10:05
  • if the URL/html file you're running on UIWebView isn't yours, then you have add in code in the viewDidLoad section to disable autoplay. If you want to play local files, you have to make your own HTML file and configure the setting internally. Not too sure by what you meant with "planned to use HTML" since your UIWebView should be using a html file(recommended) if you are planning to display vid on a webpage. – Yang Jie Domodomo May 31 '12 at 01:48
  • Actually the mp4 videos are playing in the WebView, but the thing is they are playing automatically. I created a project which has webview with embed html and the link passed in the embedHtml is internet link. That works great. But if I pass the local file NSUrl , then it is showing crossed play symbol. Anyway I am fed up of resolving this issue, I am using MPMoviePlayerController. – Perseus May 31 '12 at 12:23
1

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..

relower
  • 1,293
  • 1
  • 10
  • 20