Java Code Examples for android.support.v4.media.session.PlaybackStateCompat#Builder

The following examples show how to use android.support.v4.media.session.PlaybackStateCompat#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlaybackManager.java    From klingar with Apache License 2.0 6 votes vote down vote up
private void addCustomActions(PlaybackStateCompat.Builder stateBuilder) {
  @ShuffleMode int shuffleMode = queueManager.getShuffleMode();
  @RepeatMode int repeatMode = queueManager.getRepeatMode();

  if (shuffleMode == QueueManager.SHUFFLE_OFF) {
    stateBuilder.addCustomAction(CUSTOM_ACTION_SHUFFLE, "Shuffle", R.drawable.ic_shuffle_all);
  } else {
    stateBuilder.addCustomAction(CUSTOM_ACTION_SHUFFLE, "Shuffle", R.drawable.ic_shuffle_off);
  }

  if (repeatMode == QueueManager.REPEAT_OFF) {
    stateBuilder.addCustomAction(CUSTOM_ACTION_REPEAT, "Repeat", R.drawable.ic_repeat_all);
  } else if (repeatMode == QueueManager.REPEAT_ALL) {
    stateBuilder.addCustomAction(CUSTOM_ACTION_REPEAT, "Repeat", R.drawable.ic_repeat_one);
  } else {
    stateBuilder.addCustomAction(CUSTOM_ACTION_REPEAT, "Repeat", R.drawable.ic_repeat_off);
  }
}
 
Example 2
Source File: PlaybackService.java    From VCL-Android with Apache License 2.0 6 votes vote down vote up
protected void publishState(int state) {
    if (mMediaSession == null)
        return;
    PlaybackStateCompat.Builder bob = new PlaybackStateCompat.Builder();
    bob.setActions(PLAYBACK_ACTIONS);
    switch (state) {
        case MediaPlayer.Event.Playing:
            bob.setState(PlaybackStateCompat.STATE_PLAYING, -1, 1);
            break;
        case MediaPlayer.Event.Stopped:
            bob.setState(PlaybackStateCompat.STATE_STOPPED, -1, 0);
            break;
        default:
        bob.setState(PlaybackStateCompat.STATE_PAUSED, -1, 0);
    }
    PlaybackStateCompat pbState = bob.build();
    mMediaSession.setPlaybackState(pbState);
    mMediaSession.setActive(state != PlaybackStateCompat.STATE_STOPPED);
}
 
Example 3
Source File: MusicService.java    From VinylMusicPlayer with GNU General Public License v3.0 6 votes vote down vote up
private void setCustomAction(PlaybackStateCompat.Builder stateBuilder) {
    int repeatIcon = R.drawable.ic_repeat_white_nocircle_48dp;  // REPEAT_MODE_NONE
    if (getRepeatMode() == REPEAT_MODE_THIS) {
        repeatIcon = R.drawable.ic_repeat_one_white_circle_48dp;
    } else if (getRepeatMode() == REPEAT_MODE_ALL) {
        repeatIcon = R.drawable.ic_repeat_white_circle_48dp;
    }
    stateBuilder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(
            CYCLE_REPEAT, getString(R.string.action_cycle_repeat), repeatIcon)
            .build());

    final int shuffleIcon = getShuffleMode() == SHUFFLE_MODE_NONE ? R.drawable.ic_shuffle_white_nocircle_48dp : R.drawable.ic_shuffle_white_circle_48dp;
    stateBuilder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(
            TOGGLE_SHUFFLE, getString(R.string.action_toggle_shuffle), shuffleIcon)
            .build());

    final int favoriteIcon = MusicUtil.isFavorite(getApplicationContext(), getCurrentSong()) ? R.drawable.ic_favorite_white_circle_48dp : R.drawable.ic_favorite_border_white_nocircle_48dp;
    stateBuilder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(
            TOGGLE_FAVORITE, getString(R.string.action_toggle_favorite), favoriteIcon)
            .build());
}
 
