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.
Asked
Active
Viewed 6.8k times
5 Answers
164
-
1after call "player.stop"..did "player.start"...but it's not working ...any idea how to restart looping..? – CoDe Dec 30 '13 at 09:55
-
9once you call `stop()`, you have to call `prepare()/prepareAsync()` before calling `start()` again – aldrin Jan 24 '14 at 04:38
-
3How can i make it loop for 3 times only ?? – K Pradeep Kumar Reddy May 01 '20 at 13:39
10
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

Supriyanto
- 149
- 2
- 10
3
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);
}

Jeffy
- 276
- 3
- 9
3
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()

Marci
- 899
- 10
- 13
0
mediaPlayer.start()
for starting the mediaPlayer, mediaPlayer.pause()
for pausing the mediaPlayer. For looping the mediaPlayer for number of times then you can use loop:-
for(int i=1;i<=3;i++){
mediaPlayer.setLooping(true);
i++;
}

Art
- 2,836
- 4
- 17
- 34
-
1
-
to set the audio in loop according to user's choice, it's just an example not a perfect code. – Nov 13 '21 at 19:50