Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use AudioTrack with AudioManager setSpeakerphoneOn

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.

like image 567
SoH Avatar asked Jan 18 '13 17:01

SoH


People also ask

How do I request audio focus on Android?

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.

What is an audio manager?

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.


2 Answers

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" />
  1. Before you change any settings, you must save the current AudioManager settings!

    oldAudioMode = audioManager.getMode();
    oldRingerMode = audioManager.getRingerMode();
    isSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
    
  2. Apply your Audio settings (Example)

    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    audioManager.setMode(AudioManager.MODE_NORMAL);
    audioManager.setSpeakerphoneOn(true);
    
  3. Then on finish, restore the settings

    audioManager.setSpeakerphoneOn(isSpeakerPhoneOn);
    audioManager.setMode(oldAudioMode);
    audioManager.setRingerMode(oldRingerMode);
    
like image 105
TouchBoarder Avatar answered Sep 21 '22 20:09

TouchBoarder


Set this when closing the app.

m_amAudioManager.setMode(AudioManager.MODE_NORMAL);

like image 25
WoodsLink Avatar answered Sep 21 '22 20:09

WoodsLink