我正在进行闹钟应用,而且我陷入了警报音阶段。我目前正在尝试使用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);
}
});
这是铃声选择器的意图:
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);
}
}
}
任何帮助都将受到高度赞赏。感谢。
答案 0 :(得分:1)
以下是使用RingtoneManager
的示例 Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
ringtone.play();
您不需要MediaPlayer。