List of usage examples for android.media AudioManager requestAudioFocus
public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint)
From source file:Main.java
/** * Utility method that//w ww . ja va 2 s. c om * @param am the AudioManager requesting the focus * @param afChangeListener the foucs listener associated to the AudioManager * @return true if focus is granted, false if not. */ public static boolean focusRequestGranted(AudioManager am, Object afChangeListener) { int result = am.requestAudioFocus((AudioManager.OnAudioFocusChangeListener) afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; }
From source file:Main.java
public static void stopExtraMusic(Context context) { AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); }
From source file:net.gcompris.GComprisActivity.java
public static boolean requestAudioFocus() { Context mContext = m_instance.getApplicationContext(); AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); // Request audio focus for playback int result = am.requestAudioFocus(null, // Use the music stream. AudioManager.STREAM_MUSIC,/*from w w w . j av a 2 s.com*/ // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { return true; } return false; }
From source file:org.fdroid.enigtext.notifications.MessageNotifier.java
private static void sendInThreadNotification(Context context) { try {/*from w w w.ja v a 2 s .c om*/ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); if (!sp.getBoolean(ApplicationPreferencesActivity.IN_THREAD_NOTIFICATION_PREF, true)) { return; } String ringtone = sp.getString(ApplicationPreferencesActivity.RINGTONE_PREF, null); if (ringtone == null) return; Uri uri = Uri.parse(ringtone); MediaPlayer player = new MediaPlayer(); player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); player.setDataSource(context, uri); player.setLooping(false); player.setVolume(0.25f, 0.25f); player.prepare(); final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { audioManager.abandonAudioFocus(null); } }); player.start(); } catch (IOException ioe) { Log.w("MessageNotifier", ioe); } }
From source file:com.securecomcode.text.notifications.MessageNotifier.java
private static void sendInThreadNotification(Context context) { try {//w w w. jav a 2 s .co m if (!TextSecurePreferences.isInThreadNotifications(context)) { return; } String ringtone = TextSecurePreferences.getNotificationRingtone(context); if (ringtone == null) return; Uri uri = Uri.parse(ringtone); MediaPlayer player = new MediaPlayer(); player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); player.setDataSource(context, uri); player.setLooping(false); player.setVolume(0.25f, 0.25f); player.prepare(); final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { audioManager.abandonAudioFocus(null); } }); player.start(); } catch (IOException ioe) { Log.w("MessageNotifier", ioe); } }
From source file:org.thoughtcrime.SMP.notifications.MessageNotifier.java
private static void sendInThreadNotification(Context context, Recipients recipients) { try {/*w ww.j a v a 2 s . co m*/ if (!TextSecurePreferences.isInThreadNotifications(context)) { return; } Uri uri = recipients.getRingtone(); if (uri == null) { String ringtone = TextSecurePreferences.getNotificationRingtone(context); if (ringtone == null) { Log.w(TAG, "ringtone preference was null."); return; } else { uri = Uri.parse(ringtone); } } if (uri == null) { Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context)); return; } MediaPlayer player = new MediaPlayer(); player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); player.setDataSource(context, uri); player.setLooping(false); player.setVolume(0.25f, 0.25f); player.prepare(); final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { audioManager.abandonAudioFocus(null); } }); player.start(); } catch (IOException ioe) { Log.w("MessageNotifier", ioe); } }
From source file:de.j4velin.headsetturnon.ListeningService.java
@Override public int onStartCommand(final Intent intent, int flags, int startId) { if (BuildConfig.DEBUG) android.util.Log.d("HeadsetTurnOn", "service start"); AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); boolean failed = am.requestAudioFocus(focusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_FAILED; am.registerMediaButtonEventReceiver(new ComponentName(ListeningService.this, Receiver.class)); if (BuildConfig.DEBUG) android.util.Log.d("HeadsetTurnOn", "request audiofocus failed? " + failed); NotificationCompat.Builder b = new NotificationCompat.Builder(this); b.setOngoing(true).setContentText(getString(R.string.notification_text)) .setContentTitle(getString(R.string.notification_title)).setSmallIcon(R.drawable.ic_notification) .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, Main.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0)); startForeground(1, b.build());// w ww . j a v a2s .c o m return START_STICKY; }
From source file:github.daneren2005.dsub.util.Util.java
@TargetApi(8) public static void requestAudioFocus(final Context context) { if (Build.VERSION.SDK_INT >= 8 && !hasFocus) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); hasFocus = true;//from w w w .j av a2 s. c om audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadServiceImpl downloadService = (DownloadServiceImpl) context; if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isJukeboxEnabled()) { if (downloadService.getPlayerState() == PlayerState.STARTED) { SharedPreferences prefs = getPreferences(context); int lossPref = Integer .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { lowerFocus = true; downloadService.setVolume(0.1f); } else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { pauseFocus = true; downloadService.pause(); } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (pauseFocus) { pauseFocus = false; downloadService.start(); } else if (lowerFocus) { lowerFocus = false; downloadService.setVolume(1.0f); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) { hasFocus = false; downloadService.pause(); audioManager.abandonAudioFocus(this); } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
From source file:ca.mudar.snoozy.receiver.PowerConnectionReceiver.java
private void nativeRingtone(Context context, boolean hasSound) { if (hasSound) { final AudioManager.OnAudioFocusChangeListener listener = this; final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int result = audioManager.requestAudioFocus(listener, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mRingtone = RingtoneManager.getRingtone(context, notification); mRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION); mRingtone.play();// ww w . ja v a 2 s .co m final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { audioManager.abandonAudioFocus(listener); } }, AUDIO_FOCUS_DURATION); } } }
From source file:com.reallynourl.nourl.fmpfoldermusicplayer.backend.MediaService.java
private boolean requestAudioFocus() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { mIsPreparedToPlay = false;/*from w ww . j ava2s. co m*/ Toast.makeText(getApplicationContext(), "Failed to get audio focus. Not starting playback.", Toast.LENGTH_LONG).show(); if (mMediaPlayer != null) { mMediaPlayer.stop(); } return false; } else { mMediaPlayer.setVolume(1.0f, 1.0f); } return true; }