2

我正在尝试为 Android 构建自定义类型的闹钟,并且我已经设置了一个设置页面以允许用户选择他们想要使用的闹钟声音。在他们选择它之后,它会被放入 SharedPreferences 中,这很容易。

我遇到的问题是如何在使用 RingtoneManager.TYPE_ALARM 时播放他们选择的警报,以便音量基于系统警报声音而不是系统通知音量。

我已经尝试了多种方法来让它使用警报音量,但似乎没有任何效果。

1.

Uri currentRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
currentRingtone = RingtoneManager.getRingtone(context, currentRingtoneUri);
currentRingtone.play();

2.

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String syncConnPref = sharedPref.getString("alarm_new_alarm_sound", null);

if (syncConnPref != null) {
  RingtoneManager.getRingtone(this, Uri.parse(syncConnPref)).play();
}

我似乎找不到任何其他直接播放实际警报音量的方法。网上的一切都只是在播放铃声。

如果有人对使用警报音量有任何见解,我将不胜感激。

4

1 回答 1

2

有点旧的线程,但也许这会在未来帮助某人。
我目前正在自己​​制作闹钟,我正在这样做:

int audioFile = prefs.getInt("audioFile", R.raw.yourFile);
MediaPlayer mp = new MediaPlayer();

try {
    mp.reset();

    mp.setDataSource(PlayerActivity.this,    
    Uri.parse("android.resource://your.package.here/" + audioFile));

    mp.setAudioStreamType(STREAM_ALARM);
    mp.setLooping(true);
    mp.prepare();
    mp.start();
}

catch (...) {
    ...
}

它对我来说很好。

于 2017-12-16T10:32:55.380 回答