List of usage examples for android.media AudioManager AUDIOFOCUS_GAIN
int AUDIOFOCUS_GAIN
To view the source code for android.media AudioManager AUDIOFOCUS_GAIN.
Click Source Link
From source file:ru.org.sevn.audiobookplayer.AppService.java
@Override public void onCreate() { defaultIcon = BitmapFactory.decodeResource(getResources(), R.drawable.picture); defaultIcon = Util.getScaledImage(defaultIcon, ICON_SIZE, ICON_SIZE, Util.getImageFactor(getResources())); audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); super.onCreate(); mp3Player.initialize();/*from w w w . j av a 2s. c om*/ restoreSettings(); mp3Player.getMediaPlayer().addChangeStateListener(this); mp3Player.getAppSettings().addPropertyChangeListener(this); TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if (mgr != null) { mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); } musicIntentReceiver = new MusicIntentReceiver(); registerReceiver(musicIntentReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); }
From source file:com.customprogrammingsolutions.MediaStreamer.MediaStreamerService.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) return false; else/*w ww . j a v a 2 s. c om*/ return true; }
From source file:ru.kudzmi.rajitaku.radioService.RadioService.java
private boolean requestAudioFocus() { int result = audioManager.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; }
From source file:com.lithiumli.fiction.PlaybackService.java
public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { pause();//from ww w . java 2s. co m } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // Need to sync this so that we don't react to our own events if (!isPlaying()) { // unpause(); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { abandonAudioFocus(); pause(); } }
From source file:bala.padio.Player.java
@Override public void onAudioFocusChange(int focusChange) { Log.e(TAG, "onAudioFocusChange: " + focusChange); switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: // resume playback if (mediaPlayer == null || !mediaPlayer.isPlaying()) { Play(playerUrl);/*w w w . j av a2 s . c o m*/ } mediaPlayer.setVolume(1.0f, 1.0f); break; case AudioManager.AUDIOFOCUS_LOSS: // Lost focus for an unbounded amount of time: stop playback and release media player Stop(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // Lost focus for a short time, but we have to stop // playback. We don't release the media player because playback // is likely to resume if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.reset(); } break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // Lost focus for a short time, but it's ok to keep playing // at an attenuated level if (mediaPlayer.isPlaying()) mediaPlayer.setVolume(0.1f, 0.1f); break; } }
From source file:com.sourceauditor.sahomemonitor.MainActivity.java
private void playAudio() { if (audioPlayer == null) { audioPlayer = new MediaPlayer(); audioPlayer.setOnPreparedListener(this); audioPlayer.setOnBufferingUpdateListener(this); audioPlayer.setOnErrorListener(this); } else {/*from w w w .j a va2 s . c o m*/ audioPlayer.stop(); audioPlayer.reset(); } try { audioPlayer.setDataSource(this, getAudioUri()); audioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { logAndDisplayError("Audio in use by another app"); } else { pauseBeforePlaying = false; audioPlayer.prepareAsync(); } } catch (IllegalArgumentException e) { logAndDisplayError("Illegal argument for audioPlayer"); } catch (SecurityException e) { logAndDisplayError("Security error for audioPlayer"); } catch (IllegalStateException e) { logAndDisplayError("Illegal state exception for audioPlayer"); } catch (IOException e) { logAndDisplayError("IO Error for audio player: " + e.getMessage()); } }
From source file:com.ironsmile.cordova.mediaevents.MediaEventListener.java
/** * Updates the JavaScript side with new audio focus event * * @param focusEvent the received event//from w ww . j av a 2s . c om * @return */ private void sendFocusEvent(int focusEvent) { JSONObject obj = new JSONObject(); try { switch (focusEvent) { case AudioManager.AUDIOFOCUS_GAIN: case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT: case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE: case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK: obj.put("type", "audiofocusgain"); break; case AudioManager.AUDIOFOCUS_LOSS: obj.put("type", "audiofocusloss"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: obj.put("type", "audiofocuslosstransient"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: obj.put("type", "audiofocuslosstransientcanduck"); break; } } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } sendUpdate(obj, true); }
From source file:com.andreadec.musicplayer.MusicService.java
/** * Called when the service is created.//from ww w. j av a2s. c o m */ @Override public void onCreate() { PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MusicServiceWakelock"); // Initialize the telephony manager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); phoneStateListener = new MusicPhoneStateListener(); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // Initialize pending intents quitPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.andreadec.musicplayer.quit"), 0); previousPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.andreadec.musicplayer.previous"), 0); playpausePendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.andreadec.musicplayer.playpause"), 0); nextPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.andreadec.musicplayer.next"), 0); pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), PendingIntent.FLAG_UPDATE_CURRENT); // Read saved user preferences preferences = PreferenceManager.getDefaultSharedPreferences(this); // Initialize the media player mediaPlayer = new MediaPlayer(); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK); // Enable the wake lock to keep CPU running when the screen is switched off shuffle = preferences.getBoolean(Constants.PREFERENCE_SHUFFLE, Constants.DEFAULT_SHUFFLE); repeat = preferences.getBoolean(Constants.PREFERENCE_REPEAT, Constants.DEFAULT_REPEAT); repeatAll = preferences.getBoolean(Constants.PREFERENCE_REPEATALL, Constants.DEFAULT_REPEATALL); try { // This may fail if the device doesn't support bass boost bassBoost = new BassBoost(1, mediaPlayer.getAudioSessionId()); bassBoost.setEnabled( preferences.getBoolean(Constants.PREFERENCE_BASSBOOST, Constants.DEFAULT_BASSBOOST)); setBassBoostStrength(preferences.getInt(Constants.PREFERENCE_BASSBOOSTSTRENGTH, Constants.DEFAULT_BASSBOOSTSTRENGTH)); bassBoostAvailable = true; } catch (Exception e) { bassBoostAvailable = false; } try { // This may fail if the device doesn't support equalizer equalizer = new Equalizer(1, mediaPlayer.getAudioSessionId()); equalizer.setEnabled( preferences.getBoolean(Constants.PREFERENCE_EQUALIZER, Constants.DEFAULT_EQUALIZER)); setEqualizerPreset( preferences.getInt(Constants.PREFERENCE_EQUALIZERPRESET, Constants.DEFAULT_EQUALIZERPRESET)); equalizerAvailable = true; } catch (Exception e) { equalizerAvailable = false; } random = new Random(System.nanoTime()); // Necessary for song shuffle shakeListener = new ShakeListener(this); if (preferences.getBoolean(Constants.PREFERENCE_SHAKEENABLED, Constants.DEFAULT_SHAKEENABLED)) { shakeListener.enable(); } telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); // Start listen for telephony events // Inizialize the audio manager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mediaButtonReceiverComponent = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName()); audioManager.registerMediaButtonEventReceiver(mediaButtonReceiverComponent); audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); // Initialize remote control client if (Build.VERSION.SDK_INT >= 14) { icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mediaButtonReceiverComponent); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT); audioManager.registerRemoteControlClient(remoteControlClient); } updateNotificationMessage(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.andreadec.musicplayer.quit"); intentFilter.addAction("com.andreadec.musicplayer.previous"); intentFilter.addAction("com.andreadec.musicplayer.previousNoRestart"); intentFilter.addAction("com.andreadec.musicplayer.playpause"); intentFilter.addAction("com.andreadec.musicplayer.next"); intentFilter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY); broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals("com.andreadec.musicplayer.quit")) { sendBroadcast(new Intent("com.andreadec.musicplayer.quitactivity")); stopSelf(); return; } else if (action.equals("com.andreadec.musicplayer.previous")) { previousItem(false); } else if (action.equals("com.andreadec.musicplayer.previousNoRestart")) { previousItem(true); } else if (action.equals("com.andreadec.musicplayer.playpause")) { playPause(); } else if (action.equals("com.andreadec.musicplayer.next")) { nextItem(); } else if (action.equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { if (preferences.getBoolean(Constants.PREFERENCE_STOPPLAYINGWHENHEADSETDISCONNECTED, Constants.DEFAULT_STOPPLAYINGWHENHEADSETDISCONNECTED)) { pause(); } } } }; registerReceiver(broadcastReceiver, intentFilter); if (!isPlaying()) { loadLastSong(); } startForeground(Constants.NOTIFICATION_MAIN, notification); }
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 a v a 2s. c om // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { return true; } return false; }
From source file:com.hang.exoplayer.PlayService.java
private boolean requestFocus() { // Request audio focus for playback int result = mAm.requestAudioFocus(mOnAudioFocusChangeListener, // Use the music stream. AudioManager.STREAM_MUSIC,/* w w w . ja va 2 s . co m*/ // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; }