播放从RingtonePreference中选择的铃声

播放从RingtonePreference中选择的铃声

我正在尝试播放从RingtonePreference中选择的铃声。我该怎么玩?

这是我的xml文件代码

<RingtonePreference
    android:title="Choose Alarm"
    android:key="ringtone"
    android:summary="this is summary"
    ></RingtonePreference>

这是我在用java做的
SharedPreferences getAlarms = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String alarms = getAlarms.getString("ringtone", "default ringtone");

当我像这样烤面包时
Toast.makeText(getApplicationContext(), alarms, Toast.LENGTH_LONG).show();

然后它显示了这种路径
content://media/internal/audio/media/50

但我不知道该怎么玩。

请帮忙。

最佳答案

private void alarm(){
    SharedPreferences getAlarms = PreferenceManager.
                                  getDefaultSharedPreferences(getBaseContext());
    String alarms = getAlarms.getString("ringtone", "default ringtone");
    Uri uri = Uri.parse(alarms);
    playSound(this, uri);

    //call mMediaPlayer.stop(); when you want the sound to stop
}


private MediaPlayer mMediaPlayer;
private void playSound(Context context, Uri alert) {
        mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(context, alert);
            final AudioManager audioManager = (AudioManager) context
                    .getSystemService(Context.AUDIO_SERVICE);
            if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
            }
        } catch (IOException e) {
            System.out.println("OOPS");
        }
    }

这应该是您想要的:)我希望它能起作用

07-27 17:02