Example 4
Source File: RemoteControlKitKat.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override
public void onClientPlaybackStateUpdate(int state) {
    if (null == mPlaybackState) return;
    PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    builder.setState(state, rController.getEstimatedMediaPosition(), mPlaybackState.getPlaybackSpeed());
    builder.setActions(getPlaybackStateActions(mTransportControlFlags));
    playbackStateChanged(builder.build());
}
 
Example 5
Source File: MediaNotificationManager.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
private void updateMediaSession() {
    if (!mMediaNotificationInfo.supportsPlayPause()) return;

    if (mMediaSession == null) mMediaSession = createMediaSession();

    try {
        // Tell the MediaRouter about the session, so that Chrome can control the volume
        // on the remote cast device (if any).
        // Pre-MR1 versions of JB do not have the complete MediaRouter APIs,
        // so getting the MediaRouter instance will throw an exception.
        MediaRouter.getInstance(mContext).setMediaSessionCompat(mMediaSession);
    } catch (NoSuchMethodError e) {
        // Do nothing. Chrome can't be casting without a MediaRouter, so there is nothing
        // to do here.
    }

    mMediaSession.setMetadata(createMetadata());

    PlaybackStateCompat.Builder playbackStateBuilder =
            new PlaybackStateCompat.Builder().setActions(computeMediaSessionActions());
    if (mMediaNotificationInfo.isPaused) {
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_PAUSED,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
    } else {
        // If notification only supports stop, still pretend
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_PLAYING,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
    }
    mMediaSession.setPlaybackState(playbackStateBuilder.build());
}
 
Example 6
Source File: PlaybackManager.java    From YouTube-In-Background with MIT License 5 votes vote down vote up
/**
 * Update the current media player state, one of the following, optionally showing an error message.
 * <ul>
 * <li> {@link PlaybackState#STATE_NONE}</li>
 * <li> {@link PlaybackState#STATE_STOPPED}</li>
 * <li> {@link PlaybackState#STATE_PLAYING}</li>
 * <li> {@link PlaybackState#STATE_PAUSED}</li>
 * <li> {@link PlaybackState#STATE_FAST_FORWARDING}</li>
 * <li> {@link PlaybackState#STATE_REWINDING}</li>
 * <li> {@link PlaybackState#STATE_BUFFERING}</li>
 * <li> {@link PlaybackState#STATE_ERROR}</li>
 * <li> {@link PlaybackState#STATE_CONNECTING}</li>
 * <li> {@link PlaybackState#STATE_SKIPPING_TO_PREVIOUS}</li>
 * <li> {@link PlaybackState#STATE_SKIPPING_TO_NEXT}</li>
 * <li> {@link PlaybackState#STATE_SKIPPING_TO_QUEUE_ITEM}</li>
 * </ul>
 *
 * @param error if not null, error message to present to the user.
 */
public void updatePlaybackState(String error)
{
    LogHelper.d(TAG, "updatePlaybackState, playback state=" + playback.getState());
    long position = PLAYBACK_POSITION_UNKNOWN;

    if (playback != null && playback.isConnected()) {
        position = playback.getCurrentStreamPosition();
    }

    // Noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat
            .Builder()
            .setActions(getAvailableActions());

    int state = playback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(ERROR_CODE_SKIP_LIMIT_REACHED, error);
        state = STATE_ERROR;
    }
    // Noinspection ResourceType
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    YouTubeVideo currentYouTubeVideo = queueManager.getCurrentVideo();
    if (currentYouTubeVideo != null) {
        stateBuilder.setActiveQueueItemId(queueManager.getCurrentVideoIndex(currentYouTubeVideo.getId()));
    }

    serviceCallback.onPlaybackStateUpdated(stateBuilder.build());

    if (state == STATE_PLAYING || state == STATE_PAUSED) {
        serviceCallback.onNotificationRequired();
    }
}
 
Example 7
Source File: MusicControls.java    From cordova-music-controls-plugin with MIT License 5 votes vote down vote up
private void setMediaPlaybackState(int state) {
	PlaybackStateCompat.Builder playbackstateBuilder = new PlaybackStateCompat.Builder();
	if( state == PlaybackStateCompat.STATE_PLAYING ) {
		playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
			PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID |
			PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH);
		playbackstateBuilder.setState(state, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
	} else {
		playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
			PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID |
			PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH);
		playbackstateBuilder.setState(state, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 0);
	}
	this.mediaSessionCompat.setPlaybackState(playbackstateBuilder.build());
}
 
