Reputation: 11608
I use a simple picker dialog to let the user choose a notification sound, here's the code to start the picker:
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);
The LocalCfg.getNotificationSound()
simply checks the setting inside SharedPreferences
and returns a default notification sound Uri
in case the setting does not yet exist:
public static String getNotificationSound() {
return mPrefs.getString(KEY_PREF_NOTIFY_SOUND_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString());
}
issue observed on all phones tested: the "default" sound listed is NOT a notification/alarm sound, but the actual phone ringtone (system default or custom that was set by user).
Some phones (Samsung Galaxy Young, Xperia Z1 Compact) display it as "default notification sound" (which is actually wrong), some others (Nexus devices, SDK 22) as "default ringtone".
Why is it happening if I explicitly pass the RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM
flags?
Upvotes: 1
Views: 1311
Reputation: 306
Use RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI
extra:
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);
Upvotes: 1
Reputation: 29
I have looked at different ways of doing this, because I have this problem on my phone and Whatsapp has seemed to bypass it themselves (playing their own tone on my phone).
The only possible way from my research is to check the length (see this answer) and play your own tone if the file is not possible, here's my code:
//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();
Upvotes: 0