Code example for AudioManager

Methods: isSpeakerphoneOnsetSpeakerphoneOnsetWiredHeadsetOn

0
        return mAudioMode == AudioMode.DEVICE;
    } 
 
    public boolean isSpeakerOn() 
    { 
        Log.d( TAG, " - isSpeakerphoneOn: " + mAudioManager.isSpeakerphoneOn() );
        return mAudioManager.isSpeakerphoneOn();
    } 
 
    public void toggleSpeaker() 
    { 
        if ( isSpeakerOn() ) 
        { 
            turnOnHeadset(); 
        } 
        else 
        { 
            turnOnSpeaker(); 
        } 
    } 
 
    public void turnOnHeadset() 
    { 
        switchAudioMode( AudioMode.DEVICE );
    } 
 
    public void turnOnSpeaker() 
    { 
        switchAudioMode( AudioMode.SPEAKER );
    } 
 
    public void turnOnWiredHeadset() 
    { 
        if ( mWiredHeadsetEnabled )
        { 
            switchAudioMode( AudioMode.EARPIECE );
        } 
    } 
 
    public boolean wiredHeadsetEnabled() 
    { 
        return mWiredHeadsetEnabled;
    } 
 
    private void initReceiver() 
    { 
        IntentFilter intentFilter = new IntentFilter( Intent.ACTION_HEADSET_PLUG );
        mContext.registerReceiver( mReceiver, intentFilter );
    } 
 
    private void switchAudioMode( AudioMode mode )
    { 
        Log.d( TAG, " - switching audio to " + mode.toString() );
        mAudioManager.setWiredHeadsetOn( mode == AudioMode.EARPIECE );
        mAudioManager.setSpeakerphoneOn( mode == AudioMode.SPEAKER );
        mAudioMode = mode;
        if ( mListener != null )
        { 
            mListener.onAudioChange();
        }