I am using AudioTrack to play the sound I recieve through UDP sockets. I am getting a lot of noise along with the sound so I decided to use AudioManager. But AudioManager changes sound routing beyond the bounds of the application. Below is the code I am using.
m_amAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
The problem with this code is that when I close the app and start a Music Player, the sound comes from the front speaker and not the ususal back speaker and I cannot change it somehow. To resolve this issue I decided to add the following line when I am closing my app.
m_amAudioManager.setSpeakerphoneOn(true);
But with this line the problem is that when I recieve a call (normal call), by default the speaker is turned on. I really need help on this please.
The requestAudioFocus() method also requires an AudioManager. OnAudioFocusChangeListener . This listener should be created in the same activity or service that owns your media session. It implements the callback onAudioFocusChange() that your app receives when some other app acquires or abandons audio focus.
Audio Manager in android is a class that provides access to the volume and modes of the device. Android audio manager helps us adjust the volume and ringing modes of devices based on our requirements. The modes that are well known to us, that are Ringing, Vibration, Loud, Silent, etc.
First you will need to declare the User permission MODIFY_AUDIO_SETTINGS in you manifest to change the AudioManager settings.
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Before you change any settings, you must save the current AudioManager settings!
oldAudioMode = audioManager.getMode();
oldRingerMode = audioManager.getRingerMode();
isSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
Apply your Audio settings (Example)
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(true);
Then on finish, restore the settings
audioManager.setSpeakerphoneOn(isSpeakerPhoneOn);
audioManager.setMode(oldAudioMode);
audioManager.setRingerMode(oldRingerMode);
Set this when closing the app.
m_amAudioManager.setMode(AudioManager.MODE_NORMAL);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With