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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With