Android WebView HTML5 Video Spawns MediaPlayer that lives on Samsung S4 forever [hacked answer found]

From what I was able to find out, this is apparently characteristic of the latest Samsung devices. S4 will do it. Nexus 7 will not.

If a WebView with WebChromeClient starts playing HTML5 video, it creates an instance of MediaPlayer. Once the video has finished, there seems to be no way to kill MediaPlayer, which is smaller than System.exit (0).

This is all my MainActivity.java

package com.test.webviewtest; import android.app.Activity; import android.os.Bundle; import android.webkit.WebChromeClient; import android.webkit.WebView; public class MainActivity extends Activity { WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); webView = new WebView(this); webView.setWebChromeClient(new WebChromeClient()); String html = "<video width=\"320\" height=\"240\" controls autoplay>" + "<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... } } 

And naturally, this is required in the manifest

 <uses-permission android:name="android.permission.INTERNET"/> 

As soon as you click a video, logcat will start splashing out MediaPlayer messages and never stop. There are some useful videos during playback, but after that it just records

 02-25 17:13:19.395 220-8584/? V/MediaPlayerService﹕ [470] notify (0x51b7aa00, 3, 100, 0) 02-25 17:13:19.395 8532-8610/? V/MediaPlayer﹕ message received msg=3, ext1=100, ext2=0 02-25 17:13:19.395 8532-8610/? V/MediaPlayer﹕ buffering 100 02-25 17:13:19.395 8532-8610/? V/MediaPlayer﹕ callback application 02-25 17:13:19.405 8532-8610/? V/MediaPlayer﹕ back from callback 02-25 17:13:20.406 220-8584/? V/MediaPlayerService﹕ [470] notify (0x51b7aa00, 3, 100, 0) 02-25 17:13:20.406 8532-8544/? V/MediaPlayer﹕ message received msg=3, ext1=100, ext2=0 02-25 17:13:20.406 8532-8544/? V/MediaPlayer﹕ buffering 100 02-25 17:13:20.406 8532-8544/? V/MediaPlayer﹕ callback application 02-25 17:13:20.406 8532-8544/? V/MediaPlayer﹕ back from callback 02-25 17:13:21.407 220-8584/? V/MediaPlayerService﹕ [470] notify (0x51b7aa00, 3, 100, 0) 02-25 17:13:21.407 8532-8611/? V/MediaPlayer﹕ message received msg=3, ext1=100, ext2=0 02-25 17:13:21.407 8532-8611/? V/MediaPlayer﹕ buffering 100 02-25 17:13:21.407 8532-8611/? V/MediaPlayer﹕ callback application 02-25 17:13:21.417 8532-8611/? V/MediaPlayer﹕ back from callback 

And this repetition never ends until you close or delete or something else is decided. I already tried:

  • webView.onPause(); This pauses the video, so it stops playing in the background, but does not kill MediaPlayer.
  • webView.destroy(); Still does not stop this MediaPlayer zombie
  • webView = null; nope
  • webView = new WebView(this); still not working
  • finish();
  • I also tried various ways to capture WebChromeClient and destroy it, but nothing works.

I will update this list when I try more things. Appreciate the help.

  • I also tried redirecting webView to non-video content with webView.loadData("hi", "text/html", null); still alive ...

CUT OFF ANSWER

Well, this is the best I can find. If anyone has a better solution, please let me know.

To stop MediaPlayer , I told him to go to a page with a <video> tag that pointed to something other than the video.

 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); 

When I do this, I see the video player on the screen, and when I press the play button, it records

 02-26 12:26:37.112 220-11354/? V/MediaPlayerService﹕ [576] notify (0x47fa92b8, 100, 1, -1004) 02-26 12:26:37.112 11264-11325/com.test.webviewtest V/MediaPlayer﹕ message received msg=100, ext1=1, ext2=-1004 02-26 12:26:37.112 11264-11325/com.test.webviewtest E/MediaPlayer﹕ error (1, -1004) 02-26 12:26:37.112 11264-11325/com.test.webviewtest V/MediaPlayer﹕ callback application 02-26 12:26:37.112 11264-11325/com.test.webviewtest V/MediaPlayer﹕ back from callback 02-26 12:26:37.122 11264-11264/com.test.webviewtest E/MediaPlayer﹕ Error (1,-1004) 

From now on, MediaPlayer seems mostly dead (only slightly alive). He stops all registration work, which is the desired result. The only consequence of this is that you force the application to close, you will still see it.

 02-26 12:29:35.826 220-738/? V/MediaPlayerService﹕ Client(576) destructor pid = 11264 02-26 12:29:35.836 220-738/? V/MediaPlayerService﹕ disconnect(576) from pid 11264 02-26 12:29:35.836 220-738/? W/MediaPlayerService﹕ native_window_api_disconnect returned an error: Broken pipe (-32) 

I'm fine, but a real solution would be nice. I will obviously connect a bad video with autoplay, so a crash can happen in the background, but this is a framework.


UPDATE 2-27-14 Although this fixes the problem on Samsung devices, it breaks other devices, so this is not a good solution. On other devices (Nexus 7 tested), this code will make WebView useless for any future calls. I will update this when I find more information. If you don’t need to display another WebView for the rest of the application’s lifespan, I think that’s fine, but I need it to be able to load another page later.

+6
source share
2 answers

There was no other solution for me except:

 android.os.Process.killProcess(android.os.Process.myPid()); 

This is because you cannot actually create activity from within, but I overcame this with the second process.

+1
source
 ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).requestAudioFocus( new AudioManager.OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int focusChange) { } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); 
-1
source

All Articles