Example usage for android.content Intent ACTION_MEDIA_BUTTON

List of usage examples for android.content Intent ACTION_MEDIA_BUTTON

Introduction

In this page you can find the example usage for android.content Intent ACTION_MEDIA_BUTTON.

Prototype

String ACTION_MEDIA_BUTTON

To view the source code for android.content Intent ACTION_MEDIA_BUTTON.

Click Source Link

Document

Broadcast Action: The "Media Button" was pressed.

Usage

From source file:org.videolan.vlc.PlaybackService.java

@Override
public void onCreate() {
    super.onCreate();

    if (!VLCInstance.testCompatibleCPU(this)) {
        stopSelf();//from  w w  w .j  ava 2  s. c  om
        return;
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    mDetectHeadset = prefs.getBoolean("enable_headset_detection", true);

    mMediaListPlayer = MediaWrapperListPlayer.getInstance();

    mCallback = new HashMap<IPlaybackServiceCallback, Integer>();
    mCurrentIndex = -1;
    mPrevIndex = -1;
    mNextIndex = -1;
    mPrevious = new Stack<Integer>();
    mEventHandler = EventHandler.getInstance();
    mRemoteControlClientReceiverComponent = new ComponentName(BuildConfig.APPLICATION_ID,
            RemoteControlClientReceiver.class.getName());

    // Make sure the audio player will acquire a wake-lock while playing. If we don't do
    // that, the CPU might go to sleep while the song is playing, causing playback to stop.
    PowerManager pm = (PowerManager) VLCApplication.getAppContext().getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    IntentFilter filter = new IntentFilter();
    filter.setPriority(Integer.MAX_VALUE);
    filter.addAction(ACTION_REMOTE_BACKWARD);
    filter.addAction(ACTION_REMOTE_PLAYPAUSE);
    filter.addAction(ACTION_REMOTE_PLAY);
    filter.addAction(ACTION_REMOTE_PAUSE);
    filter.addAction(ACTION_REMOTE_STOP);
    filter.addAction(ACTION_REMOTE_FORWARD);
    filter.addAction(ACTION_REMOTE_LAST_PLAYLIST);
    filter.addAction(ACTION_WIDGET_INIT);
    filter.addAction(Intent.ACTION_HEADSET_PLUG);
    filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    filter.addAction(VLCApplication.SLEEP_INTENT);
    filter.addAction(VLCApplication.INCOMING_CALL_INTENT);
    filter.addAction(VLCApplication.CALL_ENDED_INTENT);
    registerReceiver(serviceReceiver, filter);

    final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    boolean stealRemoteControl = pref.getBoolean("enable_steal_remote_control", false);

    if (!AndroidUtil.isFroyoOrLater() || stealRemoteControl) {
        /* Backward compatibility for API 7 */
        filter = new IntentFilter();
        if (stealRemoteControl)
            filter.setPriority(Integer.MAX_VALUE);
        filter.addAction(Intent.ACTION_MEDIA_BUTTON);
        mRemoteControlClientReceiver = new RemoteControlClientReceiver();
        registerReceiver(mRemoteControlClientReceiver, filter);
    }
    try {
        getPackageManager().getPackageInfo("com.getpebble.android", PackageManager.GET_ACTIVITIES);
        mPebbleEnabled = true;
    } catch (PackageManager.NameNotFoundException e) {
        mPebbleEnabled = false;
    }
}

From source file:org.videolan.vlc.audio.AudioService.java

/**
 * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
 * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
 *//*  www  .  j  a v  a 2 s  .  co  m*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = VLCApplication.getAppContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

    if (LibVlcUtil.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (LibVlcUtil.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}

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   www.j  av  a  2  s .  co  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);
}

From source file:com.marvin.rocklock.RockLockActivity.java

@Override
public void onResume() {
    super.onResume();
    // Poke application
    mPoked = false;//from  w w w  .  j a  v  a  2s  .co  m
    new Thread(new PokeWatcher()).start();
    // Restore music
    mPlayer.getNavigator().restore();
    // Register headphone receiver
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_BUTTON);
    filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
    registerReceiver(mMediaButtonReceiver, filter);
}

From source file:com.dzt.musicplay.player.AudioService.java

/**
 * Set up the remote control and tell the system we want to be the default
 * receiver for the MEDIA buttons/*from   ww  w  . ja  v a  2s  .  c o m*/
 * 
 * @see http 
 *      ://android-developers.blogspot.fr/2010/06/allowing-applications-to
 *      -play-nicer.html
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = getApplicationContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

    if (LibVlcUtil.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (LibVlcUtil.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}

From source file:io.radio.streamer.MainActivity.java

@TargetApi(14)
private void initializeRemoteControls() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        ComponentName eventReceiver = new ComponentName(getPackageName(),
                RemoteControlReceiver.class.getName());

        audioManager.registerMediaButtonEventReceiver(eventReceiver);
        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(eventReceiver);
        PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
                mediaButtonIntent, 0);/*from   w  w w.  j  a  v a2 s .  c om*/
        remoteControlClient = new RemoteControlClient(mediaPendingIntent);
        remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
        remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
        audioManager.registerRemoteControlClient(remoteControlClient);
    }
}

From source file:org.videolan.vlc.PlaybackService.java

/**
 * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
 * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
 *//* www  .jav a  2s .c o  m*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = VLCApplication.getAppContext();
    AudioManager audioManager = (AudioManager) VLCApplication.getAppContext().getSystemService(AUDIO_SERVICE);

    if (AndroidUtil.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (AndroidUtil.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}

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

/**
 * @hide/*from   w ww  .  j a  va 2  s .  com*/
 */
@RestrictTo(LIBRARY)
public static ComponentName getMediaButtonReceiverComponent(Context context) {
    Intent queryIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    queryIntent.setPackage(context.getPackageName());
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0);
    if (resolveInfos.size() == 1) {
        ResolveInfo resolveInfo = resolveInfos.get(0);
        return new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
    } else if (resolveInfos.size() > 1) {
        Log.w(TAG, "More than one BroadcastReceiver that handles " + Intent.ACTION_MEDIA_BUTTON
                + " was found, returning null.");
    }
    return null;
}

From source file:org.easyaccess.phonedialer.CallStateService.java

@Override
public void onShake(float force) {
    if (Utils.ringing == 1) {
        // answer call
        Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
        buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
        cxt.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
    }//w  w  w  .  ja  v  a 2  s .c o m
}

From source file:com.Duo.music.player.Services.AudioPlaybackService.java

/**
 * Initializes remote control clients for this service session. 
 * Currently used for lockscreen controls.
 *///from   ww  w. java  2  s .  co  m
public void initRemoteControlClient() {
    if (mRemoteControlClientCompat == null) {
        Intent remoteControlIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        remoteControlIntent.setComponent(mMediaButtonReceiverComponent);

        mRemoteControlClientCompat = new RemoteControlClientCompat(
                PendingIntent.getBroadcast(mContext, 0, remoteControlIntent, 0));
        RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat);

    }

    mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    mRemoteControlClientCompat.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
            | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
            | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS);

}