android plug in headphones and use the speaker to play music

You can also choose to use the speaker to play music when the headset is plugged in, which is how the ringtone is used. But only MediaPlayer can play audio files.
Playing with AudioTrack.write won't work. It stands to reason that the AudioRecord and AudioTrack classes are closer to the bottom layer than MediaRecorder mediaPlayer, and should work.

AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);  
audioManager.setMicrophoneMute(false);          
audioManager.setSpeakerphoneOn(true);//Use the speaker to play, even if the headset is plugged in  
//setVolumeControlStream(AudioManager.STREAM_MUSIC);//Control the size of the sound  
audioManager.setMode(AudioManager.STREAM_MUSIC);

 

Switching between speaker mode and handset mode requires additional permissions:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326998586&siteId=291194637