gpt4 book ai didi

java - 如何在 Android 中的闹钟应用程序中设置闹钟铃声

转载 作者:行者123 更新时间:2023-12-01 12:47:07 25 4
gpt4 key购买 nike

我正在制作一个警报应用程序,但我陷入了警报音阶段。我目前正在尝试使用 RingtoneManager 设置闹钟铃声,但它根本不起作用。此外,我有一个搜索栏应该播放用户选择的警报音,但它没有。当我设置闹钟铃声时,我不希望它成为默认铃声,而只是该闹钟的铃声,因为用户可以选择创建多个闹钟。

这是搜索栏代码:

volumeBar = (SeekBar) findViewById(R.id.seekBarVolume);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mMediaPlayer = new MediaPlayer();

volumeBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
volumeBar.setKeyProgressIncrement(1);
volumeBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM));

volumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
try {
Uri alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);

mMediaPlayer.setDataSource(getApplicationContext(), alert);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0 && !mMediaPlayer.isPlaying()) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, progress, AudioManager.FLAG_SHOW_UI);
}
});

这是铃声选择器的 Intent :

case R.id.buttonAlarmTones:
try {
mMediaPlayer.release();
mMediaPlayer.stop();
} catch (Exception e) {
// TODO: handle exception
}
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM));
startActivityForResult(intent, 5);
break;

最后,这是 onActivityResult():

@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
// TODO Auto-generated method stub
super.onActivityResult(arg0, arg1, arg2);
if (arg1 == RESULT_OK && arg0 == 5) {
Uri uri = arg2.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), uri);

Uri uri2 = Uri.parse(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString());
Ringtone ringtone2 = RingtoneManager.getRingtone(getApplicationContext(), uri2);

if (uri != null) {
selectedAlarmTone.setText(ringtone.getTitle(getApplicationContext()));
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM, uri);
} else {
selectedAlarmTone.setText(ringtone2.getTitle(getApplicationContext()));
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM, uri2);
}
}
}

任何帮助将不胜感激。谢谢。

最佳答案

这是使用 RingtoneManager 的示例

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
ringtone.play();

您不需要 MediaPlayer。

关于java - 如何在 Android 中的闹钟应用程序中设置闹钟铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590542/

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