Example usage for android.media RemoteControlClient FLAG_KEY_MEDIA_POSITION_UPDATE

List of usage examples for android.media RemoteControlClient FLAG_KEY_MEDIA_POSITION_UPDATE

Introduction

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

Prototype

int FLAG_KEY_MEDIA_POSITION_UPDATE

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

Click Source Link

Document

Flag indicating a RemoteControlClient can receive changes in the media playback position through the OnPlaybackPositionUpdateListener interface.

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;
        }//  w  ww  .j ava 2  s .c o  m

        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.namelessdev.mpdroid.NotificationService.java

/**
 * A simple method to enable lock screen seeking on 4.3 and higher.
 *
 * @param controlFlags The control flags you set beforehand, so that we can add our
 *                     required flag/*from  w  w  w. j av  a  2s .c  o  m*/
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void enableSeeking(final int controlFlags) {
    mRemoteControlClient
            .setTransportControlFlags(controlFlags | RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE);

    /* Allows Android to show the song position */
    mRemoteControlClient
            .setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() {
                /**
                 * Android's callback that queries us for the elapsed time
                 * Here, we are guessing the elapsed time using the last time we
                 * updated the elapsed time and its value at the time.
                 *
                 * @return The guessed song position
                 */
                @Override
                public long onGetPlaybackPosition() {
                    /**
                     * If we don't know the position, return
                     * a negative value as per the API spec.
                     */
                    long result = -1L;

                    if (lastStatusRefresh > 0L) {
                        result = lastKnownElapsed + new Date().getTime() - lastStatusRefresh;
                    }
                    return result;
                }
            });

    /* Allows Android to seek */
    mRemoteControlClient
            .setPlaybackPositionUpdateListener(new RemoteControlClient.OnPlaybackPositionUpdateListener() {
                /**
                 * Android's callback for when the user seeks using the remote
                 * control.
                 *
                 * @param newPositionMs The position in MS where we should seek
                 */
                @Override
                public void onPlaybackPositionUpdate(final long newPositionMs) {
                    try {
                        app.oMPDAsyncHelper.oMPD.seek(newPositionMs / DateUtils.SECOND_IN_MILLIS);
                        mRemoteControlClient.setPlaybackState(getRemoteState(getStatus()), newPositionMs, 1.0f);
                    } catch (final MPDServerException e) {
                        Log.e(TAG, "Could not seek", e);
                    }

                }
            });
}

From source file:com.andrew.apollo.MusicPlaybackService.java

/**
 * Initializes the remote control client
 *///from  w  w w. jav a  2  s  .c om
private void setUpRemoteControlClient() {
    final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mMediaButtonReceiverComponent);
    mRemoteControlClient = new RemoteControlClient(PendingIntent.getBroadcast(getApplicationContext(), 0,
            mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    try {
        if (mAudioManager != null) {
            mAudioManager.registerRemoteControlClient(mRemoteControlClient);
        }
    } catch (Throwable t) {
        // seems like this doesn't work on some devices where it requires MODIFY_PHONE_STATE
        // which is a permission only given to system apps, not third party apps.
    }

    // Flags for the media transport control that this client supports.
    int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP;

    flags |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE;
    mRemoteControlClient.setOnGetPlaybackPositionListener(this::position);
    mRemoteControlClient.setPlaybackPositionUpdateListener(this::seek);

    mRemoteControlClient.setTransportControlFlags(flags);
}

From source file:org.moire.ultrasonic.service.DownloadServiceImpl.java

@SuppressLint("NewApi")
public void setUpRemoteControlClient() {
    if (!Util.isLockScreenEnabled(this))
        return;//  w ww  . ja va2 s . co m

    ComponentName componentName = new ComponentName(getPackageName(),
            MediaButtonIntentReceiver.class.getName());

    if (remoteControlClient == null) {
        final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(componentName);
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteControlClient = new RemoteControlClient(broadcast);
        audioManager.registerRemoteControlClient(remoteControlClient);

        // Flags for the media transport control that this client supports.
        int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            flags |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE;

            remoteControlClient
                    .setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() {
                        @Override
                        public long onGetPlaybackPosition() {
                            return mediaPlayer.getCurrentPosition();
                        }
                    });

            remoteControlClient.setPlaybackPositionUpdateListener(
                    new RemoteControlClient.OnPlaybackPositionUpdateListener() {
                        @Override
                        public void onPlaybackPositionUpdate(long newPositionMs) {
                            seekTo((int) newPositionMs);
                        }
                    });
        }

        remoteControlClient.setTransportControlFlags(flags);
    }
}