Example usage for android.media RemoteControlClient PLAYSTATE_PLAYING

List of usage examples for android.media RemoteControlClient PLAYSTATE_PLAYING

Introduction

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

Prototype

int PLAYSTATE_PLAYING

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

Click Source Link

Document

Playback state of a RemoteControlClient which is playing media.

Usage

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

void processPlayRequest() {

    if (MusicExecutor.getInstance().isPlayListEmpty()) {
        if (null != sOnStateChangedListener)
            sOnStateChangedListener.onPlayListIsEmpty();
        return;// w w  w.ja va 2  s.c o m
    }

    tryToGetAudioFocus();

    // actually play the song

    if (mState == State.Stopped) {
        // If we're stopped, just go ahead to the next song and start playing
        playNextSong(mPlayingItem);
    } else if (mState == State.Paused) {
        // If we're paused, just continue playback and restore the 'foreground service' state.
        mState = State.Playing;
        setUpAsForeground(mSongTitle + "");
        configAndStartMediaPlayer();

    }

    // Tell any remote controls that our playback state is 'playing'.
    if (mRemoteControlClientCompat != null) {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }
}

From source file:co.shunya.gita.player.MusicService.java

void processPlayRequest(Intent intent) {
    Long _id = intent.getLongExtra(INTENT_ID_EXTRA, -1);
    //        if (_id < 0) {
    //            throw new IllegalArgumentException("You must pass the id and language to the intent");
    //        }//from   w ww. jav  a  2  s .  com

    if (mState == State.Retrieving) {
        // If we are still retrieving media, just set the flag to start playing when we're
        // ready
        if (_id < 0) {
            throw new IllegalArgumentException("something went wrong intent");
        }

        mWhatToPlayAfterRetrieve = _id; // play a requested song
        mStartPlayingAfterRetrieve = true;
        return;
    }

    tryToGetAudioFocus();

    // actually play the song
    //TODO set the playing mechanism
    Log.i(TAG, "Current State of player:" + mState.toString());
    if (mState == State.Stopped) {
        if (_id < 0) {
            throw new IllegalArgumentException("something went wrong with intent");
        }
        // If we're stopped, just go ahead to the next song and start playing
        playNextSong(_id);
    } else if (mState == State.Paused) {
        // If we're paused, just continue playback and restore the 'foreground service' state.
        setState(State.Playing);
        updateUI();
        setUpAsForeground(mSongTitle + " (playing)");
        configAndStartMediaPlayer();
    }

    // Tell any remote controls that our playback state is 'playing'.
    if (mRemoteControlClientCompat != null) {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }
}

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

void processPlayRequest() {

    tryToGetAudioFocus();/*w  w  w.j a va  2  s  . c  om*/

    if (mAudioFocus == AudioFocus.Focused) {

        startPlayback();

        // Tell any remote controls that our playback state is 'playing'.
        if (mRemoteControlClientCompat != null) {
            mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
        }

        if (mPlayerEventListener != null) {
            mPlayerEventListener.onPlayPauseStateChanged();
        }

        mWakeLock.acquire();

        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(MUSIC_BROAD_CAST_PLAYBACK_STARTED));
    } else {
        Toast.makeText(getApplicationContext(), R.string.audio_focus_unavailable, Toast.LENGTH_LONG).show();
    }
}

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

/**
 * Initializes remote control clients for this service session. 
 * Currently used for lockscreen controls.
 *///ww  w  . j  a v a2 s . c om
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);

}

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

void processPlayRequest() {
    if (mState == State.Retrieving) {
        // If we are still retrieving media, just set the flag to start playing when we're
        // ready/*from w w  w . j  a v  a  2s .  c  o m*/
        mWhatToPlayAfterRetrieve = position; // play a random song
        mStartPlayingAfterRetrieve = true;
        return;
    }

    tryToGetAudioFocus();

    // actually play the song

    if (mState == State.Stopped) {
        // If we're stopped, just go ahead to the next song and start playing
        playNextSong(-1);
    } else if (mState == State.Paused) {
        // If we're paused, just continue playback and restore the 'foreground service' state.
        mState = State.Playing;
        setUpAsForeground(mSongTitle + " (playing)");
        configAndStartMediaPlayer();

    }

    // Tell any remote controls that our playback state is 'playing'.
    if (mRemoteControlClientCompat != null) {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }
}

