public abstract class

PlayerAdapter

extends java.lang.Object

 java.lang.Object

↳androidx.leanback.media.PlayerAdapter

Subclasses:

LeanbackPlayerAdapter, MediaPlayerAdapter, MediaControllerAdapter

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback', version: '1.2.0-alpha04'

  • groupId: androidx.leanback
  • artifactId: leanback
  • version: 1.2.0-alpha04

Artifact androidx.leanback:leanback:1.2.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.leanback:leanback com.android.support:leanback-v17

Androidx class mapping:

androidx.leanback.media.PlayerAdapter android.support.v17.leanback.media.PlayerAdapter

Overview

Base class that wraps underlying media player. The class is used by PlaybackGlue, for example PlaybackTransportControlGlue is bound to a PlayerAdapter. This class is intended to be subclassed, MediaPlayerAdapter is a concrete subclass using android.media.MediaPlayer. ExoPlayer also provides a leanback extension that implements PlayerAdapter. Please see ExoPlayer https://developer.android.com/guide/topics/media/exoplayer

Summary

Constructors
publicPlayerAdapter()

Methods
public voidfastForward()

Optional method.

public longgetBufferedPosition()

Returns the current buffered position of the media item in milliseconds.

public final PlayerAdapter.CallbackgetCallback()

Gets callback for event of PlayerAdapter.

public longgetCurrentPosition()

Returns the current position of the media item in milliseconds.

public longgetDuration()

Returns the duration of the media item in milliseconds.

public longgetSupportedActions()

Return xor combination of values defined in PlaybackBaseControlGlue.

public booleanisPlaying()

Returns true if media is currently playing.

public booleanisPrepared()

public voidnext()

Optional method.

public voidonAttachedToHost(PlaybackGlueHost host)

This method is called attached to associated PlaybackGlueHost.

public voidonDetachedFromHost()

This method is called when current associated PlaybackGlueHost is attached to a different PlaybackGlue or PlaybackGlueHost is destroyed.

public abstract voidpause()

Pauses the media player.

public abstract voidplay()

Starts the media player.

public voidprevious()

Optional method.

public voidrewind()

Optional method.

public voidseekTo(long positionInMs)

Seek to new position.

public final voidsetCallback(PlayerAdapter.Callback callback)

Sets callback for event of PlayerAdapter.

public voidsetProgressUpdatingEnabled(boolean enable)

Implement this method to enable or disable progress updating.

public voidsetRepeatAction(int repeatActionIndex)

Optional method.

public voidsetShuffleAction(int shuffleActionIndex)

Optional method.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public PlayerAdapter()

Methods

public final void setCallback(PlayerAdapter.Callback callback)

Sets callback for event of PlayerAdapter.

Parameters:

callback: Client for event of PlayerAdapter.

public final PlayerAdapter.Callback getCallback()

Gets callback for event of PlayerAdapter.

Returns:

Client for event of PlayerAdapter.

public boolean isPrepared()

Returns:

True if media is ready for playback, false otherwise.

public abstract void play()

Starts the media player.

public abstract void pause()

Pauses the media player.

public void next()

Optional method. Override this method if PlayerAdapter.getSupportedActions() include PlaybackBaseControlGlue.ACTION_SKIP_TO_NEXT to skip to next item.

public void previous()

Optional method. Override this method if PlayerAdapter.getSupportedActions() include PlaybackBaseControlGlue.ACTION_SKIP_TO_PREVIOUS to skip to previous item.

public void fastForward()

Optional method. Override this method if PlayerAdapter.getSupportedActions() include PlaybackBaseControlGlue.ACTION_FAST_FORWARD to fast forward current media item.

public void rewind()

Optional method. Override this method if PlayerAdapter.getSupportedActions() include PlaybackBaseControlGlue.ACTION_REWIND to rewind in current media item.

public void seekTo(long positionInMs)

Seek to new position.

Parameters:

positionInMs: New position in milliseconds.

public void setProgressUpdatingEnabled(boolean enable)

Implement this method to enable or disable progress updating.

Parameters:

enable: True to enable progress updating, false otherwise.

public void setShuffleAction(int shuffleActionIndex)

Optional method. Override this method if PlayerAdapter.getSupportedActions() include PlaybackBaseControlGlue.ACTION_SHUFFLE to set the shuffle action.

Parameters:

shuffleActionIndex: The repeat action. Must be one of the followings: PlaybackControlsRow.ShuffleAction.INDEX_OFF PlaybackControlsRow.ShuffleAction.INDEX_ON

