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.
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:
#if UNITY_ANDROID
try
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject audioManager = activity.Call<AndroidJavaObject>("getSystemService", "audio");
bool resBef = audioManager.Call<Boolean>("isSpeakerphoneOn");
audioManager.Call("setSpeakerphoneOn", true);
audioManager.Call("setMode", 2);
bool res = audioManager.Call<Boolean>("isSpeakerphoneOn");
int mode2 = audioManager.Call<Int32>("getMode");
txtMicCaps.text += " " + resBef + " " + res + " " + mode2;
}
catch (Exception ex)
{
txtException.text = ex.ToString();
}
#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?
2 Likes
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.
- Add permissions to the AndroidManifest.xml file: Create a folder in your unity project “Assets/Plugins/Android”
- 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”
- Edit the cloned AndroidManifest.xml and add the following line:
- 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)
public void ToggleMode()
{
#if UNITY_ANDROID
try
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject audioManager = activity.Call<AndroidJavaObject>("getSystemService", "audio");
int mode2 = audioManager.Call<Int32>("getMode");
if(mode2 == 3)
{
mode2 = 0;
}
else
{
mode2 = 3;
}
audioManager.Call("setMode", mode2);
mode2 = audioManager.Call<Int32>("getMode");
Debug.Log("Mode is now set to: " + mode2);
}
catch (Exception ex)
{
Debug.Log(ex.toString());
}
#endif
}
Toggle Speakers: (Need to set to false for our use case)
public void ToggleSpeakerMode()
{
#if UNITY_ANDROID
try
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject audioManager = activity.Call<AndroidJavaObject>("getSystemService", "audio");
bool isSpeakers = audioManager.Call<Boolean>("isSpeakerphoneOn");
audioManager.Call("setSpeakerphoneOn", !isSpeakers);
bool res = audioManager.Call<Boolean>("isSpeakerphoneOn");
Debug.Log("Speakers are now set to: " + res);
}
catch (Exception ex)
{
Debug.Log(ex.toString());
}
#endif
}
6 Likes