How to play ringtone sound in Android with infinite loop?
Asked Answered
B

2

8

i want to play the ringtone selected in the settings of the device, but in loop mode.

here you can see how to play it only once: How to play ringtone/alarm sound in Android

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();

I need to make it play in loop mode, but i don't know how to do it...

thanks

Bihari answered 14/12, 2014 at 19:38 Comment(0)
M
15

It seems the simplest way is to create a MediaPlayer from the Uri returned from RingtoneManager, and set it to loop.

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer player = MediaPlayer.create(this, notification);
player.setLooping(true);
player.start();
Mchenry answered 14/12, 2014 at 19:51 Comment(2)
Works fine, but how to stop that loop then ?Agrostology
Call player.stop().Arana
K
2

By using setLooping(true) you can achieve this:

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mMediaPlayer.setDataSource(this, alert);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
Kalmia answered 12/11, 2019 at 6:25 Comment(1)
Please also provide some explanation for your answer to show how it solves the problem.Swinney

© 2022 - 2024 — McMap. All rights reserved.