public void setRepeatAction(int repeatActionIndex)

Optional method. Override this method if PlayerAdapter.getSupportedActions() include PlaybackBaseControlGlue.ACTION_REPEAT to set the repeat action.

Parameters:

repeatActionIndex: The shuffle action. Must be one of the followings: PlaybackControlsRow.RepeatAction.INDEX_ONE PlaybackControlsRow.RepeatAction.INDEX_ALL, PlaybackControlsRow.RepeatAction.INDEX_NONE,

public boolean isPlaying()

Returns true if media is currently playing.

public long getDuration()

Returns the duration of the media item in milliseconds.

public long getSupportedActions()

Return xor combination of values defined in PlaybackBaseControlGlue. Default is PLAY_PAUSE (unless subclass enforce to be 0)

public long getCurrentPosition()

Returns the current position of the media item in milliseconds.

public long getBufferedPosition()

Returns the current buffered position of the media item in milliseconds.

public void onAttachedToHost(PlaybackGlueHost host)

This method is called attached to associated PlaybackGlueHost.

Parameters:

host:

public void onDetachedFromHost()

This method is called when current associated PlaybackGlueHost is attached to a different PlaybackGlue or PlaybackGlueHost is destroyed. Subclass may override. A typical implementation will release resources (e.g. MediaPlayer or connection to playback service) in this method.

Source

/*
 * Copyright (C) 2017 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.leanback.media;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Base class that wraps underlying media player. The class is used by PlaybackGlue, for example
 * {@link PlaybackTransportControlGlue} is bound to a PlayerAdapter.
 * This class is intended to be subclassed, {@link MediaPlayerAdapter} is a concrete subclass
 * using {@link android.media.MediaPlayer}.
 * ExoPlayer also provides a leanback extension that implements PlayerAdapter. Please see
 * <a href="https://developer.android.com/guide/topics/media/exoplayer">ExoPlayer</a>
 * https://developer.android.com/guide/topics/media/exoplayer
 */
public abstract class PlayerAdapter {

    /**
     * Client for client of PlayerAdapter.
     */
    public static class Callback {

        /**
         * Client for Play/Pause state change. See {@link #isPlaying()}.
         */
        public void onPlayStateChanged(@NonNull PlayerAdapter adapter) {
        }

        /**
         * Client for {@link #isPrepared()} changed.
         * @param adapter The adapter that has changed ready state.
         */
        public void onPreparedStateChanged(@NonNull PlayerAdapter adapter) {
        }

        /**
         * Client when the current media is finished.
         * @param adapter The adapter that has just finished current media.
         */
        public void onPlayCompleted(@NonNull PlayerAdapter adapter) {
        }

        /**
         * Event for {@link #getCurrentPosition()} changed.
         * @param adapter The adapter whose {@link #getCurrentPosition()} changed.
         */
        public void onCurrentPositionChanged(@NonNull PlayerAdapter adapter) {
        }

        /**
         * Event for {@link #getBufferedPosition()} changed.
         * @param adapter The adapter whose {@link #getBufferedPosition()} changed.
         */
        public void onBufferedPositionChanged(@NonNull PlayerAdapter adapter) {
        }

        /**
         * Event for {@link #getDuration()} changed. Usually the duration does not change
         * after playing except for live stream.
         * @param adapter The adapter whose {@link #getDuration()} changed.
         */
        public void onDurationChanged(@NonNull PlayerAdapter adapter) {
        }

        /**
         * Event for video size changed.
         * @param adapter The adapter whose video size has been detected or changed.
         * @param width Intrinsic width of the video.
         * @param height Intrinsic height of the video.
         */
        public void onVideoSizeChanged(@NonNull PlayerAdapter adapter, int width, int height) {
        }

        /**
         * Event for error.
         * @param adapter The adapter that encounters error.
         * @param errorCode Optional error code, specific to implementation.
         * @param errorMessage Optional error message, specific to implementation.
         */
        public void onError(
                @NonNull PlayerAdapter adapter,
                int errorCode,
                @Nullable String errorMessage
        ) {
        }

        /**
         * Event for buffering start or stop. Initial default value is false.
         * @param adapter The adapter that begins buffering or finishes buffering.
         * @param start True for buffering start, false otherwise.
         */
        public void onBufferingStateChanged(@NonNull PlayerAdapter adapter, boolean start) {
        }

