Example usage for android.support.v4.media.session PlaybackStateCompat toKeyCode

List of usage examples for android.support.v4.media.session PlaybackStateCompat toKeyCode

Introduction

In this page you can find the example usage for android.support.v4.media.session PlaybackStateCompat toKeyCode.

Prototype

public static int toKeyCode(@MediaKeyAction long action) 

Source Link

Document

Translates a given action into a matched key code defined in KeyEvent .

Usage

From source file:androidx.media.session.MediaButtonReceiver.java

/**
 * Creates a broadcast pending intent that will send a media button event. The {@code action}
 * will be translated to the appropriate {@link KeyEvent}, and sent to the provided media
 * button receiver via the pending intent. The {@code action} should be one of the following:
 * <ul>/*from   w w w  . ja v  a  2  s  . c  o m*/
 * <li>{@link PlaybackStateCompat#ACTION_PLAY}</li>
 * <li>{@link PlaybackStateCompat#ACTION_PAUSE}</li>
 * <li>{@link PlaybackStateCompat#ACTION_SKIP_TO_NEXT}</li>
 * <li>{@link PlaybackStateCompat#ACTION_SKIP_TO_PREVIOUS}</li>
 * <li>{@link PlaybackStateCompat#ACTION_STOP}</li>
 * <li>{@link PlaybackStateCompat#ACTION_FAST_FORWARD}</li>
 * <li>{@link PlaybackStateCompat#ACTION_REWIND}</li>
 * <li>{@link PlaybackStateCompat#ACTION_PLAY_PAUSE}</li>
 * </ul>
 *
 * @param context The context of the application.
 * @param mbrComponent The full component name of a media button receiver where you want to send
 *            this intent.
 * @param action The action to be sent via the pending intent.
 * @return Created pending intent, or null if the given component name is null or the
 *         {@code action} is unsupported/invalid.
 */
public static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName mbrComponent,
        @MediaKeyAction long action) {
    if (mbrComponent == null) {
        Log.w(TAG, "The component name of media button receiver should be provided.");
        return null;
    }
    int keyCode = PlaybackStateCompat.toKeyCode(action);
    if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
        Log.w(TAG, "Cannot build a media button pending intent with the given action: " + action);
        return null;
    }
    Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    intent.setComponent(mbrComponent);
    intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
    return PendingIntent.getBroadcast(context, keyCode, intent, 0);
}