I have some videos of which audios sound should be available only through earphone, not from inbuilt speaker or external speakers. NOTE: It should not allow External Speakers with 3.5mm jack. What is the possibility to solve it? It will be great if anybody help me out with this.
Asked
Active
Viewed 850 times
1 Answers
0
Check if the headset is plugged in:
private boolean isHeadphonesPlugged(){
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] audioDevices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
for(AudioDeviceInfo deviceInfo : audioDevices){
if(deviceInfo.getType()==AudioDeviceInfo.TYPE_WIRED_HEADPHONES
|| deviceInfo.getType()==AudioDeviceInfo.TYPE_WIRED_HEADSET){
return true;
}
}
return false;
}
In a separated thread checks if the headPhones are going to be plugged out using the method above(in that case you have to stop your mediaPlayer and apply your logic)!
For reference

Roberto Manfreda
- 2,345
- 3
- 25
- 39
-
I checked with this but this code allowing external speaker – Siraj M Oct 01 '18 at 14:45
-
i want to play audio only from earphones with 3.5 mm jack it should not play with external speakers with 3.55 jack – Siraj M Oct 01 '18 at 15:06
-
it worked when i used only TYPE_WIRED_HEADSET. Thanks – Siraj M Oct 02 '18 at 07:22