Example 8
Source File: PlaybackManager.java    From Melophile with Apache License 2.0 5 votes vote down vote up
public void updatePlaybackState(int state) {
  long position = playback.getPosition();
  this.lastState = state;
  if (state == PlaybackStateCompat.STATE_PLAYING ||
          state == PlaybackStateCompat.STATE_PAUSED) {
    serviceCallback.onNotificationRequired();
  }
  PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder()
          .setActions(getAvailableActions())
          .setState(state, position, 1.0f, SystemClock.elapsedRealtime());
  serviceCallback.onPlaybackStateUpdated(builder.build());
}
 
Example 9
Source File: MusicService.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
private void setCustomAction(PlaybackStateCompat.Builder stateBuilder) {
    MediaMetadataCompat currentMusic = getCurrentPlayingMusic();
    if (currentMusic != null) {
        // Set appropriate "Favorite" icon on Custom action:
        String musicId = currentMusic.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
        int favoriteIcon = R.drawable.ic_star_off;
        if (mMusicProvider.isFavorite(musicId)) {
            favoriteIcon = R.drawable.ic_star_on;
        }
        LogUtils.d(TAG, "updatePlaybackState, setting Favorite custom action of music ",
                musicId, " current favorite=", mMusicProvider.isFavorite(musicId));
        stateBuilder.addCustomAction(CUSTOM_ACTION_THUMBS_UP, getString(R.string.favorite),
                favoriteIcon);
    }
}
 
Example 10
Source File: BackgroundAudioService.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
private void setMediaPlaybackState(int state) {
    PlaybackStateCompat.Builder playbackstateBuilder = new PlaybackStateCompat.Builder();
    if( state == PlaybackStateCompat.STATE_PLAYING ) {
        playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS);
    } else {
        playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS);
    }
    playbackstateBuilder.setState(state, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 0);
    mMediaSessionCompat.setPlaybackState(playbackstateBuilder.build());
}
 
Example 11
Source File: RemoteControlKitKat.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override
public void onClientPlaybackStateUpdate(int state) {
    if (null == mPlaybackState) return;
    PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    builder.setState(state, rController.getEstimatedMediaPosition(), mPlaybackState.getPlaybackSpeed());
    builder.setActions(getPlaybackStateActions(mTransportControlFlags));
    playbackStateChanged(builder.build());
}
 
Example 12
Source File: MusicPlaybackService.java    From leanback-showcase with Apache License 2.0 5 votes vote down vote up
private void updateMediaSessionPlayState() {
    PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder();
    int playState;
    if (isPlaying()) {
        playState = PlaybackStateCompat.STATE_PLAYING;
    } else {
        playState = PlaybackStateCompat.STATE_PAUSED;
    }
    long currentPosition = getCurrentPosition();
    playbackStateBuilder.setState(playState, currentPosition, (float) 1.0).setActions(
            getPlaybackStateActions()
    );
    mMediaSession.setPlaybackState(playbackStateBuilder.build());
}
 
Example 13
Source File: AudioPlayerService.java    From react-native-audio-streaming-player with MIT License 5 votes vote down vote up
/**
 * Update the current media player state, optionally showing an error message.
 */
public void updatePlaybackState() {
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null ) {
        position = mPlayback.getCurrentPosition();
    }
    long actions =
            PlaybackStateCompat.ACTION_PLAY_PAUSE |
            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
            PlaybackStateCompat.ACTION_SKIP_TO_NEXT;

    if (mPlayback != null && mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }

    int state = mPlayback.getState();

    //noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(actions)
            .setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    mMediaSession.setPlaybackState(stateBuilder.build());

    Intent intent = new Intent("change-playback-state-event");
    intent.putExtra("state", state);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
 
Example 14
Source File: ServicePlayMusic.java    From Bop with Apache License 2.0 5 votes vote down vote up
private void setMediaPlaybackState(int state) {
    PlaybackStateCompat.Builder playbackstateBuilder = new PlaybackStateCompat.Builder();
    if (state == PlaybackStateCompat.STATE_PLAYING) {
        playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE);
    } else {
        playbackstateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY);
    }
    playbackstateBuilder.setState(state, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 0);
    mMediaSessionCompat.setPlaybackState(playbackstateBuilder.build());
}
 
