Example usage for android.media RemoteControlClient FLAG_KEY_MEDIA_PLAY_PAUSE

List of usage examples for android.media RemoteControlClient FLAG_KEY_MEDIA_PLAY_PAUSE

Introduction

In this page you can find the example usage for android.media RemoteControlClient FLAG_KEY_MEDIA_PLAY_PAUSE.

Prototype

int FLAG_KEY_MEDIA_PLAY_PAUSE

To view the source code for android.media RemoteControlClient FLAG_KEY_MEDIA_PLAY_PAUSE.

Click Source Link

Document

Flag indicating a RemoteControlClient makes use of the "play/pause" media key.

Usage

From source file:com.wojtechnology.sunami.MediaSessionCompatHelper.java

private static void ensureTransportControls(MediaSessionCompat session, PlaybackStateCompat playbackState) {
    long actions = playbackState.getActions();
    Object remoteObj = session.getRemoteControlClient();
    if (actions != 0 && remoteObj != null) {

        int transportControls = 0;

        if ((actions & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS;
        }//from w  w  w .j  a v  a 2  s . c  om

        if ((actions & PlaybackStateCompat.ACTION_REWIND) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_REWIND;
        }

        if ((actions & PlaybackStateCompat.ACTION_PLAY) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PLAY;
        }

        if ((actions & PlaybackStateCompat.ACTION_PLAY_PAUSE) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE;
        }

        if ((actions & PlaybackStateCompat.ACTION_PAUSE) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PAUSE;
        }

        if ((actions & PlaybackStateCompat.ACTION_STOP) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_STOP;
        }

        if ((actions & PlaybackStateCompat.ACTION_FAST_FORWARD) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD;
        }

        if ((actions & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_NEXT;
        }

        if ((actions & PlaybackStateCompat.ACTION_SEEK_TO) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE;
        }

        if ((actions & PlaybackStateCompat.ACTION_SET_RATING) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_RATING;
        }

        ((RemoteControlClient) remoteObj).setTransportControlFlags(transportControls);
    }
}

From source file:com.ferdi2005.secondgram.MusicPlayerService.java

@SuppressLint("NewApi")
@Override/* w w  w.ja v  a2s.c  om*/
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
        if (messageObject == null) {
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    stopSelf();
                }
            });
            return START_STICKY;
        }
        if (supportLockScreenControls) {
            ComponentName remoteComponentName = new ComponentName(getApplicationContext(),
                    MusicPlayerReceiver.class.getName());
            try {
                if (remoteControlClient == null) {
                    audioManager.registerMediaButtonEventReceiver(remoteComponentName);
                    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
                    mediaButtonIntent.setComponent(remoteComponentName);
                    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                            0);
                    remoteControlClient = new RemoteControlClient(mediaPendingIntent);
                    audioManager.registerRemoteControlClient(remoteControlClient);
                }
                remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                        | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                        | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
        createNotification(messageObject);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return START_STICKY;
}

From source file:com.b44t.messenger.MusicPlayerService.java

@SuppressLint("NewApi")
@Override/*from  w  w  w  . j  av  a 2 s. c om*/
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
        if (messageObject == null) {
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    stopSelf();
                }
            });
            return START_STICKY;
        }
        if (supportLockScreenControls) {
            ComponentName remoteComponentName = new ComponentName(getApplicationContext(),
                    MusicPlayerReceiver.class.getName());
            try {
                if (remoteControlClient == null) {
                    audioManager.registerMediaButtonEventReceiver(remoteComponentName);
                    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
                    mediaButtonIntent.setComponent(remoteComponentName);
                    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                            0);
                    remoteControlClient = new RemoteControlClient(mediaPendingIntent);
                    audioManager.registerRemoteControlClient(remoteControlClient);
                }
                remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                        | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                        | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
            } catch (Exception e) {
                FileLog.e("messenger", e);
            }
        }
        createNotification(messageObject);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return START_STICKY;
}

From source file:org.amahi.anywhere.service.AudioService.java

private void setUpAudioPlayerRemote() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName());

    Intent audioIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    audioIntent.setComponent(audioReceiver);
    PendingIntent audioPendingIntent = PendingIntent.getBroadcast(this, 0, audioIntent, 0);

    audioPlayerRemote = new RemoteControlClient(audioPendingIntent);
    audioPlayerRemote.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS);
    audioPlayerRemote.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);

    audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    audioManager.registerMediaButtonEventReceiver(audioReceiver);
    audioManager.registerRemoteControlClient(audioPlayerRemote);
}

