通过SoundPool播放铃声通知的方法是什么?

时间:2012-11-06 13:18:13

标签: android

用户使用RingtonePreference选择铃声,我可以通过以下方式提取Uri:

String pathString = mPreferences.getString(sKeySoundRingtone, null);
Uri pathUri;

if (pathString == null)
    pathUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);;
else
    pathUri = Uri.parse(pathString);

现在我想使用SoundPool类,因为我只使用通知(通常持续时间不超过5秒),我喜欢它提供的循环和速率选项。

尽我所能并且失败了,因为类可以用路径构造,而pathUri.getPath()不起作用。

有没有办法让这项工作?

2 个答案:

答案 0 :(得分:0)

当你的通知获得广播时,你会得到任何一个接收者的电话。那时你可以简单地调用函数来播放声音池中的声音。

见下文:

public void Sound(){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    android.util.Log.v("SOUND","["+volume+"]["+spool.play(soundID, volume, volume, 1, 0, 1f)+"]");
};

希望它会对你有所帮助。

评论我的任何查询。

感谢。

答案 1 :(得分:0)

要在SoundPool中加载铃声,可以通过以下方式使用AssetFileDescriptor:

Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION);
SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_ALARM, 0);

ContentResolver resolver = context.getContentResolver();
try (AssetFileDescriptor afd = resolver.openAssetFileDescriptor(ringtoneUri, "r")) {
    soundPool.load(afd, 1);
} catch (Exception e) {
    Log.w("SOUNDPOOL", "Couldn't open " + (ringtoneUri == null ? "null uri" : ringtoneUri.toString()), e);
}