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

Commit 0168bef1 authored by Ben Murdoch's avatar Ben Murdoch
Browse files

Fix looping HTML5 Audio.

When an <audio> tag has the loop=true attribute,
Webkit tells us to seek back to 0 before we are supposed
to have stopped the stream. But by the time that the message
gets back to the Android MediaPlayer java side, it's already
stopped. So after seeking, we play() again if the player is in
the COMPLETED state. Change the code to do this and handle
the case that we call play on a COMPLETED stream
(resetting internal state, etc).

Note that this has the side effect that we will start playing
the stream after any seek on a COMPLETED stream - e.g. dragging
the slider thumb on the progress track after the stream is
finished.

Bug: 5461143
Change-Id: I6cf4d46d9a1985caf9f9ab85dbcf65535c8dcd77
parent 2eb73093
Loading
Loading
Loading
Loading
+19 −3
Original line number Diff line number Diff line
@@ -142,8 +142,7 @@ class HTML5Audio extends Handler

    // MediaPlayer.OnCompletionListener;
    public void onCompletion(MediaPlayer mp) {
        resetMediaPlayer();
        mState = IDLE;
        mState = COMPLETE;
        nativeOnEnded(mNativePointer);
    }

@@ -265,7 +264,18 @@ class HTML5Audio extends Handler


    private void play() {
        if ((mState >= ERROR && mState < PREPARED) && mUrl != null) {
        if (mState == COMPLETE) {
            // Play it again, Sam
            mTimer.cancel();
            mTimer = new Timer();
            mAskToPlay = true;
            mMediaPlayer.stop();
            mState = STOPPED;
            mMediaPlayer.prepareAsync();
            return;
        }

        if (((mState >= ERROR && mState < PREPARED)) && mUrl != null) {
            resetMediaPlayer();
            setDataSource(mUrl);
            mAskToPlay = true;
@@ -296,6 +306,12 @@ class HTML5Audio extends Handler
    private void seek(int msec) {
        if (mState >= PREPARED) {
            mMediaPlayer.seekTo(msec);
            if (mState == COMPLETE) {
                // Seeking after the stream had completed will
                // cause us to start playing again. This is to
                // support audio tags that specify loop=true.
                play();
            }
        }
    }