Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.support.v17.leanback.media;
     18 
     19 /**
     20  * Base class that wraps underlying media player. The class is used by PlaybackGlue, for example
     21  * {@link PlaybackTransportControlGlue} is bound to a PlayerAdapter.
     22  * This class is intended to be subclassed, {@link MediaPlayerAdapter} is a concrete subclass
     23  * using {@link android.media.MediaPlayer}.
     24  */
     25 public abstract class PlayerAdapter {
     26 
     27     /**
     28      * Client for client of PlayerAdapter.
     29      */
     30     public static class Callback {
     31 
     32         /**
     33          * Client for Play/Pause state change. See {@link #isPlaying()}.
     34          */
     35         public void onPlayStateChanged(PlayerAdapter adapter) {
     36         }
     37 
     38         /**
     39          * Client for {@link #isPrepared()} changed.
     40          * @param adapter The adapter that has changed ready state.
     41          */
     42         public void onPreparedStateChanged(PlayerAdapter adapter) {
     43         }
     44 
     45         /**
     46          * Client when the current media is finished.
     47          * @param adapter The adapter that has just finished current media.
     48          */
     49         public void onPlayCompleted(PlayerAdapter adapter) {
     50         }
     51 
     52         /**
     53          * Event for {@link #getCurrentPosition()} changed.
     54          * @param adapter The adapter whose {@link #getCurrentPosition()} changed.
     55          */
     56         public void onCurrentPositionChanged(PlayerAdapter adapter) {
     57         }
     58 
     59         /**
     60          * Event for {@link #getBufferedPosition()} changed.
     61          * @param adapter The adapter whose {@link #getBufferedPosition()} changed.
     62          */
     63         public void onBufferedPositionChanged(PlayerAdapter adapter) {
     64         }
     65 
     66         /**
     67          * Event for {@link #getDuration()} changed. Usually the duration does not change
     68          * after playing except for live stream.
     69          * @param adapter The adapter whose {@link #getDuration()} changed.
     70          */
     71         public void onDurationChanged(PlayerAdapter adapter) {
     72         }
     73 
     74         /**
     75          * Event for video size changed.
     76          * @param adapter The adapter whose video size has been detected or changed.
     77          * @param width Intrinsic width of the video.
     78          * @param height Intrinsic height of the video.
     79          */
     80         public void onVideoSizeChanged(PlayerAdapter adapter, int width, int height) {
     81         }
     82 
     83         /**
     84          * Event for error.
     85          * @param adapter The adapter that encounters error.
     86          * @param errorCode Optional error code, specific to implementation.
     87          * @param errorMessage Optional error message, specific to implementation.
     88          */
     89         public void onError(PlayerAdapter adapter, int errorCode, String errorMessage) {
     90         }
     91 
     92         /**
     93          * Event for buffering start or stop. Initial default value is false.
     94          * @param adapter The adapter that begins buffering or finishes buffering.
     95          * @param start True for buffering start, false otherwise.
     96          */
     97         public void onBufferingStateChanged(PlayerAdapter adapter, boolean start) {
     98         }
     99     }
    100 
    101     Callback mCallback;
    102 
    103     /**
    104      * Sets callback for event of PlayerAdapter.
    105      * @param callback Client for event of PlayerAdapter.
    106      */
    107     public final void setCallback(Callback callback) {
    108         mCallback = callback;
    109     }
    110 
    111     /**
    112      * Gets callback for event of PlayerAdapter.
    113      * @return Client for event of PlayerAdapter.
    114      */
    115     public final Callback getCallback() {
    116         return mCallback;
    117     }
    118 
    119     /**
    120      * @return True if media is ready for playback, false otherwise.
    121      */
    122     public boolean isPrepared() {
    123         return true;
    124     }
    125 
    126     /**
    127      * Starts the media player.
    128      */
    129     public abstract void play();
    130 
    131     /**
    132      * Pauses the media player.
    133      */
    134     public abstract void pause();
    135 
    136     /**
    137      * Seek to new position.
    138      * @param positionInMs New position in milliseconds.
    139      */
    140     public void seekTo(long positionInMs) {
    141     }
    142 
    143     /**
    144      * Implement this method to enable or disable progress updating.
    145      * @param enable True to enable progress updating, false otherwise.
    146      */
    147     public void setProgressUpdatingEnabled(boolean enable) {
    148     }
    149 
    150     /**
    151      * Returns true if media is currently playing.
    152      */
    153     public boolean isPlaying() {
    154         return false;
    155     }
    156 
    157     /**
    158      * Returns the duration of the media item in milliseconds.
    159      */
    160     public long getDuration() {
    161         return 0;
    162     }
    163 
    164     /**
    165      * Returns the current position of the media item in milliseconds.
    166      */
    167     public long getCurrentPosition() {
    168         return 0;
    169     }
    170 
    171     /**
    172      * Returns the current buffered position of the media item in milliseconds.
    173      */
    174     public long getBufferedPosition() {
    175         return 0;
    176     }
    177 
    178     /**
    179      * This method is called attached to associated {@link PlaybackGlueHost}.
    180      * @param host
    181      */
    182     public void onAttachedToHost(PlaybackGlueHost host) {
    183     }
    184 
    185     /**
    186      * This method is called when current associated {@link PlaybackGlueHost} is attached to a
    187      * different {@link PlaybackGlue} or {@link PlaybackGlueHost} is destroyed. Subclass may
    188      * override. A typical implementation will release resources (e.g. MediaPlayer or connection
    189      * to playback service) in this method.
    190      */
    191     public void onDetachedFromHost() {
    192     }
    193 }
    194