我使用一个简单的选择对话框让用户选择一个通知声音,下面是启动选择器的代码:
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.selectSound));
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(LocalCfg.getNotificationSound()));
startActivityForResult(intent, SELECT_RINGTONE_REQUEST);LocalCfg.getNotificationSound()只检查SharedPreferences中的设置并返回默认通知声音Uri,以防设置尚不存在:
public static String getNotificationSound() {
return mPrefs.getString(KEY_PREF_NOTIFY_SOUND_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString());
}在所有测试的手机上观察到的问题:列出的“默认”声音不是通知/警报声音,而是实际的电话铃声(系统默认或用户设置的自定义)。
一些手机(三星Galaxy,Xperia Z1 Compact)将其显示为“默认通知声音”(实际上是错误的),还有一些手机(Nexus,SDK 22)显示为“默认铃声”。
如果我显式传递RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM标志,为什么会发生这种情况?
发布于 2016-04-26 17:22:43
额外使用RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI:
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.selectSound));
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(LocalCfg.getNotificationSound()));
startActivityForResult(intent, SELECT_RINGTONE_REQUEST);发布于 2015-12-16 12:52:00
我看过不同的方法,因为我的手机上有这个问题,Whatsapp似乎已经绕过了它(在我的手机上播放自己的语气)。
根据我的研究,唯一可能的方法是检查长度(see this answer),如果文件不可能播放您自己的音调,下面是我的代码:
//Create default notification ringtone
MediaPlayer mp = MediaPlayer.create(LinxaleApplication.getApplicationInstance(),
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
//NOTE: not checking if the sound is 0 length, becuase then it should just not be played
if (mp.getDuration() > 5000 /* Ringtone, not notification, happens on HTC 1 m7 5.X version */) {
mp.release();
mp = MediaPlayer.create(LinxaleApplication.getApplicationInstance(),
R.raw.notification_sound);
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
}
});
mp.start();https://stackoverflow.com/questions/30638492
复制相似问题