Example usage for android.media RemoteControlClient FLAG_KEY_MEDIA_PAUSE

List of usage examples for android.media RemoteControlClient FLAG_KEY_MEDIA_PAUSE

Introduction

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

Prototype

int FLAG_KEY_MEDIA_PAUSE

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

Click Source Link

Document

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

Usage

From source file:com.nd.android.u.square.service.MusicPlaybackService.java

/**
 * Starts playing the next song. If manualUrl is null, the next song will be randomly selected
 * from our Media Retriever (that is, it will be a random song in the user's device). If
 * manualUrl is non-null, then it specifies the URL or path to the song that will be played
 * next./*from  w  w w  .  j a  v a2s.  c om*/
 */
void playNextSong(Item playingItem) {
    mState = State.Stopped;
    relaxResources(false); // release everything except MediaPlayer

    try {
        if (null == playingItem) {
            playingItem = pollNextSong(mPlayingItem);
            if (null == playingItem) {
                if (sOnStateChangedListener != null) {
                    sOnStateChangedListener.onPlayListIsEmpty();
                }
                return;
            }
            mPlayingItem = playingItem;

            if (null != sOnStateChangedListener)
                sOnStateChangedListener.onCurrentPlayingItemChanged(mPlayingItem);
        }

        // set the source of the media player a a content URI
        createMediaPlayerIfNeeded();
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setDataSource(playingItem.getUrl());
        mIsStreaming = true;
        mSongTitle = playingItem.title;
        mState = State.Preparing;
        setUpAsForeground(mSongTitle + "");

        // Use the media button APIs (if available) to register ourselves for media button
        // events

        MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent);

        // Use the remote control APIs (if available) to set the playback state

        if (mRemoteControlClientCompat == null) {
            Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            intent.setComponent(mMediaButtonReceiverComponent);
            mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent
                    .getBroadcast(this /*context*/, 0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/
            ));
            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);

        // Update the remote controls
        mRemoteControlClientCompat.editMetadata(true)
                .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, playingItem.title)
                .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, playingItem.duration)
                // TODO: fetch real item artwork
                .putBitmap(RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK, mDummyAlbumArt)
                .apply();

        // starts preparing the media player in the background. When it's done, it will call
        // our OnPreparedListener (that is, the onPrepared() method on this class, since we set
        // the listener to 'this').
        //
        // Until the media player is prepared, we *cannot* call start() on it!
        mPlayingItem.bufferedPercent = 0;
        mPlayer.prepareAsync();

        if (null != sOnStateChangedListener)
            sOnStateChangedListener.onPlayerPreparing(mPlayingItem);

        // If we are streaming from the internet, we want to hold a Wifi lock, which prevents
        // the Wifi radio from going to sleep while the song is playing. If, on the other hand,
        // we are *not* streaming, we want to release the lock if we were holding it before.
        if (mIsStreaming)
            mWifiLock.acquire();
        else if (mWifiLock.isHeld())
            mWifiLock.release();
    } catch (IOException ex) {
        Log.e(TAG, "IOException playing next song: " + ex.getMessage(), ex);
    }
}

From source file:com.geryon.ocraa.MusicService.java

/**
 * Starts playing the next song. If manualUrl is null, the next song will be randomly selected
 * from our Media Retriever (that is, it will be a random song in the user's device). If
 * manualUrl is non-null, then it specifies the URL or path to the song that will be played
 * next./*from  w  w w  .  j  a va2  s .  c o  m*/
 */
