After struggling for over a day on something similar, I have found that you can crash the MediaPlayer
inside a WebView
by sending the WebView
to a page with a HTML5 video tag with a bad video url. The example below is a main activity with a valid video tag. If you press play, the MediaPlayer
will take over and start running forever... BUT when you click back or home and leave the app, the onPause()
is triggered and the WebView
is redirected to a page with a bad video url and told to autoplay. Attempting to play crashes the MediaPlayer
and voila! The MediaPlayer
is no more.
package com.test.webviewtest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
String html = "<video width=\"320\" height=\"240\" controls>" +
"<source src=\"http://www.w3schools.com/html/movie.mp4\" " +
"type=\"video/mp4\"></video>";
webView.loadData(html, "text/html", null);
setContentView(webView);
}
@Override
protected void onPause(){
super.onPause();
// attempt to kill the MediaPlayer here...
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
String js = "javascript:document.getElementsByTagName('video')[0].play();";
webView.loadUrl(js);
}
});
String html = "<video width=\"320\" height=\"240\" controls>" +
"<source src=\"http://www.w3schools.com/html/" +
"NOT_A_MOVIE.mp4" +
"\" type=\"video/mp4\"></video>";
webView.loadData(html, "text/html", null);
}
}
Note the logs:
02-26 13:19:13.010 220-25370/? V/MediaPlayerService﹕ [607] notify (0x43a3aeb8, 100, 1, -1004)
02-26 13:19:13.010 25262-25274/com.test.webviewtest V/MediaPlayer﹕ message received msg=100, ext1=1, ext2=-1004
02-26 13:19:13.010 25262-25274/com.test.webviewtest E/MediaPlayer﹕ error (1, -1004)
02-26 13:19:13.010 25262-25274/com.test.webviewtest V/MediaPlayer﹕ callback application
02-26 13:19:13.020 25262-25274/com.test.webviewtest V/MediaPlayer﹕ back from callback
02-26 13:19:13.020 25262-25262/com.test.webviewtest E/MediaPlayer﹕ Error (1,-1004)
And then the blissful silence of no more MediaPlayer
logs...
See Android WebView HTML5 Video Spawns MediaPlayer that lives forever on Samsung S4 [Hacked answer found] for more discussion.