Playing default ringtone
Asked Answered
I

1

10

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

Indamine answered 1/11, 2010 at 2:5 Comment(0)
T
27

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.

Tannen answered 1/11, 2010 at 2:38 Comment(3)
Thanks -- this is exactly what I needed. Simple, and it did the trick nicelyIndamine
Nice answer. Just a FYI you dont need to call prepare() in case of using Factory Method create().Lynden
You are correct IronBlossom. Code has been edited to remove prepare().Tannen

© 2022 - 2024 — McMap. All rights reserved.