0

There is a media player when playing music if the music starts to pull the headphones played with the dynamics of how to do that would be when removing the headphones media player stopped playing

user3064772
  • 57
  • 2
  • 9

1 Answers1

1

You can use the AudioManager to detect, wether a headphone is connected or not like this:

AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
Log.i("HEADPHONE", "Headphone is plugged in: " + am.isWiredHeadsetOn());

To use it, you have to add the following permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

Observing the state of the headphone is already described here or like this taken from here:

public class MyActivity extends Activity {

   // ...

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
       HeadsetStateReceiver receiver = new HeadsetStateReceiver();
       registerReceiver( receiver, receiverFilter );
   }

   // ...

}
Community
  • 1
  • 1
alex
  • 5,516
  • 2
  • 36
  • 60