[General Questions and Answers] Neil_Armstrong_:
OxygenOS is not CyanogenMod ehe, stock OOS doesn't provide this feature, i know I can use Frinky or Button Mapper but i'd like to find a more permanent solution, something that doesn't require an app

I found this while browsing LOS sources on github:

<h2 id="PlaybackControls">Use Hardware Playback Control Keys to Control Your App’s Audio

Playback</h2>

<p>Media playback buttons such as play, pause, stop, skip, and previous are available on some

handsets and many connected or wireless headsets. Whenever a user presses one of these hardware

keys, the system broadcasts an intent with the {@link android.content.Intent#ACTION_MEDIA_BUTTON}

action.</p>

<p>To respond to media button clicks, you need to register a {@link

android.content.BroadcastReceiver} in your manifest that listens for this action broadcast as shown

below.</p>

<pre>

<receiver android:name=".RemoteControlReceiver">

<intent-filter>

<action android:name="android.intent.action.MEDIA_BUTTON" />

</intent-filter>

</receiver>

</pre>

<p>The receiver implementation itself needs to extract which key was pressed to cause the broadcast.

The {@link android.content.Intent} includes this under the {@link

android.content.Intent#EXTRA_KEY_EVENT} key, while the {@link android.view.KeyEvent} class includes

a list {@code KEYCODE_MEDIA_*} static constants that represents each of the possible media

buttons, such as {@link android.view.KeyEvent#KEYCODE_MEDIA_PLAY_PAUSE} and {@link

android.view.KeyEvent#KEYCODE_MEDIA_NEXT}.</p>

<p>The following snippet shows how to extract the media button pressed and affects the media playback accordingly.</p>

<pre>

public class RemoteControlReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {

KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {

// Handle key press.

}

}

}

}

</pre>

<p>Because multiple applications might want to listen for media button presses, you must

also programmatically control when your app should receive media button press events.</p>

<p>The following code can be used within your app to register and de-register your media button

event receiver using the {@link android.media.AudioManager}. When registered, your broadcast

receiver is the exclusive receiver of all media button broadcasts.<p>

<pre>

AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);

...

// Start listening for button presses

am.registerMediaButtonEventReceiver(RemoteControlReceiver);

...

// Stop listening for button presses

am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);

</pre>

<p>Typically, apps should unregister most of their receivers whenever they become inactive or

invisible (such as during the {@link android.app.Activity#onStop onStop()} callback). However, it’s

not that simple for media playback apps—in fact, responding to media playback buttons is most

important when your application isn’t visible and therefore can’t be controlled by the on-screen

UI.</p>

<p>A better approach is to register and unregister the media button event receiver when your

application gains and loses the audio focus. This is covered in detail in the next lesson.</p>