How to play ringtone sound in Android with infinite loop?
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();
How to play an android notification sound
If anyone's still looking for a solution to this, I found an answer at How to play ringtone/alarm sound in Android
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
You can change TYPE_NOTIFICATION to TYPE_ALARM, but you'll want to keep track of your Ringtone r in order to stop playing it... say, when the user clicks a button or something.
Playing sound as Media/Alarm/Ringtone?
I have found a solution: not using the .create()
method. Instead use .setDataSource()
and .prepare()
. Code below:
public void ringAlarm() {
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
if (alarmUri == null) {
Log.e("ringAlarm" , "alarmUri null. Unable to get default sound URI");
return;
}
MediaPlayer mp = new MediaPlayer();
// This is what sets the media type as alarm
// Thus, the sound will be influenced by alarm volume
mp.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM).build());
try {
mp.setDataSource(getApplicationContext(), alarmUri);
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
// To continuously loop the alarm sound
mp.setLooping(true);
mp.start();
}
How to get Ringtone from system sounds
This worked for me :
Uri ringtone= RingtoneManager.getActualDefaultRingtoneUri(YourActivity.this, RingtoneManager.TYPE_ALARM);
And as stated by this answer, you have to do the following:
Intent intent=new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtone);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, ringtone);
startActivityForResult(intent , 1);
And finally you get the selected tone "uri" which you can store in ringtone
as shown below in onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
break;
default:
break;
}
}
}
Using ringtone and notification in an alarm receiver class
Here is a similar issue that shows how to use ringtone, vibration and lights through, notifications in a receiver
How can I enable vibration and lights using the Android notifications api?
MediaPlayer (or Ringtone) doesn't start playing when the screen is off (phone is locked)
I know what the problem was. I was stopping the MediaPlayer in onPause and it was called before the activity got visible (I don't know why).
2019-10-19 02:38:00.365 LOG:onReceive
2019-10-19 02:38:00.428 LOG:onCreate
2019-10-19 02:38:00.529 LOG:onResume
2019-10-19 02:38:00.542 LOG:onPause
2019-10-19 02:38:00.592 LOG:onStop
2019-10-19 02:38:00.716 LOG:onResume
Related Topics
How to Execute Async Task Repeatedly After Fixed Time Intervals
Firebase Analytics Custom Events Params
Call Activity Method from Adapter
How to Listen for Changes in Contact Database
Firebase Runtransaction Not Working - Mutabledata Is Null
Asynctask VS Thread in Android
Android: How to Get Accurate Altitude
Android Map V2 Zoom to Show All the Markers
Android: Access Child Views from a Listview
Example of Code to Implement a PDF Reader
How to Programmatically Answer/End a Call in Android 4.1
Use Picasso to Get a Callback with a Bitmap
Start an Activity with a Parameter
Updating Paging 3 Alpha to Stable Cause Indexing Issue Android
Broadcastreceiver Not Receiving Boot_Completed
How to Make an Android App Return to the Last Open Activity When Relaunched