Example usage for android.media AudioManager AUDIOFOCUS_GAIN_TRANSIENT

List of usage examples for android.media AudioManager AUDIOFOCUS_GAIN_TRANSIENT

Introduction

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

Prototype

int AUDIOFOCUS_GAIN_TRANSIENT

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

Click Source Link

Document

Used to indicate a temporary gain or request of audio focus, anticipated to last a short amount of time.

Usage

From source file:com.example.android.gft.ColorsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_list, container, false);

    // Create and setup the {@link AudioManager} to request audio focus
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word(R.string.color_red, R.string.miwok_color_red, R.drawable.color_red, R.raw.color_red));
    words.add(new Word(R.string.color_mustard_yellow, R.string.miwok_color_mustard_yellow,
            R.drawable.color_mustard_yellow, R.raw.color_mustard_yellow));
    words.add(new Word(R.string.color_dusty_yellow, R.string.miwok_color_dusty_yellow,
            R.drawable.color_dusty_yellow, R.raw.color_dusty_yellow));
    words.add(new Word(R.string.color_green, R.string.miwok_color_green, R.drawable.color_green,
            R.raw.color_green));//ww  w. j  av  a  2 s. c  o m
    words.add(new Word(R.string.color_brown, R.string.miwok_color_brown, R.drawable.color_brown,
            R.raw.color_brown));
    words.add(
            new Word(R.string.color_gray, R.string.miwok_color_gray, R.drawable.color_gray, R.raw.color_gray));
    words.add(new Word(R.string.color_black, R.string.miwok_color_black, R.drawable.color_black,
            R.raw.color_black));
    words.add(new Word(R.string.color_white, R.string.miwok_color_white, R.drawable.color_white,
            R.raw.color_white));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_colors);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);

            // Request audio focus so in order to play the audio file. The app needs to play a
            // short audio file, so we will request audio focus with a short amount of time
            // with AUDIOFOCUS_GAIN_TRANSIENT.
            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // We have audio focus now.

                // Create and setup the {@link MediaPlayer} for the audio resource associated
                // with the current word
                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                // Start the audio file
                mMediaPlayer.start();

                // Setup a listener on the media player, so that we can stop and release the
                // media player once the sound has finished playing.
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });

    return rootView;
}

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 w w  .j a  v  a  2s  .  com*/
 * @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.example.android.gft.PhrasesFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_list, container, false);

    // Create and setup the {@link AudioManager} to request audio focus
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word(R.string.phrase_where_are_you_going, R.string.miwok_phrase_where_are_you_going,
            R.raw.phrase_where_are_you_going));
    words.add(new Word(R.string.phrase_what_is_your_name, R.string.miwok_phrase_what_is_your_name,
            R.raw.phrase_what_is_your_name));
    words.add(new Word(R.string.phrase_my_name_is, R.string.miwok_phrase_my_name_is, R.raw.phrase_my_name_is));
    words.add(new Word(R.string.phrase_how_are_you_feeling, R.string.miwok_phrase_how_are_you_feeling,
            R.raw.phrase_how_are_you_feeling));
    words.add(new Word(R.string.phrase_im_feeling_good, R.string.miwok_phrase_im_feeling_good,
            R.raw.phrase_im_feeling_good));
    words.add(new Word(R.string.phrase_are_you_coming, R.string.miwok_phrase_are_you_coming,
            R.raw.phrase_are_you_coming));
    words.add(new Word(R.string.phrase_yes_im_coming, R.string.miwok_phrase_yes_im_coming,
            R.raw.phrase_yes_im_coming));
    words.add(new Word(R.string.phrase_im_coming, R.string.miwok_phrase_im_coming, R.raw.phrase_im_coming));
    words.add(new Word(R.string.phrase_lets_go, R.string.miwok_phrase_lets_go, R.raw.phrase_lets_go));
    words.add(new Word(R.string.phrase_come_here, R.string.miwok_phrase_come_here, R.raw.phrase_come_here));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_phrases);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);//from w  w  w  .j  a  v a  2s . co m

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);

            // Request audio focus so in order to play the audio file. The app needs to play a
            // short audio file, so we will request audio focus with a short amount of time
            // with AUDIOFOCUS_GAIN_TRANSIENT.
            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // We have audio focus now.

                // Create and setup the {@link MediaPlayer} for the audio resource associated
                // with the current word
                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                // Start the audio file
                mMediaPlayer.start();

                // Setup a listener on the media player, so that we can stop and release the
                // media player once the sound has finished playing.
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });

    return rootView;
}

