我正在创建一个闹钟应用程序。我列出了所有可用的铃声(不仅仅是标准铃声)。当用户点击列表中的音调时,我使用MediaPlayer播放它。在此期间,我还希望能够使用音量按钮(在手机上)调整闹钟音量。但是当我按下这些按钮时,我会调整媒体音量,而不是警报音量。
有什么方法吗?
以下是我用来显示音调的代码
private void chooseTone(final ArrayList<String> tones, final ArrayList<String> paths) {
//final String lastRingtone = tuneName.getText().toString();
//int i = tones.indexOf(lastRingtone);
int i ;
try {
i = paths.indexOf(crt.tune);
} catch (NullPointerException ex) {
i = -1;
}
final AlertDialog alert = new AlertDialog.Builder(this).setTitle("Ringtone").
setSingleChoiceItems(tones.toArray(new String[]{}), i, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int index) {
String currentPath = paths.get(index);
try {
if (mp != null)
mp.stop();
mp = new MediaPlayer();
mp.setDataSource(currentPath);
mp.prepare();
mp.seekTo(0);
mp.start();
} catch (Exception ex) {
}
selectedRingtoneIndex = index;
}
}).create();
alert.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
tuneName.setText(tones.get(selectedRingtoneIndex));
//uri = Uri.parse(paths.get(selectedRingtoneIndex));
crt.tune = paths.get(selectedRingtoneIndex);
try {
mp.stop();
} catch (Exception ex) {}
}
});
alert.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
try {
mp.stop();
} catch (Exception ex) {}
}
});
alert.show();
}
答案 0 :(得分:0)
使用:
setVolumeControlStream(AudioManager.STREAM_ALARM);
答案 1 :(得分:0)
我认为您还应该为媒体播放器设置AudioStreamType。
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);