void playNextSong(int pos) {
    position = pos;
    mState = State.Stopped;
    progressBarHandler.removeCallbacks(mUpdateTimeTask);
    MusicRetriever.Item playingItem = null;
    relaxResources(false); // release everything except MediaPlayer

    try {
        if ((position == -1) || (repeat == false && shuffle == true)) {

            Random mRandom = new Random();

            int random = mRandom.nextInt(mRetriever.ItemSize());
            playingItem = mRetriever.getItem(random);
            Log.d("Randomize:", String.valueOf(random));
            position = random;
            Log.d("Position:", String.valueOf(random));
            if (playingItem == null) {
                Toast.makeText(this, "No available music to play. Place some music on your external storage "
                        + "device (e.g. your SD card) and try again.", Toast.LENGTH_LONG).show();
                processStopRequest(true); // stop everything!
                return;
            }

            createMediaPlayerIfNeeded();
            mPlayer.setDataSource(playingItem.getURI().toString());
        }

        else

        if (position != -1) {
            if (repeat == false) {
                if (shuffle == false) {
                    if (position == mRetriever.ItemSize() - 1) {
                        position = 0;
                    } else {
                        position++;
                    }

                }
            }
            playingItem = mRetriever.getItem(position);

            createMediaPlayerIfNeeded();

            mPlayer.setDataSource(playingItem.getURI().toString());

        }

        mSongTitle = playingItem.getTitle();

        mState = State.Preparing;
        setUpAsForeground(mSongTitle + " (loading)");

        // Use the media button APIs (if available) to register ourselves for media button
        // events

        MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent);

        // Use the remote control APIs (if available) to set the playback state

        if (mRemoteControlClientCompat == null) {
            Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            intent.setComponent(mMediaButtonReceiverComponent);
            mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(
                    this /*context*/, 0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/));
            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);

        // Update the remote controls
        mRemoteControlClientCompat.editMetadata(true)
                .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, playingItem.getArtist())
                .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, playingItem.getAlbum())
                .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, playingItem.getTitle())
                .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, playingItem.getDuration())
                // TODO: fetch real item artwork
                .putBitmap(RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK, mDummyAlbumArt)
                .apply();

        // starts preparing the media player in the background. When it's done, it will call
        // our OnPreparedListener (that is, the onPrepared() method on this class, since we set
        // the listener to 'this').
        //
        // Until the media player is prepared, we *cannot* call start() on it!
        mPlayer.prepareAsync();
        updateProgressBar();
        // If we are streaming from the internet, we want to hold a Wifi lock, which prevents
        // the Wifi radio from going to sleep while the song is playing. If, on the other hand,
        // we are *not* streaming, we want to release the lock if we were holding it before.
        if (mIsStreaming)
            mWifiLock.acquire();
        else if (mWifiLock.isHeld())
            mWifiLock.release();
    } catch (IOException ex) {
        //  Log.e("MusicService", "IOException playing next song: " + ex.getMessage());
        // ex.printStackTrace();
    }
}

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

/**
 * Initializes the remote control client
 *///from   ww w.  j  av  a2 s  . c  o m
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:com.iamplus.musicplayer.MusicService.java

private void startPlayback() {
    try {/*from w  w w .ja  v  a  2  s. c  o  m*/
        MediaItem playingItem = null;
        mIsStreaming = false; // playing a locally available song

        playingItem = MusicRetriever.getInstance().getCurrentSong();
        if (playingItem == null) {
            //Get all songs if the current list is empty
            MusicRetriever.getInstance().setSonglist(getAllSongs(), 0);
            MusicRetriever.getInstance().setPlayMode(e_play_mode.e_play_mode_shuffle);

            playingItem = MusicRetriever.getInstance().getCurrentSong();

            if (playingItem == null) {
                processStopRequest(true); // stop everything!
                return;
            }
        }
        mCurrentPlayingItem = playingItem;

        // set the source of the media player a a content URI
        createMediaPlayerIfNeeded();
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setDataSource(getApplicationContext(), playingItem.getURI());

        // starts preparing the media player in the background. When it's done, it will call
        // our OnPreparedListener (that is, the onPrepared() method on this class, since we set
        // the listener to 'this').
        //
        // Until the media player is prepared, we *cannot* call start() on it!
        mPlayer.prepareAsync();

        // If we are streaming from the internet, we want to hold a Wifi lock, which prevents
        // the Wifi radio from going to sleep while the song is playing. If, on the other hand,
        // we are *not* streaming, we want to release the lock if we were holding it before.
        //         if (mIsStreaming) mWifiLock.acquire();
        //         else if (mWifiLock.isHeld()) mWifiLock.release();
    } catch (IOException ex) {
        Log.e(TAG, "IOException playing first song: " + ex.getMessage());
        //ex.printStackTrace();
        Toast.makeText(getApplicationContext(), R.string.invalid_file_track, Toast.LENGTH_LONG).show();
        //Stop Playback
        processStopRequest();
        return;
    }
    //mSongTitle = mCurrentPlayingItem.getTitle();

    //setUpAsForeground(mSongTitle + " (loading)");

    // Use the media button APIs (if available) to register ourselves for media button
    // events

    MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent);

    //
    // Use the remote control APIs (if available) to set the playback state
    //
    if (mRemoteControlClientCompat == null) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        intent.setComponent(mMediaButtonReceiverComponent);

        mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this /*context*/,
                0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/));

        RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat);
    }

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

    updateRemoteControlPlayingState();
}

