1

I want to loop a track using MediaPlayer. I have set the MediaPlayer object to loop. The problem is the track finishes and goes into next loop, there is a noticeable jitter/lag. How can I avoid that? I currently use the code mentioned in this link. I use the following code to set up my MediaPlayer instance.

private void setupMPLesson() {
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    mMPCurrPlayer = new MediaPlayer();
    mMPCurrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mMPCurrPlayer.setDataSource(this, Uri.parse("android.resource://" + RESOURCES_PACKAGE +
                "/" + R.raw.sweep_up_down_wav));
        mMPCurrPlayer.prepare();
    } catch (IOException e) {
        Log.e(TAG, "Could not find the file:", e);
    }

    // Create the media player instance for the next iteration ...
    createNextMediaPlayer();

}

The method createNextMediaPlayer() is as follows:

private void createNextMediaPlayer() {
        mNextPlayer = new MediaPlayer();
        mNextPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mNextPlayer.setDataSource(this, Uri.parse("android.resource://" + RESOURCES_PACKAGE +
                    "/" + R.raw.sweep_up_down_wav));
        } catch (IOException e) {
            Log.e(TAG,"Could not find the file:", e);
        }

        mNextPlayer.setOnPreparedListener(this);
        mNextPlayer.prepareAsync();

        // setting the completion listener to the mMPCurrPlayer
        mMPCurrPlayer.setOnCompletionListener(this);
    } 

Once the mNextPlayer is prepared, I do the following:

@Override
    public void onPrepared(MediaPlayer mp) {
        mMPCurrPlayer.setNextMediaPlayer(mNextPlayer);
    }

And on completion of a cloop, I implement the OnCompletionListener as:

@Override
    public void onCompletion(MediaPlayer mp) {
        // Check if the mp completed playing the number of loops required ..
        // If the current loop is smaller than the number of set loops ..

        mCompletedLoops++;

        mp.release();

        // Assign the current mMPCurrPlayer to the created mNextPlayer for looping ..
        mMPCurrPlayer = mNextPlayer;
        // Create the next media player and make it ready for the next iteration ..
        createNextMediaPlayer();

        if (mCompletedLoops < mNumLoops) {
            return;
        }

        // stop the media player ..
        mMPCurrPlayer.pause();
}

I also keep the track of the current loop and stop once I have crossed the required number of loops.

Community
  • 1
  • 1
Swapnil
  • 1,870
  • 2
  • 23
  • 48
  • Is this audio only? or video? Please edit your question and add your current code that preps, starts, the tracks to be played. – petey Dec 09 '15 at 17:25
  • It is only audio. I think the problem is that the buffer size used by the MediaPlayer is quite big in size and at the end of one cycle it skips the playback of the last buffer (because it is smaller than size of the buffer size it needs) and that is why I am having this problem. – Swapnil Dec 15 '15 at 21:30
  • For the purpose of looping, I was earlier setting the `MediaPlayer` object to loop by calling `setLooping(true)` on that that object. But, this has noticeable lag at the end while switching into the new cycle. Then I used the solution mentioned [here](http://stackoverflow.com/questions/26274182/cant-able-to-achieve-gapless-audio-looping-so-far-on-android). This is able to loop seamlessly but I still have the problem with the last buffer, due to which I have an observable glitch with the accompanying visualization. – Swapnil Dec 15 '15 at 21:50
  • Can you please edit your question and add your current code that preps, starts, the mediaplayer & the track to be played – petey Dec 16 '15 at 18:30
  • Updated the question with the code, I hope this helps. Btw, I came across another android class known as `AudioTrack` and now I use that for looping. Seem to work quite well for me. – Swapnil Dec 17 '15 at 14:02

0 Answers0