Audio stream stays on earpiece after using AudioMa

2019-08-30 14:26发布

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条回答
Melony?
2楼-- · 2019-08-30 14:50

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.

查看更多
登录 后发表回答