- Home /
Mobile - Check for headphones
Is there a way to check to see if something is plugged into the audio jack, like headphones, for instance?
Answer by kmeboe · Oct 11, 2012 at 06:06 PM
If you're on iOS, you can use Objective C to do what you want. Here's one example: http://stackoverflow.com/questions/3575463/detecting-if-headphones-are-plugged-into-iphone
Calling into Objective C from Unity can be tricky. Here's one great reference: http://www.tinytimgames.com/2010/01/10/the-unityobjective-c-divide/
Good luck.
was hoping there was a unity solution since I wanted to do it for both ios and android.
Here's one showing how to do it on android:
http://stackoverflow.com/questions/5264634/android-headphone-detection
I know it's annoying to have to solve it separately for each OS, but I don't see any built-in way to do it through Unity.
Any update on this? What's the easiest way to check if earphones are plugged or unplugged?
Any update on this? Did Unity develop a built-in function for detecting earphones?
Answer by tuberains · Dec 22, 2016 at 11:03 AM
First, to update this thread a little, there is still no easy way to do this...
Making a headphone detector for Android i discovered some pitfalls, so here is it a little more detailed:
1. Build an Android library detecting a headset
With AndroidStudio, start a new project or module from the library template. Apply the same targetSDK and minSDK values as in Unity, as Unity would fail on merging the AndroidManifests otherwise. Now there are two things of doing it. First is to register a BroadcastReceiver on the intent "andorid.intent.action.HEADPHONE_PLUGGED", unfortunately this will only be fired when a wired headphone is plugged (kinda outdated), and it must be actively registered in the code which means declaring it in the AndroidManifest.xml is not enough. Second is to poll on some methods of the AudioManager:
// initialize this once // get context from unity
AudioManager audioManager = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
// poll on these
boolean headsetConnected = !audioManager.isSpeakerphoneOn()
&& (audioManager.isBluetoothA2dpOn()
|| audioManager.isBluetoothScoOn()
|| audioManager.isWiredHeadsetOn());
Be shure to use some kind of factory method to initialize this (providing the Context of the actual Unity game app). Build the lib. For Android Studio the output is in build/outputs/aar.
2. Add the .aar lib to your Unity project
Copy the .aar package to any directory in the Unity project. I recommend using Assets/Plugins/Android. Such added, such wow. If you are using the intent way, add
<category android:name="com.google.intent.action.HEADSET_PLUG"/>
to the section in all AndroidManifest in the project.
3. Access the lib from your Unity C# code
AndroidJavaClass libClass; // analog to java Class objects - the class of your lib
AndroidJavaObject libInstance; // instance of your lib
libClass = new AndroidJavaObject("fully.qualified.class.name.of.your.lib");
libObject = libClass.CallStatic<AndroidJavaObject>("factoryMethodName", getContext());
// access this call in a loop to get continuous detection
bool isConnected = libObject.Call<bool> ("detectConnectedHeadsetMethod");
private static AndroidJavaObject getContext () {
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
return activity.Call<AndroidJavaObject>("getApplicationContext");
}
Be shure to throttle the dection method invocation when accessing in a loop as the call is really slow.
I used macOS Sierra, Unity 5.4.1f1 and AndroidStudio 2.2.3 but i haven't discovered any version issues.
Your answer
Follow this Question
Related Questions
Gap in audio playback on Android 1 Answer
Mobile device screen sizes 4 Answers
Mobile aspect ratio and scaling - Use 16:10 or 16:9 base for full screen background ? 0 Answers
Using the Volume Control Buttons On Mobile Devices 0 Answers
Scriptable object doesn't load in the build of the project 1 Answer