public abstract class

PlaybackGlue

extends java.lang.Object

 java.lang.Object

↳androidx.leanback.media.PlaybackGlue

Subclasses:

PlaybackControlGlue, PlaybackTransportControlGlue<T>, PlaybackBaseControlGlue<T>, PlaybackBannerControlGlue<T>, MediaPlayerGlue, MediaControllerGlue

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.PlaybackGlue android.support.v17.leanback.media.PlaybackGlue

Overview

Base class for abstraction of media play/pause feature. A subclass of PlaybackGlue will contain implementation of Media Player or a connection to playback Service. App initializes PlaybackGlue subclass, associated it with a PlaybackGlueHost. PlaybackGlueHost is typically implemented by a Fragment or an Activity, it provides the environment to render UI for PlaybackGlue object, it optionally provides SurfaceHolder via SurfaceHolderGlueHost to render video. A typical PlaybackGlue should release resources (e.g. MediaPlayer or connection to playback Service) in PlaybackGlue.onDetachedFromHost(). PlaybackGlue.onDetachedFromHost() is called in two cases:

In rare case if an PlaybackGlue wants to live outside fragment / activity life cycle, it may manages resource release by itself.

Summary

Constructors
publicPlaybackGlue(Context context)

Constructor.

Methods
public voidaddPlayerCallback(PlaybackGlue.PlayerCallback playerCallback)

Add a PlayerCallback.

public ContextgetContext()

Returns the context.

public PlaybackGlueHostgetHost()

protected java.util.List<PlaybackGlue.PlayerCallback>getPlayerCallbacks()

public booleanisPlaying()

Returns true if media is currently playing.

public booleanisPrepared()

Returns true when the media player is prepared to start media playback.

public voidnext()

Goes to the next media item.

protected voidonAttachedToHost(PlaybackGlueHost host)

This method is called attached to associated PlaybackGlueHost.

protected voidonDetachedFromHost()

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

protected voidonHostPause()

This method is called when PlaybackGlueHost is paused.

protected voidonHostResume()

This method is called when PlaybackGlueHost is resumed.

protected voidonHostStart()

This method is called when PlaybackGlueHost is started.

protected voidonHostStop()

This method is called when PlaybackGlueHost is stopped.

public voidpause()

Pauses the media player.

public voidplay()

Starts the media player.

public voidplayWhenPrepared()

Starts play when PlaybackGlue.isPrepared() becomes true.

public voidprevious()

Goes to the previous media item.

public voidremovePlayerCallback(PlaybackGlue.PlayerCallback callback)

Remove a PlayerCallback.

public final voidsetHost(PlaybackGlueHost host)

This method is used to associate a PlaybackGlue with the PlaybackGlueHost which provides UI and optional SurfaceHolderGlueHost.

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

Constructors

public PlaybackGlue(Context context)

Constructor.

Methods

public Context getContext()

Returns the context.

public boolean isPrepared()

Returns true when the media player is prepared to start media playback. When returning false, app may listen to PlaybackGlue.PlayerCallback.onPreparedStateChanged(PlaybackGlue) event.

Returns:

True if prepared, false otherwise.

public void addPlayerCallback(PlaybackGlue.PlayerCallback playerCallback)

Add a PlayerCallback.

Parameters:

playerCallback: The callback to add.

public void removePlayerCallback(PlaybackGlue.PlayerCallback callback)

Remove a PlayerCallback.

Parameters:

callback: The callback to remove.

protected java.util.List<PlaybackGlue.PlayerCallback> getPlayerCallbacks()

Returns:

A snapshot of list of PlayerCallbacks set on the Glue.

public boolean isPlaying()

Returns true if media is currently playing.

public void play()

Starts the media player. Does nothing if PlaybackGlue.isPrepared() is false. To wait PlaybackGlue.isPrepared() to be true before playing, use PlaybackGlue.playWhenPrepared().

public void playWhenPrepared()

Starts play when PlaybackGlue.isPrepared() becomes true.

public void pause()

Pauses the media player.

public void next()

Goes to the next media item. This method is optional.

public void previous()

Goes to the previous media item. This method is optional.

public final void setHost(PlaybackGlueHost host)

