gpt4 book ai didi

java - 播放声音作为媒体/闹钟/铃声?

转载 作者:行者123 更新时间:2023-12-02 02:04:35 32 4
gpt4 key购买 nike

我的应用程序播放闹钟。在 Android 声音设置中,此声音由“铃声音量” slider 控制,而不是由“闹钟音量” slider 控制。如何更改由“闹钟音量”控制的声音?

 public void doAlarm(){ 

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);

if (!r.isPlaying())
r.play();
}

我尝试过使用setAudioAttributes,但结果是“媒体音量” slider 控制音量:

public void doAlarm(){

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.setAudioAttributes(new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).build());
mp.setLooping(true);
mp.start();
}

我错过了什么?

最佳答案

我找到了解决方案:不使用 .create() 方法。请改用 .setDataSource().prepare()。代码如下:

public void ringAlarm() {

Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}

if (alarmUri == null) {
Log.e("ringAlarm" , "alarmUri null. Unable to get default sound URI");
return;
}

MediaPlayer mp = new MediaPlayer();
// This is what sets the media type as alarm
// Thus, the sound will be influenced by alarm volume
mp.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM).build());

try {
mp.setDataSource(getApplicationContext(), alarmUri);
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}

// To continuously loop the alarm sound
mp.setLooping(true);
mp.start();
}

关于java - 播放声音作为媒体/闹钟/铃声?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51002669/

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