Example 15
Source File: MediaSessionManager.java    From react-native-jw-media-player with MIT License 5 votes vote down vote up
private void updatePlaybackState(PlayerState playerState) {
	PlaybackStateCompat.Builder newPlaybackState = getPlaybackStateBuilder();
	long capabilities = getCapabilities(playerState);
	//noinspection WrongConstant
	newPlaybackState.setActions(capabilities);

	int playbackStateCompat = PlaybackStateCompat.STATE_NONE;
	switch (playerState) {
		case PLAYING:
			playbackStateCompat = PlaybackStateCompat.STATE_PLAYING;
			break;
		case PAUSED:
			playbackStateCompat = PlaybackStateCompat.STATE_PAUSED;
			break;
		case BUFFERING:
			playbackStateCompat = PlaybackStateCompat.STATE_BUFFERING;
			break;
		case IDLE:
			if (mReceivedError) {
				playbackStateCompat = PlaybackStateCompat.STATE_ERROR;
			} else {
				playbackStateCompat = PlaybackStateCompat.STATE_STOPPED;
			}
			break;
	}

	if (mPlayer != null) {
		newPlaybackState.setState(playbackStateCompat, (long)mPlayer.getPosition(), mPlayer.getPlaybackRate()); // PLAYBACK_RATE
		mMediaSessionCompat.setPlaybackState(newPlaybackState.build());
		mNotificationWrapper
				.createNotification(mPlayer.getContext(), mMediaSessionCompat, capabilities);
	}
}
 
Example 16
Source File: MediaSessionWrapper.java    From Cheerleader with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the playback state builder with supported actions.
 */
private void initPlaybackStateBuilder() {
    mStateBuilder = new PlaybackStateCompat.Builder();
    mStateBuilder.setActions(
            PlaybackStateCompat.ACTION_PLAY
                    | PlaybackStateCompat.ACTION_PAUSE
                    | PlaybackStateCompat.ACTION_PLAY_PAUSE
                    | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                    | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
    );
}
 
Example 17
Source File: RemoteControlKitKat.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override
public void onClientTransportControlUpdate(int transportControlFlags) {
    mTransportControlFlags = transportControlFlags;
    PlaybackStateCompat state = getPlaybackState();
    if (null == state) return;
    PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    builder.setState(state.getState(), rController.getEstimatedMediaPosition(), state.getPlaybackSpeed());
    builder.setActions(getPlaybackStateActions(transportControlFlags));
    playbackStateChanged(builder.build());
}
 
Example 18
Source File: MusicPlaybackService.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
private void updateMediaSessionPlayState() {
    PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder();
    int playState;
    if (isPlaying()) {
        playState = PlaybackStateCompat.STATE_PLAYING;
    } else {
        playState = PlaybackStateCompat.STATE_PAUSED;
    }
    long currentPosition = getCurrentPosition();
    playbackStateBuilder.setState(playState, currentPosition, (float) 1.0).setActions(
            getPlaybackStateActions()
    );
    mMediaSession.setPlaybackState(playbackStateBuilder.build());
}
 