This method is used to associate a PlaybackGlue with the PlaybackGlueHost which provides UI and optional SurfaceHolderGlueHost.

Parameters:

host: The host for the PlaybackGlue. Set to null to detach from the host.

protected void onHostStart()

This method is called when PlaybackGlueHost is started. Subclass may override.

protected void onHostStop()

This method is called when PlaybackGlueHost is stopped. Subclass may override.

protected void onHostResume()

This method is called when PlaybackGlueHost is resumed. Subclass may override.

protected void onHostPause()

This method is called when PlaybackGlueHost is paused. Subclass may override.

protected void onAttachedToHost(PlaybackGlueHost host)

This method is called attached to associated PlaybackGlueHost. Subclass may override and call super.onAttachedToHost().

protected void onDetachedFromHost()

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

public PlaybackGlueHost getHost()

Returns:

Associated PlaybackGlueHost or null if not attached to host.

Source

/*
 * Copyright (C) 2016 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 android.annotation.SuppressLint;
import android.content.Context;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Base class for abstraction of media play/pause feature. A subclass of PlaybackGlue will contain
 * implementation of Media Player or a connection to playback Service. App initializes
 * PlaybackGlue subclass, associated it with a {@link PlaybackGlueHost}. {@link PlaybackGlueHost}
 * is typically implemented by a Fragment or an Activity, it provides the environment to render UI
 * for PlaybackGlue object, it optionally provides SurfaceHolder via {@link SurfaceHolderGlueHost}
 * to render video. A typical PlaybackGlue should release resources (e.g. MediaPlayer or connection
 * to playback Service) in {@link #onDetachedFromHost()}.
 * {@link #onDetachedFromHost()} is called in two cases:
 * <ul>
 * <li> app manually change it using {@link #setHost(PlaybackGlueHost)} call</li>
 * <li> When host (fragment or activity) is destroyed </li>
 * </ul>
 * In rare case if an PlaybackGlue wants to live outside fragment / activity life cycle, it may
 * manages resource release by itself.
 *
 * @see PlaybackGlueHost
 */
public abstract class PlaybackGlue {
    private final Context mContext;
    private PlaybackGlueHost mPlaybackGlueHost;

    /**
     * Interface to allow clients to take action once the video is ready to play and start stop.
     */
    public abstract static class PlayerCallback {
        /**
         * Event for {@link #isPrepared()} changed.
         * @param glue The PlaybackGlue that has changed {@link #isPrepared()}.
         */
        public void onPreparedStateChanged(@NonNull PlaybackGlue glue) {
        }

        /**
         * Event for Play/Pause state change. See {@link #isPlaying()}}.
         * @param glue The PlaybackGlue that has changed playing or pausing state.
         */
        public void onPlayStateChanged(@NonNull PlaybackGlue glue) {
        }

        /**
         * Event of the current media is finished.
         * @param glue The PlaybackGlue that has finished current media playing.
         */
        public void onPlayCompleted(@NonNull PlaybackGlue glue) {
        }
    }

    ArrayList<PlayerCallback> mPlayerCallbacks;

    /**
     * Constructor.
     */
    public PlaybackGlue(@NonNull Context context) {
        this.mContext = context;
    }

    /**
     * Returns the context.
     */
    @NonNull
    public Context getContext() {
        return mContext;
    }

    /**
     * Returns true when the media player is prepared to start media playback. When returning false,
     * app may listen to {@link PlayerCallback#onPreparedStateChanged(PlaybackGlue)} event.
     * @return True if prepared, false otherwise.
     */
    public boolean isPrepared() {
        return true;
    }

    /**
     * Add a PlayerCallback.
     * @param playerCallback The callback to add.
     */
    public void addPlayerCallback(@NonNull PlayerCallback playerCallback) {
        if (mPlayerCallbacks == null) {
            mPlayerCallbacks = new ArrayList<>();
        }
        mPlayerCallbacks.add(playerCallback);
    }

    /**
     * Remove a PlayerCallback.
     * @param callback The callback to remove.
     */
    public void removePlayerCallback(@NonNull PlayerCallback callback) {
        if (mPlayerCallbacks != null) {
            mPlayerCallbacks.remove(callback);
        }
    }

