gpt4 book ai didi

java - 如何播放从 RingtonePreference 中选择的铃声

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:03 26 4
gpt4 key购买 nike

我正在尝试播放从 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的时候

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");
}
}

这应该是你想要的:)我希望它有效

关于java - 如何播放从 RingtonePreference 中选择的铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10460499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com