简体   繁体   中英

Android 5.0+ AudioManager setMode not working

i am working on AudioManager which is a Android SystemService. with Android System 5.0+ , i encounter a problem which AudioManager the setMode method is not working .

i through a test , Android M, Lollipop.. 5.0+ version , AudioManager setMode is not working . example :

public void initAudioImageIcon(boolean initLoad) {
    boolean isAudioHeaderMode = IMSharedPreferences.getBooleanExtra(this, IMSPConstant.SP_NAME_MESSAGE,
            IMSPConstant.SP_KEY_AUDIO_HEADER_MODE);


    if (isAudioHeaderMode) {
        mAudioHanderMode.setVisibility(View.VISIBLE);
        // audioManager.setMode(AudioManager.MODE_IN_CALL) , but android system 5.0+ no any change, getMode() == AudioManager.MODE_NORMAL
        setAudioMode(AudioManager.MODE_IN_CALL);
        audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
        if (!initLoad) {
            showAudioModePrompt(this.getText(R.string.im_audio_in_call), 1000);
        }
    } else {
        mAudioHanderMode.setVisibility(View.GONE);
        setAudioMode(AudioManager.MODE_NORMAL);
        if (!initLoad) {
            showAudioModePrompt(this.getText(R.string.im_audio_in_speeker), 1000);
        }
    }
}

but Android 3.0+,4.0+ is ok ,only 5.0+ . so ,i don`t know where happen mistakes.

With audio mode set to :

setMode(AudioManager.MODE_IN_COMMUNICATION);
setSpeakerphoneOn(false);

while my audio stream is set to STREAM_MUSIC I can easily route audio to earpiece. I have tested it myself in AOSP Lollipop code.

Here in the question you have never mentioned about your stream type. Do set your stream to STREAM_MUSIC or STREAM_VOICE_CALL and the code should work for you too.

In android Lollipop setAudioMode(AudioManager.MODE_IN_CALL) is restricted. It can be used only by system application with MODIFY_PHONE_STATE permission. However you can use MODE_IN_COMMUNICATION and MODE_NORMAL in normal applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM