Manuel
Manuel

Reputation: 127

Android: Prevent a song from looping in MediaPlayer. OnCompletionListener not firing

I'm building an application that among other things plays some audio files. Here is my code for doing so:

public void reproducirAudioSelect() {

        String audioPath = directorio1 + File.separator + getItemSeleccionado();
        try {
           // mediaplayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
            mediaplayer = new MediaPlayer();

            mediaplayer.setDataSource(audioPath);
            mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaplayer.setLooping(false);
            mediaplayer.prepare();
            mediaplayer.start();
            mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaplayer) {
                    Log.i("A", "onComplete hit");
                    mediaplayer.stop();
                    mediaplayer.release();
                }
            });

        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }

    }

There is no problem when I just try to play whichever song I choose. However, the song is played again and again. I just want them to be played once, and when the song is finished, stop. (Hence the use of the setOnCompletionListener(..)).

I've tried many different solutions to this problem. The most popular (https://stackoverflow.com/a/19555480/13127574) consists of placing the listener after the .start(). But it doesn't work for me. I can't see anything wrong in my code, after debugging. Simply, onCompletition is not triggered.

Logcat if that's of any help:

2020-12-28 13:14:57.677 3662-3662/com.example.a_2_b_a19manuelgp W/MediaPlayer: Use of stream types is deprecated for operations other than volume control 2020-12-28 13:14:57.677 3662-3662/com.example.a_2_b_a19manuelgp W/MediaPlayer: See the documentation of setAudioStreamType() for what to use instead with android.media.AudioAttributes to qualify your playback use case 2020-12-28 13:15:03.057 3662-3662/com.example.a_2_b_a19manuelgp W/2_b_a19manuelg: Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist, linking) 2020-12-28 13:15:03.553 3662-3707/com.example.a_2_b_a19manuelgp D/EGL_emulation: eglMakeCurrent: 0xefcc3580: ver 2 0 (tinfo 0xefc31ca0) 2020-12-28 13:15:03.562 3662-3662/com.example.a_2_b_a19manuelgp V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false 2020-12-28 13:15:03.563 3662-3662/com.example.a_2_b_a19manuelgp V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null

Upvotes: 0

Views: 108

Answers (1)

CSmith
CSmith

Reputation: 13458

I think you want to use prepareAsync() along with setOnPreparedListener(), and set your listeners before calling prepareAsync, something like this:

String audioPath = directorio1 + File.separator + getItemSeleccionado();
try 
{
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(audioPath);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
    {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer)
        {
            mediaPlayer.start();
        }
    };
            
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
    {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) 
        {
            Log.i("A", "onComplete hit");
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    });

    mediaPlayer.setLooping (false);     
    mediaPlayer.prepareAsync();
    
} 
catch (Exception e) 
{
    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}

Upvotes: 1

Related Questions