android - How to Play audio through speaker even when headset is plugged in? -


the title of question might repeated, problem problem , causes glitches. have used below code play through speaker when headset plugged in.

audiomanager audiomanager = (audiomanager)mcontext.getsystemservice(context.audio_service);          audiomanager.setmode(audiomanager.stream_music);         audiomanager.setspeakerphoneon(true);          if(! globals.mediaplayer.isplaying()){             globals.mediaplayer.start();         } 

the above code plays audio in following ways: 1. few times, plays perfectly. 2. of times, plays looping sound in background. 3. few times, doesnot play anything.

it seems system sounds play no error when headset plugged in. example - setting ringtone plays corresponding ringtone correctly without glitches. please me understand how can play sound correctly through speaker headset plugged in.

i had similar problem solved, creating new media player if it's not playing , setting media player stream type same mode of audio manager, try this:

audiomanager audiomanager = (audiomanager)mcontext.getsystemservice(context.audio_service); audiomanager.setmode(audiomanager.stream_music); audiomanager.setspeakerphoneon(true);  if(! globals.mediaplayer.isplaying()){     mediaplayer mp = mediaplayer.create(....);     mp.setaudiostreamtype(audiomanager.stream_music);     mp.start(); } 

in case using audiomanager.mode_in_communication. sure set permission

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

in androidmanifest.xml


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -