Video playing automatically in UIWebView

2020-03-24 08:34发布

问题:

Hi I have a webview and when i tried to load the video files in that , it starts playing the video automatically without user intervention. And also the play button is still displayed in webview (video is playing behind that). I am using the following code to load the URL

NSURL*  url = [NSURL fileURLWithPath:filePath];
NSURLRequest*   request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

Can you tell me how to disable the play button OR the Video should start playing only after the user taps the play button ??

回答1:

- (void)videoThumb:(NSString *)urlString frame:(CGRect)frame {
          NSString *embedHTML = @"\
          <html><head>\
          <style type=\"text/css\">\
          body {\
          background-color: transparent;
          color: white;
          }\
          </style>\
          </head><body style=\"margin:0\">\
          <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
          width=\"%0.0f\" height=\"%0.0f\"></embed>\
          </body></html>";


  NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width,frame.size.height];

  [webView loadHTMLString:html baseURL:nil];
  [self.view addSubview:webView];
  [webView release];
}

//Include above function to your page and call it by passing the urlstring
[self videoThumb:url:frame];


//frame : this parameter needs the size of the thumb you want to use.

I hope this may help you and better way for your purpose.