How to Control My Headset for My Music Player

How can i control my headset for my music player?

This document says that to receive player event notifications you need to

  1. begin playing audio
  2. be the "Now playing app"

The definition of "Now playing app" is hard to pin down, but it seems to be any app that has an active, non-mixable audio session and is playing audio (or has very recently played audio, there seems to be a brief grace period here) . One possible non-mixable audio session is:

let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayback)
try session.setActive(true)
} catch let err as NSError {
print("Error setting up non mixable audio session \(err)")
}

p.s. if you want the headset controls to work while the screen is locked (or if you want the lockscreen controls to work for that matter), you will need to add audio to Background Modes, because technically, if the screen is locked then your app has been backgrounded:

Sample Image

Android: How to control music service play/pause from bluetooth device?

As explained in the Media playback the right way talk, you must be registered as the 'preferred media app'. This is much easier when you use MediaSessionCompat as explained in the MediaSessionCompat video:

ComponentName mediaButtonReceiver =
new ComponentName(context, YourBroadcastReceiver.class);
MediaSessionCompat mediaSession =
new MediaSessionCompat(context,
tag, // Debugging tag, any string
mediaButtonReceiver,
null);
mediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(this); // a MediaSessionCompat.Callback

// This is what enables media buttons and should be called
// Immediately after getting audio focus
mediaSession.setActive(true);

Respond to Headphone events when using MediaPlayer

You should hook into the ACTION_MEDIA_BUTTON intent: Documentation.

How to control Music Player in iOS Device using BLE

You can't do this through BLE directly.

You will either need to implement the legacy AVRCP profile in your peripheral or install a companion app on the iOS device that can receive control events from your peripheral and issue the appropriate play/pause/skip commands via the AV framework.

Android, any way to controll the default music player

I haven't found anything in the API
but I want the same behavior that is
available in any headset so there must
be some API right?

No. The media player app responds to headset events.

Anyone who has an idea on how to
control the default media app?

There is no published API for the default media app AFAIK, and it is not part of the Android SDK.



Related Topics



Leave a reply



Submit