1

I'm using a Webview, playing different html5 videos (internal tag and Youtube videos).

When closing the Webview I tried loading a blank page + hiding the view + pausing the webview. The Webview seems to be closed correctly and the video stops playing.

The problem is on Galaxy S4 + S2, when opening the Webview again and playing another video. The new video is been paused immediately after being played. When I press "play" again, the video is automatically paused again. This will happen with every video that I'll try to load (Youtube and internal ), until I'll kill my app. After killing my app everything will work normally again. Is it possible that the first view/video is not terminated so each new video will automatically be paused, in order to prevent the two to play at the same time?

Tnx!

Yaniv

Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96
  • 1
    Have you used JavaScript to poke at the – Matt Dec 09 '13 at 20:35
  • Thanks for your answer. It is a problem to pause the video via javascript for two reasons: a. When the use presses "home" or "back", the javascript is no longer "alive" (the view is closed). I already tried it with no luck (unless I am missing something). b. There are external iFrame players which I does not control and cannot pause. – Yaniv Efraim Dec 09 '13 at 21:08
  • Edit: Matt's answer partially solved my problem. I was able to catch the "backPress" event and load a javascript code which paused the video. With external iFrame players I simply emptied the container's content on this event. I could still find situations which it happens but it is much less frequent now. I would love to get a better solution!!! – Yaniv Efraim Dec 11 '13 at 11:56

1 Answers1

4

After struggling with this issue for few days, I found the solution. It appears that when closing the Webview, the Mediaplay probably did not have focus, which prevented him from pausing/dismissing. This prevented all other Webviews with Mediaplayers from playing (automatically paused each time trying to play, with no ability to resume).

When adding this code:

@Override
protected void onPause() {
    super.onPause();

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

The problem was completely solved. The Mediaplayer was dismissed/paused (not sure which, have to check), and a new Webview with a new Mediaplayer can gain focus and play normally.

Hope it could help other people struggling with this issue..

Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96
  • does this apply, even if the video is muted or no audio is present in the mp4? – Someone Somewhere Jan 17 '14 at 22:48
  • not sure, I did not tried muting the mp4. I guess it has nothing to do with the sound of the video – Yaniv Efraim Jan 22 '14 at 00:38
  • 4
    I made a discovery today... whilst I have Pandora playing in the background: launch my app that creates a webview with a *muted* HTML5 video. Result: pandora's volume is reduced by 50%. This isn't super bad but I wish it didn't happen. However, once the user navigates away from the webview and the onPause() code above is used, Pandora's music stream is no longer audible ! (NOTE: this is on Android 4.4.2 on Nexus 4) – Someone Somewhere Mar 20 '14 at 21:58
  • If this code gets executed. It will make whatever background music player to pause. – Ads Oct 21 '15 at 20:26