开发者

Forcing sound output through speaker in Android

开发者 https://www.devze.com 2022-12-31 23:48 出处:网络
Is there a way in Android to force output through the phone speaker, even if a headphone is plugged into the jack? I know there has to be a way because when you are talking on the phone, you can put s

Is there a way in Android to force output through the phone speaker, even if a headphone is plugged into the jack? I know there has to be a way because when you are talking on the phone, you can put someone on speaker phone, even if there is headphon开发者_开发技巧es plugged into the jack.


You can change this system-wide using the AudioManager.setSpeakerphoneOn method.

I don't believe you can set this for a particular MediaPlayer/AudioTrack/SoundPool instance, but depending on your use case, you might actually be looking to set your audio stream type using MediaPlayer.setAudioStreamType or an equivalent for other audio playback classes.


Sound output through speaker

AudioManager m_amAudioManager = 
        AudioManager)getSystemService(Context.AUDIO_SERVICE);  

m_amAudioManager.setMode(AudioManager.MODE_NORMAL); 
m_amAudioManager.setSpeakerphoneOn(true); 


This worked for me

First, I Added permissions in Manifest.xml

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

Then, I used this code

AudioManager m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    m_amAudioManager.setMode(AudioManager.MODE_RINGTONE | AudioManager.MODE_IN_CALL);
    m_amAudioManager.setSpeakerphoneOn(true);
0

精彩评论

暂无评论...
验证码 换一张
取 消