2010-11-01 229 views
6

我一直在尝试使用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); 

我收到消息“错误加载内容/系统/铃声样本0不准备”。将硬盘路径的URI替换为SD卡上现有的mp3文件会得到相似的结果。

我在做什么错?谢谢,

凯尔

回答

18

你可能不希望使用的Soundpool这种类型的音频播放是。 SoundPool通常用于播放非常小的音频片段,存储为本地文件,甚至比大多数铃声还小。您应该考虑MediaPlayer。下面应该非常漂亮的工作:

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

但如果你没有权限从应用程序访问的铃声,你可以得到一个FileNotFoundException异常。

+0

谢谢 - 这正是我所需要的。简单,它很好地诀窍 – 2010-11-01 04:54:26

+0

好的答案。只是一个FYI,如果使用Factory Method create(),你不需要调用prepare()。 – IronBlossom 2012-10-20 11:35:20

+0

你是对的IronBlossom。代码已被编辑以删除prepare()。 – 2012-10-23 01:36:33