public abstract class

RemoteSessionPlayer

extends SessionPlayer

 java.lang.Object

androidx.media2.common.SessionPlayer

↳androidx.media2.session.RemoteSessionPlayer

Gradle dependencies

compile group: 'androidx.media2', name: 'media2-session', version: '1.2.1'

  • groupId: androidx.media2
  • artifactId: media2-session
  • version: 1.2.1

Artifact androidx.media2:media2-session:1.2.1 it located at Google repository (https://maven.google.com/)

Overview

Base interface for all remote media players that want media session and playback happens on the remote device through MediaRouter.

If you use this to the MediaSession, session would dispatch incoming volume change event to the player instead of changing device stream volume.

Summary

Fields
public static final intVOLUME_CONTROL_ABSOLUTE

The volume control uses an absolute value.

public static final intVOLUME_CONTROL_FIXED

The volume is fixed and can not be modified.

public static final intVOLUME_CONTROL_RELATIVE

The volume control uses relative adjustment via RemoteSessionPlayer.adjustVolume(int).

from SessionPlayerBUFFERING_STATE_BUFFERING_AND_PLAYABLE, BUFFERING_STATE_BUFFERING_AND_STARVED, BUFFERING_STATE_COMPLETE, BUFFERING_STATE_UNKNOWN, INVALID_ITEM_INDEX, PLAYER_STATE_ERROR, PLAYER_STATE_IDLE, PLAYER_STATE_PAUSED, PLAYER_STATE_PLAYING, REPEAT_MODE_ALL, REPEAT_MODE_GROUP, REPEAT_MODE_NONE, REPEAT_MODE_ONE, SHUFFLE_MODE_ALL, SHUFFLE_MODE_GROUP, SHUFFLE_MODE_NONE, UNKNOWN_TIME
Constructors
publicRemoteSessionPlayer()

Methods
public abstract java.util.concurrent.Future<SessionPlayer.PlayerResult>adjustVolume(int direction)

Adjusts player volume with the direction.

public abstract intgetMaxVolume()

Gets the maximum volume that can be used in RemoteSessionPlayer.setVolume(int).

public abstract intgetVolume()

Gets the current volume of this player to this player.

public abstract intgetVolumeControlType()

Gets the volume type.

public abstract java.util.concurrent.Future<SessionPlayer.PlayerResult>setVolume(int volume)

Sets the volume of the audio of the media to play, expressed as a linear multiplier on the audio samples.

from SessionPlayeraddPlaylistItem, close, deselectTrack, getAudioAttributes, getBufferedPosition, getBufferingState, getCallbacks, getCurrentMediaItem, getCurrentMediaItemIndex, getCurrentPosition, getDuration, getNextMediaItemIndex, getPlaybackSpeed, getPlayerState, getPlaylist, getPlaylistMetadata, getPreviousMediaItemIndex, getRepeatMode, getSelectedTrack, getShuffleMode, getTracks, getVideoSize, movePlaylistItem, pause, play, prepare, registerPlayerCallback, removePlaylistItem, replacePlaylistItem, seekTo, selectTrack, setAudioAttributes, setMediaItem, setPlaybackSpeed, setPlaylist, setRepeatMode, setShuffleMode, setSurface, skipToNextPlaylistItem, skipToPlaylistItem, skipToPreviousPlaylistItem, unregisterPlayerCallback, updatePlaylistMetadata
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int VOLUME_CONTROL_FIXED

The volume is fixed and can not be modified. Requests to change volume should be ignored.

public static final int VOLUME_CONTROL_RELATIVE

The volume control uses relative adjustment via RemoteSessionPlayer.adjustVolume(int). Attempts to set the volume to a specific value should be ignored.

public static final int VOLUME_CONTROL_ABSOLUTE

The volume control uses an absolute value. It may be adjusted using RemoteSessionPlayer.adjustVolume(int) or set directly using RemoteSessionPlayer.setVolume(int).

Constructors

public RemoteSessionPlayer()

Methods

public abstract java.util.concurrent.Future<SessionPlayer.PlayerResult> adjustVolume(int direction)

Adjusts player volume with the direction. Override this API to customize volume change in remote device.

This would be ignored when volume control type is RemoteSessionPlayer.VOLUME_CONTROL_FIXED.

Parameters:

direction: direction of the volume changes. Positive value for volume up, negative for volume down.

Returns:

result of adjusting the volume. Shouldn't be null.

public abstract java.util.concurrent.Future<SessionPlayer.PlayerResult> setVolume(int volume)

Sets the volume of the audio of the media to play, expressed as a linear multiplier on the audio samples.

Note that this volume is specific to the player, and is separate from stream volume used across the platform.

A value of 0 indicates muting. See RemoteSessionPlayer.getMaxVolume() for the volume range supported by this player.

Parameters:

volume: a value between 0 and RemoteSessionPlayer.getMaxVolume().

Returns:

result of setting the volume. Shouldn't be null.

public abstract int getVolume()

Gets the current volume of this player to this player.

Note that it does not take into account the associated stream volume because the playback is happening outside of the phone device.

Returns:

the player volume.

public abstract int getMaxVolume()

Gets the maximum volume that can be used in RemoteSessionPlayer.setVolume(int).

Returns:

the maximum volume. Shouldn't be negative.

public abstract int getVolumeControlType()

Gets the volume type.

This shouldn't be changed after instantiation.

Returns:

one of the volume type

See also: RemoteSessionPlayer.VOLUME_CONTROL_FIXED, RemoteSessionPlayer.VOLUME_CONTROL_RELATIVE, RemoteSessionPlayer.VOLUME_CONTROL_ABSOLUTE

Source

/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.media2.session;

import static androidx.annotation.RestrictTo.Scope.LIBRARY;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.media2.common.SessionPlayer;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;

/**
 * Base interface for all remote media players that want media session and playback happens on the
 * remote device through MediaRouter.
 * <p>
 * If you use this to the {@link MediaSession}, session would dispatch incoming volume change event
 * to the player instead of changing device stream volume.
 */
public abstract class RemoteSessionPlayer extends SessionPlayer {
    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    @IntDef({VOLUME_CONTROL_FIXED, VOLUME_CONTROL_RELATIVE, VOLUME_CONTROL_ABSOLUTE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface VolumeControlType {}

    /**
     * The volume is fixed and can not be modified. Requests to change volume
     * should be ignored.
     */
    public static final int VOLUME_CONTROL_FIXED = 0;

    /**
     * The volume control uses relative adjustment via
     * {@link #adjustVolume(int)}. Attempts to set the volume to a specific
     * value should be ignored.
     */
    public static final int VOLUME_CONTROL_RELATIVE = 1;

    /**
     * The volume control uses an absolute value. It may be adjusted using
     * {@link #adjustVolume(int)} or set directly using
     * {@link #setVolume(int)}.
     */
    public static final int VOLUME_CONTROL_ABSOLUTE = 2;

    /**
     * Adjusts player volume with the direction. Override this API to customize volume change in
     * remote device.
     * <p>
     * This would be ignored when volume control type is {@link #VOLUME_CONTROL_FIXED}.
     *
     * @param direction direction of the volume changes. Positive value for volume up, negative for
     *                  volume down.
     * @return result of adjusting the volume. Shouldn't be {@code null}.
     */
    @NonNull
    public abstract Future<PlayerResult> adjustVolume(int direction);

    /**
     * Sets the volume of the audio of the media to play, expressed as a linear multiplier
     * on the audio samples.
     * <p>
     * Note that this volume is specific to the player, and is separate from stream volume
     * used across the platform.
     * <p>
     * A value of {@code 0} indicates muting. See {@link #getMaxVolume()} for the volume range
     * supported by this player.
     *
     * @param volume a value between {@code 0} and {@link #getMaxVolume()}.
     * @return result of setting the volume. Shouldn't be {@code null}.
     */
    @NonNull
    public abstract Future<PlayerResult> setVolume(int volume);

    /**
     * Gets the current volume of this player to this player.
     * <p>
     * Note that it does not take into account the associated stream volume because the playback is
     * happening outside of the phone device.
     *
     * @return the player volume.
     */
    public abstract int getVolume();

    /**
     * Gets the maximum volume that can be used in {@link #setVolume(int)}.
     *
     * @return the maximum volume. Shouldn't be negative.
     */
    public abstract int getMaxVolume();

    /**
     * Gets the volume type.
     * <p>
     * This shouldn't be changed after instantiation.
     *
     * @return one of the volume type
     * @see #VOLUME_CONTROL_FIXED
     * @see #VOLUME_CONTROL_RELATIVE
     * @see #VOLUME_CONTROL_ABSOLUTE
     */
    @VolumeControlType
    public abstract int getVolumeControlType();

    /**
     * A callback class to receive notifications for events on the remote session player. See
     * {@link #registerPlayerCallback(Executor, PlayerCallback)} to register this callback.
     * <p>
     * This is registered by {@link MediaSession} to notify volume changes to the
     * {@link MediaController}.
     */
    public static class Callback extends SessionPlayer.PlayerCallback {
        /**
         * Called to indicate that the volume has changed.
         *
         * @param player the player that has changed volume.
         * @param volume the new volume
         * @see #setVolume(int)
         */
        public void onVolumeChanged(@NonNull RemoteSessionPlayer player, int volume) {
        }
    }
}