        /**
         * Event for meta data changed.
         * @param adapter The adapter that finishes current media item.
         */
        public void onMetadataChanged(@NonNull PlayerAdapter adapter) {
        }
    }

    Callback mCallback;

    /**
     * Sets callback for event of PlayerAdapter.
     * @param callback Client for event of PlayerAdapter.
     */
    public final void setCallback(@Nullable Callback callback) {
        mCallback = callback;
    }

    /**
     * Gets callback for event of PlayerAdapter.
     * @return Client for event of PlayerAdapter.
     */
    @Nullable
    public final Callback getCallback() {
        return mCallback;
    }

    /**
     * @return True if media is ready for playback, false otherwise.
     */
    public boolean isPrepared() {
        return true;
    }

    /**
     * Starts the media player.
     */
    public abstract void play();

    /**
     * Pauses the media player.
     */
    public abstract void pause();

    /**
     * Optional method. Override this method if {@link #getSupportedActions()} include
     * {@link PlaybackBaseControlGlue#ACTION_SKIP_TO_NEXT} to skip
     * to next item.
     */
    public void next() {
    }

    /**
     * Optional method. Override this method if {@link #getSupportedActions()} include
     * {@link PlaybackBaseControlGlue#ACTION_SKIP_TO_PREVIOUS} to skip
     * to previous item.
     */
    public void previous() {
    }

    /**
     * Optional method. Override this method if {@link #getSupportedActions()} include
     * {@link PlaybackBaseControlGlue#ACTION_FAST_FORWARD} to fast
     * forward current media item.
     */
    public void fastForward() {
    }

    /**
     * Optional method. Override this method if {@link #getSupportedActions()} include
     * {@link PlaybackBaseControlGlue#ACTION_REWIND} to rewind in
     * current media item.
     */
    public void rewind() {
    }

    /**
     * Seek to new position.
     * @param positionInMs New position in milliseconds.
     */
    public void seekTo(long positionInMs) {
    }

    /**
     * Implement this method to enable or disable progress updating.
     * @param enable True to enable progress updating, false otherwise.
     */
    public void setProgressUpdatingEnabled(boolean enable) {
    }

    /**
     * Optional method. Override this method if {@link #getSupportedActions()} include
     * {@link PlaybackBaseControlGlue#ACTION_SHUFFLE} to set the shuffle action.
     *
     * @param shuffleActionIndex The repeat action. Must be one of the followings:
     *                           {@link androidx.leanback.widget.PlaybackControlsRow.ShuffleAction#INDEX_OFF}
     *                           {@link androidx.leanback.widget.PlaybackControlsRow.ShuffleAction#INDEX_ON}
     */
    public void setShuffleAction(int shuffleActionIndex) {
    }

    /**
     * Optional method. Override this method if {@link #getSupportedActions()} include
     * {@link PlaybackBaseControlGlue#ACTION_REPEAT} to set the repeat action.
     *
     * @param repeatActionIndex The shuffle action. Must be one of the followings:
     *                          {@link androidx.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_ONE}
     *                          {@link androidx.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_ALL},
     *                          {@link androidx.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_NONE},
     */
    public void setRepeatAction(int repeatActionIndex) {
    }

    /**
     * Returns true if media is currently playing.
     */
    public boolean isPlaying() {
        return false;
    }

    /**
     * Returns the duration of the media item in milliseconds.
     */
    public long getDuration() {
        return 0;
    }

    /**
     * Return xor combination of values defined in PlaybackBaseControlGlue.
     * Default is PLAY_PAUSE (unless subclass enforce to be 0)
     */
    public long getSupportedActions() {
        return PlaybackBaseControlGlue.ACTION_PLAY_PAUSE;
    }

    /**
     * Returns the current position of the media item in milliseconds.
     */
    public long getCurrentPosition() {
        return 0;
    }

    /**
     * Returns the current buffered position of the media item in milliseconds.
     */
    public long getBufferedPosition() {
        return 0;
    }

    /**
     * This method is called attached to associated {@link PlaybackGlueHost}.
     * @param host
     */
    public void onAttachedToHost(@NonNull PlaybackGlueHost host) {
    }

    /**
     * This method is called when current associated {@link PlaybackGlueHost} is attached to a
     * different {@link PlaybackGlue} or {@link PlaybackGlueHost} is destroyed. Subclass may
     * override. A typical implementation will release resources (e.g. MediaPlayer or connection
     * to playback service) in this method.
     */
    public void onDetachedFromHost() {
    }
}