Playing default ringtone

Kyle Banerjee picture Kyle Banerjee · Nov 1, 2010 · Viewed 15k times · Source

I've been trying to use SoundPool to play the default ringtone without success. In the code below

String ringtone = Settings.System.DEFAULT_RINGTONE_URI.getPath();
SoundPool ringPhone = new SoundPool(2, AudioManager.STREAM_RING, 1);
int soundID = ringPhone.load(Settings.System.DEFAULT_RINGTONE_URI.getPath(), 1);
int soundID = ringPhone.load(ringtone, 1);
ringPhone.play(soundID, 0.99f, 0.99f, 1, 0, 1);

I get the message "error loading content /system/ringtone sample 0 not READY". Replacing the URI with a hard path to an existing mp3 file on the sd card yields similar results.

What am I doing wrong? Thanks,

kyle

Answer

Dave MacLean picture Dave MacLean · Nov 1, 2010

You probably don't want to be using the SoundPool for this type of audio playing. SoundPool is usually used to play very small snippets of audio, stored as local files, even smaller than most ringtones. You should consider MediaPlayer instead. The following ought to work very nicely:

MediaPlayer player = MediaPlayer.create(this,
    Settings.System.DEFAULT_RINGTONE_URI);
player.start();

Although if you don't have permission to access that ringtone from your application, you could get a FileNotFoundException.