Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by eviltenchi_84 · Oct 09, 2012 at 06:44 PM · androidiosaudiomobiledevice

Mobile - Check for headphones

Is there a way to check to see if something is plugged into the audio jack, like headphones, for instance?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eviltenchi_84 · Oct 11, 2012 at 05:49 PM 0
Share

going to take that as a no

avatar image kmeboe · Oct 11, 2012 at 06:03 PM 0
Share

Are you on iOS?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eviltenchi_84 · Oct 11, 2012 at 06:08 PM 0
Share

was hoping there was a unity solution since I wanted to do it for both ios and android.

avatar image kmeboe · Oct 11, 2012 at 06:14 PM 0
Share

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.

avatar image eviltenchi_84 · Oct 11, 2012 at 06:19 PM 0
Share

okay, thanks.

avatar image AlexHPG · Sep 26, 2014 at 09:48 AM 0
Share

Any update on this? What's the easiest way to check if earphones are plugged or unplugged?

avatar image AlexHPG · Sep 26, 2014 at 09:48 AM 0
Share

Any update on this? Did Unity develop a built-in function for detecting earphones?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges