播放默认铃声

10

我一直试图使用SoundPool播放默认铃声,但没有成功。在下面的代码中:

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);

我收到了“error loading content /system/ringtone sample 0 not READY”的错误消息。将URI替换为SD卡上现有mp3文件的硬路径会产生类似的结果。

我做错了什么?谢谢,

kyle

1个回答

27

你可能不想在这种类型的音频播放中使用SoundPool。SoundPool通常用于播放非常小的音频片段,存储为本地文件,甚至比大多数铃声还要小。你应该考虑使用MediaPlayer。以下代码应该非常适合:

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

如果您的应用程序没有访问该铃声的权限,那么可能会发生FileNotFoundException。


谢谢 - 这正是我所需要的。简单而且非常有效。 - Kyle Banerjee
好的回答。顺便提一下,如果使用工厂方法create(),则不需要调用prepare()。 - IronBlossom
你是对的,IronBlossom。代码已经被编辑以移除prepare()函数。 - Dave MacLean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接