Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Help please, Recording mode, earpiece mode and not speaker mode

Discussion in 'Audio & Video' started by kantzuker, Mar 26, 2017.

  1. kantzuker

    kantzuker

    Joined:
    Oct 31, 2016
    Posts:
    3
    Hello Devs,
    I am looking for a way to set the microphone for android / IOS devices,
    so that the recording wont be In a speaker mode.
    I am making an app which uses the Microphone and I need to set the exposure for noise to the bear minimum.
    However when I use Microphone.Start() function the microphone starts in "Speaker Mode" which adds a lot of un wanted extra noise to the recording. ( I tested it on android device ).

    Is there a way to switch from speaker mode to "earpieace mode" or something like that?

    Thanks a lot.
     
  2. kantzuker

    kantzuker

    Joined:
    Oct 31, 2016
    Posts:
    3
    Hello,
    I did a lot of digging and found out a solution using android ndk however when I try to invoke the android functions it does not do anything, I use the following code:

    Code (CSharp):
    1. #if UNITY_ANDROID
    2.         try
    3.         {
    4.            
    5.             AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    6.             AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    7.             AndroidJavaObject audioManager = activity.Call<AndroidJavaObject>("getSystemService", "audio");
    8.             bool resBef = audioManager.Call<Boolean>("isSpeakerphoneOn");
    9.             audioManager.Call("setSpeakerphoneOn", true);
    10.             audioManager.Call("setMode", 2);
    11.             bool res = audioManager.Call<Boolean>("isSpeakerphoneOn");
    12.             int mode2 = audioManager.Call<Int32>("getMode");
    13.            
    14.             txtMicCaps.text +=  " " + resBef + " " + res + " " + mode2;
    15.         }
    16.         catch (Exception ex)
    17.         {
    18.             txtException.text = ex.ToString();
    19.         }
    20. #endif
    I tested the above code on my samsung galaxy, I am setting setSpeakerphoneOn to true, setMode 2 (IN_CALL_MODE).

    After that I check if my changes applied but I get the following values:

    resBef: false
    res: false
    mode2: 0


    which mean my changes did not take place, anyone have an idea why?
     
    bahar_Shanab and kvicksilv3r like this.
  3. kantzuker

    kantzuker

    Joined:
    Oct 31, 2016
    Posts:
    3
    SOLVED!

    As part of my research on this question, I have seen many unanswered threads for the same question so to save some hours of searching, Here is a step by step Instructions to make the microphone and earpiece work as if you are speaking on the phone, on regular mode ( Not speaker Mode )
    The Solution Works only for android devices, IOS devices probably would need a plugin to make it work.
    1. Add permissions to the AndroidManifest.xml file: Create a folder in your unity project "Assets/Plugins/Android"
    2. Copy unity's default Android Manifest File into the above folder - you can find the default manifest file under: "<Unity_Installed_Folder>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk"
    3. Edit the cloned AndroidManifest.xml and add the following line: <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    4. Use the code below to set toggle between modes.

    Toggle Mode between Communication (3) Mode To Normal Mode (0): (Need to set to communication mode (3) in our use case)

    Code (CSharp):
    1. public void ToggleMode()
    2.     {
    3. #if UNITY_ANDROID
    4.         try
    5.         {
    6.             AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    7.             AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    8.             AndroidJavaObject audioManager = activity.Call<AndroidJavaObject>("getSystemService", "audio");
    9.             int mode2 = audioManager.Call<Int32>("getMode");
    10.             if(mode2 == 3)
    11.             {
    12.                 mode2 = 0;
    13.             }
    14.             else
    15.             {
    16.                 mode2 = 3;
    17.             }
    18.  
    19.             audioManager.Call("setMode", mode2);
    20.             mode2 = audioManager.Call<Int32>("getMode");
    21.  
    22.             Debug.Log("Mode is now set to: " + mode2);
    23.         }
    24.         catch (Exception ex)
    25.         {
    26.              Debug.Log(ex.toString());
    27.         }
    28. #endif
    29.     }

    Toggle Speakers: (Need to set to false for our use case)

    Code (CSharp):
    1.     public void ToggleSpeakerMode()
    2.     {
    3. #if UNITY_ANDROID
    4.         try
    5.         {
    6.             AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    7.             AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    8.             AndroidJavaObject audioManager = activity.Call<AndroidJavaObject>("getSystemService", "audio");
    9.             bool isSpeakers = audioManager.Call<Boolean>("isSpeakerphoneOn");
    10.             audioManager.Call("setSpeakerphoneOn", !isSpeakers);
    11.             bool res = audioManager.Call<Boolean>("isSpeakerphoneOn");
    12.          
    13.             Debug.Log("Speakers are now set to: " + res);
    14.         }
    15.         catch (Exception ex)
    16.         {
    17.             Debug.Log(ex.toString());
    18.         }
    19. #endif
    20.     }
     
    Last edited: Mar 30, 2017
unityunity