From source file:com.example.android.gft.NumbersFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_list, container, false);

    // Create and setup the {@link AudioManager} to request audio focus
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(/* w  w w.  j  av  a 2 s . c om*/
            new Word(R.string.number_one, R.string.miwok_number_one, R.drawable.number_one, R.raw.number_one));
    words.add(
            new Word(R.string.number_two, R.string.miwok_number_two, R.drawable.number_two, R.raw.number_two));
    words.add(new Word(R.string.number_three, R.string.miwok_number_three, R.drawable.number_three,
            R.raw.number_three));
    words.add(new Word(R.string.number_four, R.string.miwok_number_four, R.drawable.number_four,
            R.raw.number_four));
    words.add(new Word(R.string.number_five, R.string.miwok_number_five, R.drawable.number_five,
            R.raw.number_five));
    words.add(
            new Word(R.string.number_six, R.string.miwok_number_six, R.drawable.number_six, R.raw.number_six));
    words.add(new Word(R.string.number_seven, R.string.miwok_number_seven, R.drawable.number_seven,
            R.raw.number_seven));
    words.add(new Word(R.string.number_eight, R.string.miwok_number_eight, R.drawable.number_eight,
            R.raw.number_eight));
    words.add(new Word(R.string.number_nine, R.string.miwok_number_nine, R.drawable.number_nine,
            R.raw.number_nine));
    words.add(
            new Word(R.string.number_ten, R.string.miwok_number_ten, R.drawable.number_ten, R.raw.number_ten));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_numbers);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);

            // Request audio focus so in order to play the audio file. The app needs to play a
            // short audio file, so we will request audio focus with a short amount of time
            // with AUDIOFOCUS_GAIN_TRANSIENT.
            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // We have audio focus now.

                // Create and setup the {@link MediaPlayer} for the audio resource associated
                // with the current word
                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                // Start the audio file
                mMediaPlayer.start();

                // Setup a listener on the media player, so that we can stop and release the
                // media player once the sound has finished playing.
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });

    return rootView;
}

From source file:com.example.android.gft.FamilyFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_list, container, false);

    // Create and setup the {@link AudioManager} to request audio focus
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word(R.string.family_father, R.string.miwok_family_father, R.drawable.family_father,
            R.raw.family_father));/*  w ww. j a v a 2  s.  c  o  m*/
    words.add(new Word(R.string.family_mother, R.string.miwok_family_mother, R.drawable.family_mother,
            R.raw.family_mother));
    words.add(
            new Word(R.string.family_son, R.string.miwok_family_son, R.drawable.family_son, R.raw.family_son));
    words.add(new Word(R.string.family_daughter, R.string.miwok_family_daughter, R.drawable.family_daughter,
            R.raw.family_daughter));
    words.add(new Word(R.string.family_older_brother, R.string.miwok_family_older_brother,
            R.drawable.family_older_brother, R.raw.family_older_brother));
    words.add(new Word(R.string.family_younger_brother, R.string.miwok_family_younger_brother,
            R.drawable.family_younger_brother, R.raw.family_younger_brother));
    words.add(new Word(R.string.family_older_sister, R.string.miwok_family_older_sister,
            R.drawable.family_older_sister, R.raw.family_older_sister));
    words.add(new Word(R.string.family_younger_sister, R.string.miwok_family_younger_sister,
            R.drawable.family_younger_sister, R.raw.family_younger_sister));
    words.add(new Word(R.string.family_grandmother, R.string.miwok_family_grandmother,
            R.drawable.family_grandmother, R.raw.family_grandmother));
    words.add(new Word(R.string.family_grandfather, R.string.miwok_family_grandfather,
            R.drawable.family_grandfather, R.raw.family_grandfather));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_family);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);

            // Request audio focus so in order to play the audio file. The app needs to play a
            // short audio file, so we will request audio focus with a short amount of time
            // with AUDIOFOCUS_GAIN_TRANSIENT.
            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // We have audio focus now.

                // Create and setup the {@link MediaPlayer} for the audio resource associated
                // with the current word
                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                // Start the audio file
                mMediaPlayer.start();

                // Setup a listener on the media player, so that we can stop and release the
                // media player once the sound has finished playing.
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });

    return rootView;
}

