13bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu/* 23bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Copyright (C) 2017 The Android Open Source Project 33bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * 43bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Licensed under the Apache License, Version 2.0 (the "License"); 53bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * you may not use this file except in compliance with the License. 63bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * You may obtain a copy of the License at 73bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * 83bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * http://www.apache.org/licenses/LICENSE-2.0 93bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * 103bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Unless required by applicable law or agreed to in writing, software 113bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * distributed under the License is distributed on an "AS IS" BASIS, 123bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * See the License for the specific language governing permissions and 143bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * limitations under the License. 153bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 163bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 173bcad88cbf4488e747d84893c35f2351b8f84afeDake Gupackage android.support.v17.leanback.media; 183bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 193bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu/** 203bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Base class that wraps underlying media player. The class is used by PlaybackGlue, for example 213bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * {@link PlaybackTransportControlGlue} is bound to a PlayerAdapter. 223bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * This class is intended to be subclassed, {@link MediaPlayerAdapter} is a concrete subclass 233bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * using {@link android.media.MediaPlayer}. 243bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 253bcad88cbf4488e747d84893c35f2351b8f84afeDake Gupublic abstract class PlayerAdapter { 263bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 273bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 283bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Client for client of PlayerAdapter. 293bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 303bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public static class Callback { 313bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 323bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 333bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Client for Play/Pause state change. See {@link #isPlaying()}. 343bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 353bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onPlayStateChanged(PlayerAdapter adapter) { 363bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 373bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 383bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 393bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Client for {@link #isPrepared()} changed. 403bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param adapter The adapter that has changed ready state. 413bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 423bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onPreparedStateChanged(PlayerAdapter adapter) { 433bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 443bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 453bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 463bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Client when the current media is finished. 473bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param adapter The adapter that has just finished current media. 483bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 493bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onPlayCompleted(PlayerAdapter adapter) { 503bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 513bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 523bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 533bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Event for {@link #getCurrentPosition()} changed. 543bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param adapter The adapter whose {@link #getCurrentPosition()} changed. 553bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 563bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onCurrentPositionChanged(PlayerAdapter adapter) { 573bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 583bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 593bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 603bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Event for {@link #getBufferedPosition()} changed. 613bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param adapter The adapter whose {@link #getBufferedPosition()} changed. 623bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 633bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onBufferedPositionChanged(PlayerAdapter adapter) { 643bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 653bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 663bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 673bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Event for {@link #getDuration()} changed. Usually the duration does not change 683bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * after playing except for live stream. 693bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param adapter The adapter whose {@link #getDuration()} changed. 703bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 713bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onDurationChanged(PlayerAdapter adapter) { 723bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 733bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 743bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 753bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Event for video size changed. 763bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param adapter The adapter whose video size has been detected or changed. 773bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param width Intrinsic width of the video. 783bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param height Intrinsic height of the video. 793bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 803bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onVideoSizeChanged(PlayerAdapter adapter, int width, int height) { 813bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 823bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 833bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 843bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Event for error. 853bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param adapter The adapter that encounters error. 863bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param errorCode Optional error code, specific to implementation. 873bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param errorMessage Optional error message, specific to implementation. 883bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 893bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onError(PlayerAdapter adapter, int errorCode, String errorMessage) { 903bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 913bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 923bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 933bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Event for buffering start or stop. Initial default value is false. 943bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param adapter The adapter that begins buffering or finishes buffering. 953bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param start True for buffering start, false otherwise. 963bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 973bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onBufferingStateChanged(PlayerAdapter adapter, boolean start) { 983bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 993bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1003bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1013bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu Callback mCallback; 1023bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1033bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1043bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Sets callback for event of PlayerAdapter. 1053bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param callback Client for event of PlayerAdapter. 1063bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1073bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public final void setCallback(Callback callback) { 1083bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu mCallback = callback; 1093bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1103bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1113bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1123bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Gets callback for event of PlayerAdapter. 1133bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @return Client for event of PlayerAdapter. 1143bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1153bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public final Callback getCallback() { 1163bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu return mCallback; 1173bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1183bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1193bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1203bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @return True if media is ready for playback, false otherwise. 1213bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1223bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public boolean isPrepared() { 1233bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu return true; 1243bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1253bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1263bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1273bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Starts the media player. 1283bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1293bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public abstract void play(); 1303bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1313bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1323bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Pauses the media player. 1333bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1343bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public abstract void pause(); 1353bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1363bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1373bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Seek to new position. 1383bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param positionInMs New position in milliseconds. 1393bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1403bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void seekTo(long positionInMs) { 1413bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1423bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1433bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1443bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Implement this method to enable or disable progress updating. 1453bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param enable True to enable progress updating, false otherwise. 1463bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1473bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void setProgressUpdatingEnabled(boolean enable) { 1483bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1493bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1503bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1513bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Returns true if media is currently playing. 1523bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1533bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public boolean isPlaying() { 1543bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu return false; 1553bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1563bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1573bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1583bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Returns the duration of the media item in milliseconds. 1593bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1603bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public long getDuration() { 1613bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu return 0; 1623bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1633bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1643bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1653bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Returns the current position of the media item in milliseconds. 1663bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1673bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public long getCurrentPosition() { 1683bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu return 0; 1693bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1703bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1713bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1723bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Returns the current buffered position of the media item in milliseconds. 1733bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1743bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public long getBufferedPosition() { 1753bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu return 0; 1763bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1773bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1783bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1793bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * This method is called attached to associated {@link PlaybackGlueHost}. 1803bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * @param host 1813bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1823bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onAttachedToHost(PlaybackGlueHost host) { 1833bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1843bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu 1853bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu /** 1863bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * This method is called when current associated {@link PlaybackGlueHost} is attached to a 1873bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * different {@link PlaybackGlue} or {@link PlaybackGlueHost} is destroyed. Subclass may 1883bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * override. A typical implementation will release resources (e.g. MediaPlayer or connection 1893bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * to playback service) in this method. 1903bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */ 1913bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu public void onDetachedFromHost() { 1923bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu } 1933bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu} 194