From source file:com.dmplayer.manager.MusicPlayerService.java

@SuppressLint("NewApi")
@Override//from   w  w w.j av  a  2s.  c  o m
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        SongDetail messageObject = MediaController.getInstance().getPlayingSongDetail();
        if (messageObject == null) {
            DMPlayerUtility.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    stopSelf();
                }
            });
            return START_STICKY;
        }
        if (supportLockScreenControls) {
            ComponentName remoteComponentName = new ComponentName(getApplicationContext(),
                    MusicPlayerReceiver.class.getName());
            try {
                if (remoteControlClient == null) {
                    audioManager.registerMediaButtonEventReceiver(remoteComponentName);
                    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
                    mediaButtonIntent.setComponent(remoteComponentName);
                    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                            0);
                    remoteControlClient = new RemoteControlClient(mediaPendingIntent);
                    audioManager.registerRemoteControlClient(remoteControlClient);
                }
                remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                        | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                        | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
            } catch (Exception e) {
                Log.e("tmessages", e.toString());
            }
        }
        createNotification(messageObject);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return START_STICKY;
}

From source file:in.risysnetworks.shplayer.mediacontroller.MusicPlayerService.java

@SuppressLint("NewApi")
@Override//www.ja  va2  s.co m
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        SongDetail messageObject = MediaController.getInstance().getPlayingSongDetail();
        if (messageObject == null) {
            SHPlayerUtility.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    stopSelf();
                }
            });
            return START_STICKY;
        }
        if (supportLockScreenControls) {
            ComponentName remoteComponentName = new ComponentName(getApplicationContext(),
                    MusicPlayerReceiver.class.getName());
            try {
                if (remoteControlClient == null) {
                    audioManager.registerMediaButtonEventReceiver(remoteComponentName);
                    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
                    mediaButtonIntent.setComponent(remoteComponentName);
                    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                            0);
                    remoteControlClient = new RemoteControlClient(mediaPendingIntent);
                    audioManager.registerRemoteControlClient(remoteControlClient);
                }
                remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                        | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                        | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
            } catch (Exception e) {
                Log.e("tmessages", e.toString());
            }
        }
        createNotification(messageObject);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return START_STICKY;
}

From source file:com.goftagram.telegram.messenger.MusicPlayerService.java

@SuppressLint("NewApi")
@Override//w  w  w.  ja  va2  s  .c o  m
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
        if (messageObject == null) {
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    stopSelf();
                }
            });
            return START_STICKY;
        }
        if (supportLockScreenControls) {
            ComponentName remoteComponentName = new ComponentName(getApplicationContext(),
                    MusicPlayerReceiver.class.getName());
            try {
                if (remoteControlClient == null) {
                    audioManager.registerMediaButtonEventReceiver(remoteComponentName);
                    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
                    mediaButtonIntent.setComponent(remoteComponentName);
                    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                            0);
                    remoteControlClient = new RemoteControlClient(mediaPendingIntent);
                    audioManager.registerRemoteControlClient(remoteControlClient);
                }
                remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                        | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                        | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
        createNotification(messageObject);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return START_STICKY;
}

From source file:co.codecrunch.musicplayerlite.manager.MusicPlayerService.java

@SuppressLint("NewApi")
@Override/*  www  . j  av a2 s . c  om*/
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        SongDetail messageObject = MediaController.getInstance().getPlayingSongDetail();
        if (messageObject == null) {
            MusicPlayerUtility.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    stopSelf();
                }
            });
            return START_STICKY;
        }
        if (supportLockScreenControls) {
            ComponentName remoteComponentName = new ComponentName(getApplicationContext(),
                    MusicPlayerReceiver.class.getName());
            try {
                if (remoteControlClient == null) {
                    audioManager.registerMediaButtonEventReceiver(remoteComponentName);
                    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
                    mediaButtonIntent.setComponent(remoteComponentName);
                    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                            0);
                    remoteControlClient = new RemoteControlClient(mediaPendingIntent);
                    audioManager.registerRemoteControlClient(remoteControlClient);
                }
                remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                        | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                        | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                        | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
            } catch (Exception e) {
                Log.e("tmessages", e.toString());
            }
        }
        createNotification(messageObject);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return START_STICKY;
}

From source file:com.andreadec.musicplayer.MusicService.java

/**
 * Called when the service is created./*from   w  w  w  . j  ava2 s . c  o  m*/
 */