From source file:com.google.android.car.kitchensink.audio.AudioTestFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    Log.i(TAG, "onCreateView");
    init();//  ww  w . jav a  2  s. c  om
    View view = inflater.inflate(R.layout.audio, container, false);
    mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    mAudioFocusHandler = new FocusHandler((RadioGroup) view.findViewById(R.id.button_focus_request_selection),
            (Button) view.findViewById(R.id.button_audio_focus_request),
            (TextView) view.findViewById(R.id.text_audio_focus_state));
    mMediaPlay = (Button) view.findViewById(R.id.button_media_play_start);
    mMediaPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean requestFocus = true;
            boolean repeat = true;
            mMusicPlayer.start(requestFocus, repeat, AudioManager.AUDIOFOCUS_GAIN);
        }
    });
    mMediaPlayOnce = (Button) view.findViewById(R.id.button_media_play_once);
    mMediaPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMusicPlayerShort.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            // play only for 1 sec and stop
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mMusicPlayerShort.stop();
                }
            }, 1000);
        }
    });
    mMediaStop = (Button) view.findViewById(R.id.button_media_play_stop);
    mMediaStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMusicPlayer.stop();
        }
    });
    mWavPlay = (Button) view.findViewById(R.id.button_wav_play_start);
    mWavPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mWavPlayer.start(true, true, AudioManager.AUDIOFOCUS_GAIN);
        }
    });
    mWavStop = (Button) view.findViewById(R.id.button_wav_play_stop);
    mWavStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mWavPlayer.stop();
        }
    });
    mNavPlayOnce = (Button) view.findViewById(R.id.button_nav_play_once);
    mNavPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAppFocusManager == null) {
                return;
            }
            if (DBG) {
                Log.i(TAG, "Nav start");
            }
            if (!mNavGuidancePlayer.isPlaying()) {
                try {
                    mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION,
                            mOwnershipCallbacks);
                } catch (CarNotConnectedException e) {
                    Log.e(TAG, "Failed to set active focus", e);
                }
                mNavGuidancePlayer.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
                        new PlayStateListener() {
                            @Override
                            public void onCompletion() {
                                mAppFocusManager.abandonAppFocus(mOwnershipCallbacks,
                                        CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
                            }
                        });
            }
        }
    });
    mVrPlayOnce = (Button) view.findViewById(R.id.button_vr_play_once);
    mVrPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAppFocusManager == null) {
                return;
            }
            if (DBG) {
                Log.i(TAG, "VR start");
            }
            try {
                mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND,
                        mOwnershipCallbacks);
            } catch (CarNotConnectedException e) {
                Log.e(TAG, "Failed to set active focus", e);
            }
            if (!mVrPlayer.isPlaying()) {
                mVrPlayer.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, new PlayStateListener() {
                    @Override
                    public void onCompletion() {
                        mAppFocusManager.abandonAppFocus(mOwnershipCallbacks,
                                CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND);
                    }
                });
            }
        }
    });
    mSystemPlayOnce = (Button) view.findViewById(R.id.button_system_play_once);
    mSystemPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "System start");
            }
            if (!mSystemPlayer.isPlaying()) {
                // system sound played without focus
                mSystemPlayer.start(false, false, 0);
            }
        }
    });
    mNavStart = (Button) view.findViewById(R.id.button_nav_start);
    mNavStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleNavStart();
        }
    });
    mNavEnd = (Button) view.findViewById(R.id.button_nav_end);
    mNavEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleNavEnd();
        }
    });
    mVrStart = (Button) view.findViewById(R.id.button_vr_start);
    mVrStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleVrStart();
        }
    });
    mVrEnd = (Button) view.findViewById(R.id.button_vr_end);
    mVrEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleVrEnd();
        }
    });
    mRadioStart = (Button) view.findViewById(R.id.button_radio_start);
    mRadioStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioStart();
        }
    });
    mRadioEnd = (Button) view.findViewById(R.id.button_radio_end);
    mRadioEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioEnd();
        }
    });
    mSpeakerPhoneOn = (Button) view.findViewById(R.id.button_speaker_phone_on);
    mSpeakerPhoneOn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setSpeakerphoneOn(true);
        }
    });
    mSpeakerPhoneOff = (Button) view.findViewById(R.id.button_speaker_phone_off);
    mSpeakerPhoneOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setSpeakerphoneOn(false);
        }
    });
    mMicrophoneOn = (Button) view.findViewById(R.id.button_microphone_on);
    mMicrophoneOn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setMicrophoneMute(false); // Turn the microphone on.
        }
    });
    mMicrophoneOff = (Button) view.findViewById(R.id.button_microphone_off);
    mMicrophoneOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setMicrophoneMute(true); // Mute the microphone.
        }
    });

    mRejectFocus = (ToggleButton) view.findViewById(R.id.button_reject_audio_focus);
    mRejectFocus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mCarEmulator == null) {
                return;
            }
            if (!mEnableMocking.isChecked()) {
                return;
            }
            if (isChecked) {
                mCarEmulator.setAudioFocusControl(true);
            } else {
                mCarEmulator.setAudioFocusControl(false);
            }
        }
    });
    mRejectFocus.setActivated(false);
    mEnableMocking = (ToggleButton) view.findViewById(R.id.button_mock_audio);
    mEnableMocking.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mCarEmulator == null) {
                //TODO(pavelm): need to do a full switch between emulated and normal mode
                // all Car*Manager references should be invalidated.
                Toast.makeText(AudioTestFragment.this.getContext(), "Not supported yet :(", Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            if (isChecked) {
                mRejectFocus.setActivated(true);
                mCarEmulator.start();
            } else {
                mRejectFocus.setActivated(false);
                mCarEmulator.stop();
                mCarEmulator = null;
            }
        }
    });
    mMuteMedia = (ToggleButton) view.findViewById(R.id.button_mute_media);
    mMuteMedia.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            try {
                if (isChecked) {
                    mCarAudioManager.setMediaMute(true);
                } else {
                    mCarAudioManager.setMediaMute(false);
                }
            } catch (CarNotConnectedException e) {
                //ignore
            }
        }
    });
    return view;
}

