private void playSounds(boolean locked) { // User feedback for keyguard. if (mSuppressNextLockSound) { mSuppressNextLockSound = false; return; } final ContentResolver cr = mContext.getContentResolver(); if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) { final String whichSound = locked ? Settings.System.LOCK_SOUND : Settings.System.UNLOCK_SOUND; final String soundPath = Settings.System.getString(cr, whichSound); if (soundPath != null) { final Uri soundUri = Uri.parse("file://" + soundPath); if (soundUri != null) { final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); if (sfx != null) { sfx.setStreamType(AudioManager.STREAM_SYSTEM); sfx.play(); } else { Log.d(TAG, "playSounds: failed to load ringtone from uri: " + soundUri); } } else { Log.d(TAG, "playSounds: could not parse Uri: " + soundPath); } } else { Log.d(TAG, "playSounds: whichSound = " + whichSound + "; soundPath was null"); } } }
public static void playNotificationSound(Context context) { Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); if (uri != null) { Ringtone rt = RingtoneManager.getRingtone(context, uri); if (rt != null) { rt.setStreamType(AudioManager.STREAM_NOTIFICATION); rt.play(); } } }
@SuppressWarnings("deprecation") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int notifID; NotificationManager nm; // Get the notification ID notifID = getIntent().getExtras().getInt("NotifID"); // Create the pending intent that will reference the intent to go when clicked Intent i = new Intent(this, MainActivity.class); i.putExtra("NotifID", notifID); PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0); // Setup notification nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notif = new Notification(R.drawable.ic_launcher, "Food has expired!", System.currentTimeMillis()); // Hides status bar notification when clicked notif.flags |= Notification.FLAG_AUTO_CANCEL; CharSequence from = "RemindMe"; CharSequence message = "Food has expired!"; notif.setLatestEventInfo(this, from, message, detailsIntent); // ---100ms delay, vibrate for 250ms, pause for 100 ms and // then vibrate for 500ms--- notif.vibrate = new long[] {100, 250, 100, 500}; nm.notify(notifID, notif); // Added sound to notification try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); // this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION); r.setStreamType(AudioManager.STREAM_NOTIFICATION); r.play(); } catch (Exception e) { } // Destroy activity finish(); }
public AudioManagerVolumizerStrategy(Context context, int streamType, Uri defaultUri) { mContext = context; mStreamType = streamType; mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); if (defaultUri == null) { if (mStreamType == AudioManager.STREAM_RING) { defaultUri = Settings.System.DEFAULT_RINGTONE_URI; } else if (mStreamType == AudioManager.STREAM_NOTIFICATION) { defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI; } else { defaultUri = Settings.System.DEFAULT_ALARM_ALERT_URI; } } mRingtone = RingtoneManager.getRingtone(mContext, defaultUri); if (mRingtone != null) { mRingtone.setStreamType(mStreamType); } }