@Override
public void onCreate() {
    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MusicServiceWakelock");

    // Initialize the telephony manager
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    phoneStateListener = new MusicPhoneStateListener();
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Initialize pending intents
    quitPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.andreadec.musicplayer.quit"), 0);
    previousPendingIntent = PendingIntent.getBroadcast(this, 0,
            new Intent("com.andreadec.musicplayer.previous"), 0);
    playpausePendingIntent = PendingIntent.getBroadcast(this, 0,
            new Intent("com.andreadec.musicplayer.playpause"), 0);
    nextPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.andreadec.musicplayer.next"), 0);
    pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Read saved user preferences
    preferences = PreferenceManager.getDefaultSharedPreferences(this);

    // Initialize the media player
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK); // Enable the wake lock to keep CPU running when the screen is switched off

    shuffle = preferences.getBoolean(Constants.PREFERENCE_SHUFFLE, Constants.DEFAULT_SHUFFLE);
    repeat = preferences.getBoolean(Constants.PREFERENCE_REPEAT, Constants.DEFAULT_REPEAT);
    repeatAll = preferences.getBoolean(Constants.PREFERENCE_REPEATALL, Constants.DEFAULT_REPEATALL);
    try { // This may fail if the device doesn't support bass boost
        bassBoost = new BassBoost(1, mediaPlayer.getAudioSessionId());
        bassBoost.setEnabled(
                preferences.getBoolean(Constants.PREFERENCE_BASSBOOST, Constants.DEFAULT_BASSBOOST));
        setBassBoostStrength(preferences.getInt(Constants.PREFERENCE_BASSBOOSTSTRENGTH,
                Constants.DEFAULT_BASSBOOSTSTRENGTH));
        bassBoostAvailable = true;
    } catch (Exception e) {
        bassBoostAvailable = false;
    }
    try { // This may fail if the device doesn't support equalizer
        equalizer = new Equalizer(1, mediaPlayer.getAudioSessionId());
        equalizer.setEnabled(
                preferences.getBoolean(Constants.PREFERENCE_EQUALIZER, Constants.DEFAULT_EQUALIZER));
        setEqualizerPreset(
                preferences.getInt(Constants.PREFERENCE_EQUALIZERPRESET, Constants.DEFAULT_EQUALIZERPRESET));
        equalizerAvailable = true;
    } catch (Exception e) {
        equalizerAvailable = false;
    }
    random = new Random(System.nanoTime()); // Necessary for song shuffle

    shakeListener = new ShakeListener(this);
    if (preferences.getBoolean(Constants.PREFERENCE_SHAKEENABLED, Constants.DEFAULT_SHAKEENABLED)) {
        shakeListener.enable();
    }

    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); // Start listen for telephony events

    // Inizialize the audio manager
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mediaButtonReceiverComponent = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
    audioManager.registerMediaButtonEventReceiver(mediaButtonReceiverComponent);
    audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    // Initialize remote control client
    if (Build.VERSION.SDK_INT >= 14) {
        icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(mediaButtonReceiverComponent);
        PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
                mediaButtonIntent, 0);

        remoteControlClient = new RemoteControlClient(mediaPendingIntent);
        remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
        audioManager.registerRemoteControlClient(remoteControlClient);
    }

    updateNotificationMessage();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.andreadec.musicplayer.quit");
    intentFilter.addAction("com.andreadec.musicplayer.previous");
    intentFilter.addAction("com.andreadec.musicplayer.previousNoRestart");
    intentFilter.addAction("com.andreadec.musicplayer.playpause");
    intentFilter.addAction("com.andreadec.musicplayer.next");
    intentFilter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("com.andreadec.musicplayer.quit")) {
                sendBroadcast(new Intent("com.andreadec.musicplayer.quitactivity"));
                stopSelf();
                return;
            } else if (action.equals("com.andreadec.musicplayer.previous")) {
                previousItem(false);
            } else if (action.equals("com.andreadec.musicplayer.previousNoRestart")) {
                previousItem(true);
            } else if (action.equals("com.andreadec.musicplayer.playpause")) {
                playPause();
            } else if (action.equals("com.andreadec.musicplayer.next")) {
                nextItem();
            } else if (action.equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
                if (preferences.getBoolean(Constants.PREFERENCE_STOPPLAYINGWHENHEADSETDISCONNECTED,
                        Constants.DEFAULT_STOPPLAYINGWHENHEADSETDISCONNECTED)) {
                    pause();
                }
            }
        }
    };
    registerReceiver(broadcastReceiver, intentFilter);

    if (!isPlaying()) {
        loadLastSong();
    }

    startForeground(Constants.NOTIFICATION_MAIN, notification);
}

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  av  a 2s  .c  o  m*/
        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);
    }
}