70
votes

I am having 3 secs of mp3 file. I want to play that mp3 file continuously still the user click pause button. Is there any method to loop the single file and play it again again till user pauses it.

4

4 Answers

150
votes
mMediaPlayer.setLooping(true);
10
votes

This is working on my projects, place mediaPlayer.setLooping(true); after mediaPlayer.start();

public static void PlayAudio(Context c, int id){
        mediaPlayer = MediaPlayer.create(c, id);
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,50);
        if (!mediaPlayer.isPlaying())
        {
            isPlayingAudio = true;
            mediaPlayer.start();
            mediaPlayer.setLooping(true);
        }
    }

Happy Coding

3
votes

This is a working code that i used in my project

 if (Flags.notificationReceived) {
                        showAlert(Flags.patientModel);
                        Flags.notificationReceived = false;
                        mp.start();
                        mp.setLooping(true);
                        vibrate(2000);
                    }
1
votes

This one works for me. (written in Kotlin)

 val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
 var mp = MediaPlayer.create(context, uri)
 mp.isLooping = true
 mp.start()

For stop, you should call your stop() function:

mp.stop()