From source file:net.nightwhistler.pageturner.fragment.ReadingFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void registerRemoteControlClient(ComponentName componentName) {

    audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    Intent remoteControlIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    remoteControlIntent.setComponent(componentName);

    RemoteControlClient localRemoteControlClient = new RemoteControlClient(
            PendingIntent.getBroadcast(context, 0, remoteControlIntent, 0));

    localRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE);

    audioManager.registerRemoteControlClient(localRemoteControlClient);

    this.remoteControlClient = localRemoteControlClient;
}

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

@SuppressLint("NewApi")
public void setUpRemoteControlClient() {
    if (!Util.isLockScreenEnabled(this))
        return;/*  w  ww. j  a v  a 2  s.  com*/

    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);
    }
}

From source file:org.tomahawk.tomahawk_android.services.PlaybackService.java

/**
 * Update the playback controls/views which are being shown on the lockscreen
 *//*from   w  ww  . j a  v a 2s  .  com*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void updateLockscreenControls() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        Log.d(TAG, "updateLockscreenControls()");
        // Use the media button APIs (if available) to register ourselves for media button
        // events
        MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent);

        if (mRemoteControlClientCompat == null) {
            Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            intent.setComponent(mMediaButtonReceiverComponent);
            mRemoteControlClientCompat = new RemoteControlClientCompat(
                    PendingIntent.getBroadcast(PlaybackService.this /*context*/, 0 /*requestCode, ignored*/,
                            intent /*intent*/, 0 /*flags*/));
            RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat);
        }

        // Use the remote control APIs (if available) to set the playback state
        if (isPlaying()) {
            mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
        } else {
            mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
        }

        int flags = RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE;
        if (hasNextEntry()) {
            flags |= RemoteControlClient.FLAG_KEY_MEDIA_NEXT;
        }
        if (hasPreviousEntry()) {
            flags |= RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS;
        }
        mRemoteControlClientCompat.setTransportControlFlags(flags);

        // Update the remote controls
        synchronized (this) {
            RemoteControlClientCompat.MetadataEditorCompat editor = mRemoteControlClientCompat
                    .editMetadata(true);
            editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST,
                    getCurrentQuery().getArtist().getPrettyName())
                    .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST,
                            getCurrentQuery().getArtist().getPrettyName())
                    .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, getCurrentQuery().getPrettyName())
                    .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION,
                            getCurrentQuery().getPreferredTrack().getDuration());
            if (!TextUtils.isEmpty(getCurrentQuery().getAlbum().getPrettyName())) {
                editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM,
                        getCurrentQuery().getAlbum().getPrettyName());
            }
            editor.apply();
            Log.d(TAG, "Setting lockscreen metadata to: " + getCurrentQuery().getArtist().getPrettyName() + ", "
                    + getCurrentQuery().getPrettyName());
        }

        Picasso.with(TomahawkApp.getContext()).cancelRequest(mLockscreenTarget);
        ImageUtils.loadImageIntoBitmap(TomahawkApp.getContext(), getCurrentQuery().getImage(),
                mLockscreenTarget, Image.getLargeImageSize(), getCurrentQuery().hasArtistImage());
    }
}