Audio stream stays on earpiece after using AudioManager

╄→гoц情女王★ 提交于 2019-12-25 03:08:50

问题


After I use the Android AudioManager to set SCO ON and speakerphone ON, then I return it back to where it started, the media stream will then stay on earpiece instead of the phones speaker. Here is where I make the changes to route a stream through the SCO speakerphone (which all works great)

    if (am2.isBluetoothScoAvailableOffCall()) {
                            am2.startBluetoothSco();
                        }
                        if(!am2.isSpeakerphoneOn()){
                            speakerPhoneWasOn = false;
                            am2.setSpeakerphoneOn(true);
                        }
                        myHash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
                                String.valueOf(AudioManager.STREAM_VOICE_CALL));
                        am2.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
                                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

Then I try to change it all back after my message is read (I am using TTS)

if(SMSstream == 1){
                if (am2.isBluetoothScoAvailableOffCall()) {
                    am2.stopBluetoothSco();

                }
                if(!speakerPhoneWasOn){
                    am2.setSpeakerphoneOn(false);
                }


            }
            else{

            }
            am2.setMode(OLD_AUDIO_MODE);
            am2.abandonAudioFocus(null);

But it stays with the phone earpiece instead of the speaker until I reboot. I saw a few post that had the opposite problem but none with this issue. I have a Droid 3 with Android 2.3.4.

You can see the whole project and source code here: http://code.google.com/p/a2dpvolume/


回答1:


OK, I finally fixed this problem. Its not pretty. There is some sort of bug in the AudioManager I believe. Even after abandoning focus it would leave the device streams in a mess. Unless the last TTS was read over the music stream just before abandoning focus, it would route streams wrong, mute streams, etc. So, I just have it read a single period if a SMS had been read over any stream except the music stream before abandoning focus. I also cleaned up the order I called things in, and fixed some of the phone mode, etc after reading a TTS. Strangely it seems to work well.



来源:https://stackoverflow.com/questions/7290489/audio-stream-stays-on-earpiece-after-using-audiomanager

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