Android 5.0+ AudioManager setMode not working

2019-03-31 11:09发布

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.

2条回答
叼着烟拽天下
2楼-- · 2019-03-31 11:15

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.

查看更多
我命由我不由天
3楼-- · 2019-03-31 11:21

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.

查看更多
登录 后发表回答