Example 19
Source File: MediaEventResponder.java    From Noyze with Apache License 2.0 4 votes vote down vote up
public static Pair<MediaMetadataCompat, PlaybackStateCompat> respond(Context context, Intent intent) {
    if (null == context || null == intent) return null;
    String mAction = intent.getAction();
    Bundle extras = intent.getExtras();
    if (null == extras) extras = Bundle.EMPTY; // In case we've got nothing.
    MediaMetadataCompat.Builder mBuilder = null;
    PlaybackStateCompat.Builder pBuilder = null;

    int state = PlaybackStateCompat.STATE_NONE;
    long position = 0;

    LOGI("MediaEventResponder", mAction + ", extras=" + Utils.bundle2string(intent.getExtras()));
    if (mAction.startsWith("com.amazon.mp3")) {
        mBuilder = new MediaMetadataCompat.Builder();
        pBuilder = new PlaybackStateCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("com.amazon.mp3.artist"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("com.amazon.mp3.track"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("com.amazon.mp3.album"));
        state = (isPlaying(extras.getInt("com.amazon.mp3.playstate")) ?
                PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
    } else if (mAction.startsWith("com.sonyericsson")) {
        mBuilder = new MediaMetadataCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("ARTIST_NAME"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("TRACK_NAME"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("ALBUM_NAME"));
    } else {
        // This is the default case, standard API check.
        mBuilder = new MediaMetadataCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("artist"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("album"));
        if (extras.containsKey("title"))
            mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("title"));
        else if (extras.containsKey("track"))
            mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("track"));
    }

    // Try the many ways to interpret the play state.
    if (null == pBuilder) {
        pBuilder = new PlaybackStateCompat.Builder();
        String extraKey = null;
        if (extras.containsKey("playstate"))
            extraKey = "playstate";
        else if (extras.containsKey("isPlaying"))
            extraKey = "isPlaying";
        else if (extras.containsKey("playing"))
            extraKey = "playing";
        else if (extras.containsKey("state"))
            extraKey = "state";

        // We still haven't given up, check the action.
        if (TextUtils.isEmpty(extraKey)) {
            boolean bState = false;
            if (mAction.endsWith("endofplayback"))
                bState = false;
            else if (mAction.endsWith("playbackcomplete"))
                bState = false;
            else if (mAction.endsWith("ACTION_PLAYBACK_PAUSE")) // SEMC Legacy
                bState = false;
            else if (mAction.endsWith("ACTION_PAUSED")) // SEMC
                bState = false;
            else if (mAction.endsWith("ACTION_TRACK_STARTED")) // SEMC Legacy
                bState = true;
            else if (mAction.endsWith("ACTION_PLAYBACK_PLAY")) // SEMC
                bState = true;
            state = (bState ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
        } else {
            state = (extras.getBoolean(extraKey) ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
        }
    }

    // Some extras we might want to use... might.
    if (extras.containsKey("duration"))
        mBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, extras.getLong("duration"));
    if (extras.containsKey("position"))
        position = extras.getLong("position");

    // Attempt to figure out what app is playing music.
    pBuilder.setState(state, position, 1.0f);
    mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, packageForAction(mAction));

    // Workaround for Google Play Music... not the best :(
    if (extras.containsKey("previewPlayType") && extras.containsKey("supportsRating") &&
            extras.containsKey("currentContainerId"))
        mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, "com.google.android.music");

    // Workaround for Poweramp... should be pretty specific.
    if (extras.containsKey("com.maxmpz.audioplayer.source"))
        mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, "com.maxmpz.audioplayer");

    return Pair.create(mBuilder.build(), pBuilder.build());
}
 
Example 20
Source File: RadioPlayerService.java    From monkeyboard-radio-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCreate() {
    // Get preferences storage;
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    // Get audio manager
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    if (!sharedPreferences.getBoolean(getString(R.string.SYNC_VOLUME_KEY), true)) {
        volume = sharedPreferences.getInt(getString(R.string.VOLUME_KEY), 13);
    } else {
        // Set the player volume to the system volume
        volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    Log.d(TAG, "Start volume: " + volume);

    // Build a media session for the RadioPlayer
    mediaSession = new MediaSessionCompat(this, this.getClass().getSimpleName());
    mediaSession.setCallback(new MediaSessionCallback());
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    setupVolumeControls();

    // Build a playback state for the player
    playbackStateBuilder = new PlaybackStateCompat.Builder();

    // Update info of the current session to initial state
    updatePlaybackState(PlaybackStateCompat.STATE_STOPPED);
    createNewPlaybackMetadataForStation(new RadioStation());

    // Connect to the radio API
    radio = new RadioDevice(getApplicationContext());

    // Register listeners of the radio
    radio.getListenerManager().registerDataListener(dataListener);
    radio.getListenerManager().registerConnectionStateChangedListener(connectionStateListener);

    saveVolume(volume);

    loadPreferences();

    // Start the process of connecting to the radio device
    openConnection();
}