From source file:com.example.android.mediarouter.player.MainActivity.java

private void registerRemoteControlClient() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // Create the RCC and register with AudioManager and MediaRouter
        mAudioManager.requestAudioFocus(mAfChangeListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        mAudioManager.registerMediaButtonEventReceiver(mEventReceiver);
        mRemoteControlClient = new RemoteControlClient(mMediaPendingIntent);
        mAudioManager.registerRemoteControlClient(mRemoteControlClient);
        mMediaRouter.addRemoteControlClient(mRemoteControlClient);
        SampleMediaButtonReceiver.setActivity(MainActivity.this);
        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
        mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }//w w w  . jav a  2  s. c o  m
}

From source file:com.example.android.mediarouter.player.RadioActivity.java

private void registerRemoteControlClient() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // Create the RCC and register with AudioManager and MediaRouter
        mAudioManager.requestAudioFocus(mAfChangeListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        mAudioManager.registerMediaButtonEventReceiver(mEventReceiver);
        mRemoteControlClient = new RemoteControlClient(mMediaPendingIntent);
        mAudioManager.registerRemoteControlClient(mRemoteControlClient);
        mMediaRouter.addRemoteControlClient(mRemoteControlClient);
        SampleRadioButtonReceiver.setActivity(RadioActivity.this);
        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
        mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }/*from w w  w . j ava  2 s  .  c  o m*/
}

From source file:be.ugent.zeus.hydra.util.audiostream.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   www  . ja va2  s. c  om
 */
void playmp3(String mp3url) {
    mState = State.Stopped;
    relaxResources(false); // release everything except MediaPlayer

    try {
        // set the source of the media player to a manual URL or path
        createMediaPlayerIfNeeded();
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setDataSource(mp3url);
        mIsStreaming = true;

        mSongTitle = "Urgent.fm";

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

        // Update the remote controls
        mRemoteControlClientCompat.editMetadata(true)
                .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, "Urgent.fm")
                .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();

        // 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:de.qspool.clementineremote.backend.ClementinePlayerConnection.java

/**
 * Register the RemoteControlClient//from   w  ww.  java 2s.  c o  m
 */
private void registerRemoteControlClient() {
    // Request AudioFocus, so the widget is shown on the lock-screen
    mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    mAudioManager.registerMediaButtonEventReceiver(mClementineMediaButtonEventReceiver);

    // Create the intent
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mClementineMediaButtonEventReceiver);
    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(App.mApp.getApplicationContext(), 0,
            mediaButtonIntent, 0);
    // Create the client
    mRcClient = new RemoteControlClient(mediaPendingIntent);
    if (App.mClementine.getState() == Clementine.State.PLAY) {
        mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    } else {
        mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
    }
    mRcClient.setTransportControlFlags(
            RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                    | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE);
    mAudioManager.registerRemoteControlClient(mRcClient);
}

From source file:com.namelessdev.mpdroid.NotificationService.java

/**
 * Get the RemoteControlClient status for the corresponding MPDStatus
 *
 * @param mpdStatus MPDStatus to parse//  www .  j a  v a  2 s  .c o  m
 * @return state to give to RemoteControlClient
 */
private int getRemoteState(final MPDStatus mpdStatus) {
    final int state;

    if (mpdStatus == null) {
        state = RemoteControlClient.PLAYSTATE_ERROR;
    } else if (mediaPlayerServiceIsBuffering) {
        state = RemoteControlClient.PLAYSTATE_BUFFERING;
    } else {
        switch (mpdStatus.getState()) {
        case MPDStatus.MPD_STATE_PLAYING:
            state = RemoteControlClient.PLAYSTATE_PLAYING;
            break;
        case MPDStatus.MPD_STATE_STOPPED:
            state = RemoteControlClient.PLAYSTATE_STOPPED;
            break;
        default:
            state = RemoteControlClient.PLAYSTATE_PAUSED;
            break;
        }
    }
    return state;
}