Play a specific notification sound

北战南征 提交于 2019-12-11 13:39:41

问题


I've created a dialog for selecting a notification sound for an app. I'm querying the notification sounds by getting a cursor from the RingtoneManager:

RingtoneManager manager = new RingtoneManager(this);
manager.setType(RingtoneManager.TYPE_NOTIFICATION);
Cursor cursor = manager.getCursor();

I then store the full path to it by concatenating the sound path with the name.

This works fine when I set the selected sound as the notification sound but I can't get media player to play it on selection:

MediaPlayer mp = MediaPlayer.create(this, Uri.parse(path));

This throws an IllegalStateException:

java.lang.IllegalStateException: Unknown URL: content://media/internal/audio/media/Capella

Any ideas on how to get the correct path to a specific notification sound?


回答1:


The correct way to play a sound is to use it's ID and not the name:

cursor.getInt(RingtoneManager.ID_COLUMN_INDEX)

Thanks Darkie for pointing me to the right direction.




回答2:


//in order to play the ringtone, you need to create a new Ringtone with RingtoneManager and pass it to a variable

Ringtone rt = mRingtoneManager.getRingtone(this, uri); rt.play();

Thanks



来源:https://stackoverflow.com/questions/28895719/play-a-specific-notification-sound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!