Android 5.0+ AudioManager setMode not working

我的梦境 提交于 2019-12-05 02:32:53

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/31871328/android-5-0-audiomanager-setmode-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!