From source file:com.mylovemhz.simplay.MusicService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
        stop();//from  www  .j a v a  2 s .  c  o m
        break;
    case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
        stopSelf();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        pause();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        lowerVolume();
        break;
    case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
    case AudioManager.AUDIOFOCUS_GAIN:
        restoreVolume();
        break;
    }
}

From source file:org.videolan.vlc.MediaService.java

@TargetApi(8)
private void changeAudioFocus(boolean gain) {
    if (!Util.isFroyoOrLater()) // NOP if not supported
        return;//from w ww.  j  a  v  a2 s  .c  o  m

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {

            @Override
            public void onAudioFocusChange(int focusChange) {
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (LibVLC.getExistingInstance().isPlaying()) {
                        stop();
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    if (LibVLC.getExistingInstance().isPlaying()) {
                        pause();
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    LibVLC.getExistingInstance().setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                    LibVLC.getExistingInstance().setVolume(100);
                    break;
                default:
                    break;
                }
            }
        };
    }

    Log.i(TAG, "changeAudioFocus gain=" + gain);
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (gain)
        am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    else
        am.abandonAudioFocus(audioFocusListener);

}

From source file:org.videolan.vlc.audio.AudioService.java

@TargetApi(Build.VERSION_CODES.FROYO)
private void changeAudioFocus(boolean gain) {
    if (!LibVlcUtil.isFroyoOrLater()) // NOP if not supported
        return;//from  w  w  w  . j a va 2  s . c  o m

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                LibVLC libVLC = LibVLC.getExistingInstance();
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (libVLC.isPlaying())
                        libVLC.pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    /*
                     * Lower the volume to 36% to "duck" when an alert or something
                     * needs to be played.
                     */
                    libVLC.setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    libVLC.setVolume(100);
                    break;
                }
            }
        };
    }

    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (gain)
        am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    else
        am.abandonAudioFocus(audioFocusListener);

}

From source file:com.dzt.musicplay.player.AudioService.java

@TargetApi(Build.VERSION_CODES.FROYO)
private void changeAudioFocus(boolean gain) {
    if (!LibVlcUtil.isFroyoOrLater()) // NOP if not supported
        return;/*from  w  w w . jav a  2  s  .c o m*/

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                LibVLC libVLC = LibVLC.getExistingInstance();
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (libVLC.isPlaying())
                        libVLC.pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    /*
                     * Lower the volume to 36% to "duck" when an alert or
                     * something needs to be played.
                     */
                    libVLC.setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    libVLC.setVolume(100);
                    break;
                }
            }
        };
    }

    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (gain)
        am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    else
        am.abandonAudioFocus(audioFocusListener);

}