Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 817d6d5e authored by Ben Murdoch's avatar Ben Murdoch
Browse files

Check MediaPlayer state, do not teardown() from UI thread.

The teardown() function should only be called from the native
thread when WebCore is happy for us to release the media player.

When we lose audio focus, simply stop playback. Also be careful
not to check the playing state of the MediaPlayer if we are in
the error state.

Bug: 5461143

Change-Id: Iba03bdad5c39b104b3071129430d7ef2177e9358
parent 9ad680ca
Loading
Loading
Loading
Loading
+19 −14
Original line number Original line Diff line number Diff line
@@ -238,24 +238,27 @@ class HTML5Audio extends Handler
        switch (focusChange) {
        switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
        case AudioManager.AUDIOFOCUS_GAIN:
            // resume playback
            // resume playback
            if (mMediaPlayer == null) resetMediaPlayer();
            if (mMediaPlayer == null) {
            else if (!mMediaPlayer.isPlaying()) mMediaPlayer.start();
                resetMediaPlayer();
            } else if (mState != ERROR && !mMediaPlayer.isPlaying()) {
                mMediaPlayer.start();
                mState = STARTED;
                mState = STARTED;
            }
            break;
            break;


        case AudioManager.AUDIOFOCUS_LOSS:
        case AudioManager.AUDIOFOCUS_LOSS:
            // Lost focus for an unbounded amount of time: stop playback and release media player
            // Lost focus for an unbounded amount of time: stop playback.
            if (mMediaPlayer.isPlaying()) mMediaPlayer.stop();
            if (mState != ERROR && mMediaPlayer.isPlaying()) {
            mMediaPlayer.release();
                mMediaPlayer.stop();
            mMediaPlayer = null;
                mState = STOPPED;
            }
            break;
            break;


        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            // Lost focus for a short time, but we have to stop
            // Lost focus for a short time, but we have to stop
            // playback. We don't release the media player because playback
            // playback.
            // is likely to resume
            if (mState != ERROR && mMediaPlayer.isPlaying()) pause();
            if (mMediaPlayer.isPlaying()) mMediaPlayer.pause();
            break;
            break;
        }
        }
    }
    }
@@ -273,10 +276,7 @@ class HTML5Audio extends Handler
            int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
            int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
                AudioManager.AUDIOFOCUS_GAIN);


            if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // could not get audio focus.
                teardown();
            } else {
                mMediaPlayer.start();
                mMediaPlayer.start();
                mState = STARTED;
                mState = STARTED;
            }
            }
@@ -299,8 +299,13 @@ class HTML5Audio extends Handler
        }
        }
    }
    }


    /**
     * Called only over JNI when WebKit is happy to
     * destroy the media player.
     */
    private void teardown() {
    private void teardown() {
        mMediaPlayer.release();
        mMediaPlayer.release();
        mMediaPlayer = null;
        mState = ERROR;
        mState = ERROR;
        mNativePointer = 0;
        mNativePointer = 0;
    }
    }