    /**
     * @return A snapshot of list of PlayerCallbacks set on the Glue.
     */
    @SuppressLint("NullableCollection")
    @Nullable
    protected List<PlayerCallback> getPlayerCallbacks() {
        if (mPlayerCallbacks == null) {
            return null;
        }
        return new ArrayList<>(mPlayerCallbacks);
    }

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

    /**
     * Starts the media player. Does nothing if {@link #isPrepared()} is false. To wait
     * {@link #isPrepared()} to be true before playing, use {@link #playWhenPrepared()}.
     */
    public void play() {
    }

    /**
     * Starts play when {@link #isPrepared()} becomes true.
     */
    public void playWhenPrepared() {
        if (isPrepared()) {
            play();
        } else {
            addPlayerCallback(new PlayerCallback() {
                @Override
                public void onPreparedStateChanged(@NonNull PlaybackGlue glue) {
                    if (glue.isPrepared()) {
                        removePlayerCallback(this);
                        play();
                    }
                }
            });
        }
    }

    /**
     * Pauses the media player.
     */
    public void pause() {
    }

    /**
     * Goes to the next media item. This method is optional.
     */
    public void next() {
    }

    /**
     * Goes to the previous media item. This method is optional.
     */
    public void previous() {
    }

    /**
     * This method is used to associate a PlaybackGlue with the {@link PlaybackGlueHost} which
     * provides UI and optional {@link SurfaceHolderGlueHost}.
     *
     * @param host The host for the PlaybackGlue. Set to null to detach from the host.
     */
    public final void setHost(@Nullable PlaybackGlueHost host) {
        if (mPlaybackGlueHost == host) {
            return;
        }
        if (mPlaybackGlueHost != null) {
            mPlaybackGlueHost.attachToGlue(null);
        }
        mPlaybackGlueHost = host;
        if (mPlaybackGlueHost != null) {
            mPlaybackGlueHost.attachToGlue(this);
        }
    }

    /**
     * This method is called when {@link PlaybackGlueHost} is started. Subclass may override.
     */
    protected void onHostStart() {
    }

    /**
     * This method is called when {@link PlaybackGlueHost} is stopped. Subclass may override.
     */
    protected void onHostStop() {
    }

    /**
     * This method is called when {@link PlaybackGlueHost} is resumed. Subclass may override.
     */
    protected void onHostResume() {
    }

    /**
     * This method is called when {@link PlaybackGlueHost} is paused. Subclass may override.
     */
    protected void onHostPause() {
    }

    /**
     * This method is called attached to associated {@link PlaybackGlueHost}. Subclass may override
     * and call super.onAttachedToHost().
     */
    @CallSuper
    protected void onAttachedToHost(@NonNull PlaybackGlueHost host) {
        mPlaybackGlueHost = host;
        mPlaybackGlueHost.setHostCallback(new PlaybackGlueHost.HostCallback() {
            @Override
            public void onHostStart() {
                PlaybackGlue.this.onHostStart();
            }

            @Override
            public void onHostStop() {
                PlaybackGlue.this.onHostStop();
            }

            @Override
            public void onHostResume() {
                PlaybackGlue.this.onHostResume();
            }

            @Override
            public void onHostPause() {
                PlaybackGlue.this.onHostPause();
            }

            @Override
            public void onHostDestroy() {
                setHost(null);
            }
        });
    }

    /**
     * This method is called when current associated {@link PlaybackGlueHost} is attached to a
     * different {@link PlaybackGlue} or {@link PlaybackGlueHost} is destroyed . Subclass may
     * override and call super.onDetachedFromHost() at last. A typical PlaybackGlue will release
     * resources (e.g. MediaPlayer or connection to playback service) in this method.
     */
    @CallSuper
    protected void onDetachedFromHost() {
        if (mPlaybackGlueHost != null) {
            mPlaybackGlueHost.setHostCallback(null);
            mPlaybackGlueHost = null;
        }
    }

    /**
     * @return Associated {@link PlaybackGlueHost} or null if not attached to host.
     */
    @Nullable
    public PlaybackGlueHost getHost() {
        return mPlaybackGlueHost;
    }
}