3

用户使用 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() 不起作用。

有没有办法使这项工作?

4

2 回答 2

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)+"]");
};
于 2012-11-06T13:30:30.247 回答
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);
}
于 2020-03-19T17:00:51.190 回答