Example usage for android.media AudioManager ACTION_AUDIO_BECOMING_NOISY

List of usage examples for android.media AudioManager ACTION_AUDIO_BECOMING_NOISY

Introduction

In this page you can find the example usage for android.media AudioManager ACTION_AUDIO_BECOMING_NOISY.

Prototype

String ACTION_AUDIO_BECOMING_NOISY

To view the source code for android.media AudioManager ACTION_AUDIO_BECOMING_NOISY.

Click Source Link

Document

Broadcast intent, a hint for applications that audio is about to become 'noisy' due to a change in audio outputs.

Usage

From source file:com.fastbootmobile.encore.service.PlaybackService.java

/**
 * Request the audio focus and registers the remote media controller
 *///from   w ww .  j  a va  2 s .  c o  m
private synchronized void requestAudioFocus() {
    if (!mHasAudioFocus) {
        final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        // Request audio focus for music playback
        int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            am.registerMediaButtonEventReceiver(RemoteControlReceiver.getComponentName(this));

            // Notify the remote metadata that we're getting active
            mRemoteMetadata.setActive(true);

            // Register AUDIO_BECOMING_NOISY to stop playback when earbuds are pulled
            registerReceiver(mAudioNoisyReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));

            // Add a WakeLock to avoid CPU going to sleep while music is playing
            mWakeLock.acquire();

            mHasAudioFocus = true;
        } else {
            Log.e(TAG, "Audio focus request denied: " + result);
        }
    }
}

From source file:leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService.java

private void registerBecomingNoisyReceiver() {
    //register after getting audio focus
    IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    registerReceiver(becomingNoisyReceiver, intentFilter);
}

From source file:com.mobilis.android.nfc.activities.MagTekFragment.java

@Override
public synchronized void onResume() {
    super.onResume();
    getActivity().registerReceiver(mHeadsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
    getActivity().registerReceiver(mNoisyAudioStreamReceiver,
            new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));

    // Performing this check in onResume() covers the case in which BT was
    // not enabled during onStart(), so we were paused to enable it...
    // onResume() will be called when ACTION_REQUEST_ENABLE activity
    // returns./*from  w ww  .jav  a 2 s  .c o  m*/
}

From source file:com.scooter1556.sms.lib.android.service.AudioPlayerService.java

private void registerAudioManagerReceiver() {
    IntentFilter filter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);

    audioManagerReceiver = new BroadcastReceiver() {
        @Override/*w  w w . j  ava  2 s . co m*/
        public void onReceive(Context context, Intent intent) {
            if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
                // Pause playback
                pause();
            }
        }
    };

    registerReceiver(audioManagerReceiver, filter);
}

From source file:com.wojtechnology.sunami.TheBrain.java

private void registerAudio() {
    if (mHasAudioFocus || !mIsInit) {
        return;//from   ww  w .  ja  va2  s  . c  om
    }
    mHasAudioFocus = true;

    // Add audio focus change listener
    mAFChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                pausePlayback();
                setUI(false);
            } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                // Does nothing cause made me play music at work
            } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                pausePlayback();
                unregisterAudio();
                setUI(false);
            }
        }
    };

    mAudioManager.requestAudioFocus(mAFChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    // Add headphone out listener
    registerReceiver(mNoisyAudioStreamReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));

    // Add notification and transport controls
    ComponentName eventReceiver = new ComponentName(getPackageName(),
            RemoteControlEventReceiver.class.getName());
    mSession = new MediaSessionCompat(this, "FireSession", eventReceiver, null);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC);
    mSession.setMetadata(new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_TITLE, "")
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, "")
            .putLong(MediaMetadata.METADATA_KEY_DURATION, -1).build());

    mSession.setCallback(new MediaSessionCompat.Callback() {

        @Override
        public void onSeekTo(long pos) {
            super.onSeekTo(pos);
            setProgress((int) pos, isPlaying());
        }
    });
    mSession.setActive(true);
}

From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java

private void registerNoisyReceiver() {
    if (mNoisyReceiver == null) {
        mNoisyReceiver = new ReceiverNoisyAudioStream();
        IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
        registerReceiver(mNoisyReceiver, intentFilter);
        if (LOCAL_LOGV)
            log("register noisy receiver", "v");
    } else {/*from   w w  w  . j  a v  a  2s  .  c o  m*/
        if (LOCAL_LOGV)
            log("noisy receiver already registered", "v");
    }
}