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:Main.java
/** * Utility method that/* w w w .j a v a 2s . c o m*/ * @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:bala.padio.Player.java
private void Play(String url) { try {//from ww w . j a va 2s. c om if (url == null) { Log.e(TAG, "Empty url"); return; } // request audio focus if (audioManager == null) { audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); } int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.e(TAG, "Audio focus not granted"); return; } // initialize player if (mediaPlayer != null) { mediaPlayer.reset(); } else { mediaPlayer = new MediaPlayer(); } // set playerUrl source and prepare media player this.playerUrl = url; Uri streamUrl = Uri.parse(getStreamUrl(url)); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(this, streamUrl); mediaPlayer.setOnErrorListener(this); mediaPlayer.setOnPreparedListener(this); mediaPlayer.prepareAsync(); } catch (Exception ex) { ex.printStackTrace(); } }
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 va 2 s . c o m return START_STICKY; }
From source file:org.chromium.tools.audio_focus_grabber.AudioFocusGrabberListenerService.java
void processIntent(Intent intent) { if (mMediaPlayer != null) { Log.i(TAG,/*from w ww .j av a 2 s .c o m*/ "There's already a MediaPlayer playing," + " stopping the existing player and abandon focus"); releaseAndAbandonAudioFocus(); } String action = intent.getAction(); if (ACTION_SHOW_NOTIFICATION.equals(action)) { showNotification(); } else if (ACTION_HIDE_NOTIFICATION.equals(action)) { hideNotification(); } else if (ACTION_GAIN.equals(action)) { gainFocusAndPlay(AudioManager.AUDIOFOCUS_GAIN); } else if (ACTION_TRANSIENT_PAUSE.equals(action)) { gainFocusAndPlay(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } else if (ACTION_TRANSIENT_DUCK.equals(action)) { gainFocusAndPlay(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); } else { assert false; } }
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;//w w w . ja v a 2 s . c o 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; }
From source file:com.ecoplayer.beta.MusicService.java
private boolean initMediaPlayer() { startForeground(NOTIFICATION_ID, notiBuilder.getNotification()); AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); int result = audioManager.requestAudioFocus(audioFocusChangeList, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { mMediaPlayer = new MediaPlayer(); // initialize it here mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnCompletionListener(onCompletion); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // prevent CPU from going to sleep mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); return true; } else {// w w w . j av a2 s .co m Log.w(LOG_TAG, "The service hasn't got the audio focus"); } return false; }
From source file:org.y20k.transistor.PlayerService.java
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { // gain of audio focus of unknown duration case AudioManager.AUDIOFOCUS_GAIN: if (mPlayback) { if (mMediaPlayer == null) { initializeMediaPlayer(); } else if (!mMediaPlayer.isPlaying()) { mMediaPlayer.start();/*from ww w . j a v a2 s.c o m*/ } mMediaPlayer.setVolume(1.0f, 1.0f); } break; // loss of audio focus of unknown duration case AudioManager.AUDIOFOCUS_LOSS: stopPlayback(); break; // transient loss of audio focus case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: if (!mPlayback && mMediaPlayer != null && mMediaPlayer.isPlaying()) { stopPlayback(); } else if (mPlayback && mMediaPlayer != null && mMediaPlayer.isPlaying()) { mMediaPlayer.pause(); } break; // temporary external request of audio focus case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { mMediaPlayer.setVolume(0.1f, 0.1f); } break; } }
From source file:com.perm.DoomPlay.PlayingService.java
@Override public void onCreate() { super.onCreate(); isLoadingTrack = false;/*from w w w .ja v a2s . com*/ isShuffle = false; isPlaying = true; isLoop = false; afListener = new AFListener(); audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audioManager.requestAudioFocus(afListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); ComponentName componentName = new ComponentName(this, MediaButtonReceiver.class); audioManager.registerMediaButtonEventReceiver(componentName); ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(callListener, CallListener.LISTEN_CALL_STATE); PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); serviceAlive = true; bassPlayer = new BassPlayer(); bassPlayer.setOnCompletetion(this); }
From source file:net.dian1.player.service.PlayerService.java
@Override public void onCreate() { Log.i(Dian1Application.TAG, "Player Service onCreate"); mPlayerEngine = new PlayerEngineImpl(); mPlayerEngine.addListener(mLocalEngineListener); mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE); mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); // mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); // mPhoneStateListener = new PhoneStateListener() { ///*ww w . ja v a2s. c o m*/ // @Override // public void onCallStateChanged(int state, String incomingNumber) { // Log.e(Dian1Application.TAG, "onCallStateChanged"); // if (state == TelephonyManager.CALL_STATE_IDLE) { // // resume playback // } else { // if (mPlayerEngine != null) { // mPlayerEngine.pause(); // } // } // } // // }; // mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); mWifiLock = mWifiManager.createWifiLock(Dian1Application.TAG); mWifiLock.setReferenceCounted(false); Dian1Application.getInstance().setConcretePlayerEngine(mPlayerEngine); mRemoteEngineListeners = Dian1Application.getInstance().fetchPlayerEngineListener(); }
From source file:org.amahi.anywhere.service.AudioService.java
private void setUpAudioPlayerRemote() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName()); Intent audioIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); audioIntent.setComponent(audioReceiver); PendingIntent audioPendingIntent = PendingIntent.getBroadcast(this, 0, audioIntent, 0); audioPlayerRemote = new RemoteControlClient(audioPendingIntent); audioPlayerRemote.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS); audioPlayerRemote.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); audioManager.registerMediaButtonEventReceiver(audioReceiver); audioManager.registerRemoteControlClient(audioPlayerRemote); }