6

In my attempts to create a WebView that plays YouTube videos via HTML5 (and not via Flash), I tried to implement this article verbatim, right inside my activity's onCreate():

  WebView webView = (WebView) findViewById(R.id.embeddedWebView);
  webView.setDownloadListener(new DownloadListener()
  {
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long size)
    {
      Log.v("TAG", "url: " + url + ", mimeType: " + mimeType);

      Intent viewIntent = new Intent(Intent.ACTION_VIEW);
      viewIntent.setDataAndType(Uri.parse(url), mimeType);

      try
      {
        startActivity(viewIntent);
      }
      catch (ActivityNotFoundException ex)
      {
        Log.w("YourLogTag", "Couldn't find activity to view mimetype: " + mimeType);
      }
    }
  });  

It didn't get called for some reason, so noticing that nowhere in my code do I specify "implements DownloadListener", I re-implemented it as a separate class that's defined as

public class MyDownloadListener implements DownloadListener 

and implements onDownloadStart() as above (passing the activity as a parameter so that it can call startActivity(). Then in onCreate(), I simply do:

mDownloadListener = new MyDownloadListener(this);
mWebView.setDownloadListener(mDownloadListener);

I tried again on YouTube and on http://broken-links.com/tests/video/ and I still don't see in LogCat any trace that onDownloadStart() is ever being called.

What do I need to do to have it called? What am I missing?

uTubeFan
  • 6,664
  • 12
  • 41
  • 65
  • I found another post describing a similar problem http://stackoverflow.com/questions/3926629/downloadlistener-not-working but his "solution" was to declare DownloadListener not working and use onLoadResource as a workaround. I would like to see a solution that makes DownloadListener work. – uTubeFan Apr 27 '11 at 00:31
  • add following code will fix issue `WebSettings settings = webView.getSettings(); settings.setLoadsImagesAutomatically(true); ` – Mohd Qasim Jan 12 '21 at 13:33

1 Answers1

6

setDownloadListener sets the listener when the WebView doesn't think the content can be handled by the rendering engine.

Register the interface to be used when content can not be handled by the rendering engine, and should be downloaded instead. This will replace the current handler.

The webview uses the WebKit rendering engine and I believe that can (or thinks it can) handle html5 so that listener will not be called.

jkhouw1
  • 7,320
  • 3
  • 32
  • 24
  • 1
    Thanks (+1) for this educating answer. Is there a programmatic way to verify this by disabling **only** HTML5 support, **without** disabling JavaScript? – uTubeFan Apr 27 '11 at 13:39
  • As of now, this very sensible answer cannot be positively verified for videos because I haven't found a way to disable HTML5 videos without disabling Javascript. So I must accept despite being unable to verify this. Thanks. – uTubeFan May 02 '11 at 18:39