• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 androidx.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          */
onPlayStateChanged(PlayerAdapter adapter)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          */
onPreparedStateChanged(PlayerAdapter adapter)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          */
onPlayCompleted(PlayerAdapter adapter)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          */
onCurrentPositionChanged(PlayerAdapter adapter)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          */
onBufferedPositionChanged(PlayerAdapter adapter)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          */
onDurationChanged(PlayerAdapter adapter)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          */
onVideoSizeChanged(PlayerAdapter adapter, int width, int height)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          */
onError(PlayerAdapter adapter, int errorCode, String errorMessage)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          */
onBufferingStateChanged(PlayerAdapter adapter, boolean start)97         public void onBufferingStateChanged(PlayerAdapter adapter, boolean start) {
98         }
99 
100         /**
101          * Event for meta data changed.
102          * @param adapter The adapter that finishes current media item.
103          */
onMetadataChanged(PlayerAdapter adapter)104         public void onMetadataChanged(PlayerAdapter adapter) {
105         }
106     }
107 
108     Callback mCallback;
109 
110     /**
111      * Sets callback for event of PlayerAdapter.
112      * @param callback Client for event of PlayerAdapter.
113      */
setCallback(Callback callback)114     public final void setCallback(Callback callback) {
115         mCallback = callback;
116     }
117 
118     /**
119      * Gets callback for event of PlayerAdapter.
120      * @return Client for event of PlayerAdapter.
121      */
getCallback()122     public final Callback getCallback() {
123         return mCallback;
124     }
125 
126     /**
127      * @return True if media is ready for playback, false otherwise.
128      */
isPrepared()129     public boolean isPrepared() {
130         return true;
131     }
132 
133     /**
134      * Starts the media player.
135      */
play()136     public abstract void play();
137 
138     /**
139      * Pauses the media player.
140      */
pause()141     public abstract void pause();
142 
143     /**
144      * Optional method. Override this method if {@link #getSupportedActions()} include
145      * {@link PlaybackBaseControlGlue#ACTION_SKIP_TO_NEXT} to skip
146      * to next item.
147      */
next()148     public void next() {
149     }
150 
151     /**
152      * Optional method. Override this method if {@link #getSupportedActions()} include
153      * {@link PlaybackBaseControlGlue#ACTION_SKIP_TO_PREVIOUS} to skip
154      * to previous item.
155      */
previous()156     public void previous() {
157     }
158 
159     /**
160      * Optional method. Override this method if {@link #getSupportedActions()} include
161      * {@link PlaybackBaseControlGlue#ACTION_FAST_FORWARD} to fast
162      * forward current media item.
163      */
fastForward()164     public void fastForward() {
165     }
166 
167     /**
168      * Optional method. Override this method if {@link #getSupportedActions()} include
169      * {@link PlaybackBaseControlGlue#ACTION_REWIND} to rewind in
170      * current media item.
171      */
rewind()172     public void rewind() {
173     }
174 
175     /**
176      * Seek to new position.
177      * @param positionInMs New position in milliseconds.
178      */
seekTo(long positionInMs)179     public void seekTo(long positionInMs) {
180     }
181 
182     /**
183      * Implement this method to enable or disable progress updating.
184      * @param enable True to enable progress updating, false otherwise.
185      */
setProgressUpdatingEnabled(boolean enable)186     public void setProgressUpdatingEnabled(boolean enable) {
187     }
188 
189     /**
190      * Optional method. Override this method if {@link #getSupportedActions()} include
191      * {@link PlaybackBaseControlGlue#ACTION_SHUFFLE} to set the shuffle action.
192      *
193      * @param shuffleActionIndex The repeat action. Must be one of the followings:
194      *                           {@link androidx.leanback.widget.PlaybackControlsRow.ShuffleAction#INDEX_OFF}
195      *                           {@link androidx.leanback.widget.PlaybackControlsRow.ShuffleAction#INDEX_ON}
196      */
setShuffleAction(int shuffleActionIndex)197     public void setShuffleAction(int shuffleActionIndex) {
198     }
199 
200     /**
201      * Optional method. Override this method if {@link #getSupportedActions()} include
202      * {@link PlaybackBaseControlGlue#ACTION_REPEAT} to set the repeat action.
203      *
204      * @param repeatActionIndex The shuffle action. Must be one of the followings:
205      *                          {@link androidx.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_ONE}
206      *                          {@link androidx.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_ALL},
207      *                          {@link androidx.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_NONE},
208      */
setRepeatAction(int repeatActionIndex)209     public void setRepeatAction(int repeatActionIndex) {
210     }
211 
212     /**
213      * Returns true if media is currently playing.
214      */
isPlaying()215     public boolean isPlaying() {
216         return false;
217     }
218 
219     /**
220      * Returns the duration of the media item in milliseconds.
221      */
getDuration()222     public long getDuration() {
223         return 0;
224     }
225 
226     /**
227      * Return xor combination of values defined in PlaybackBaseControlGlue.
228      * Default is PLAY_PAUSE (unless subclass enforce to be 0)
229      */
getSupportedActions()230     public long getSupportedActions() {
231         return PlaybackBaseControlGlue.ACTION_PLAY_PAUSE;
232     }
233 
234     /**
235      * Returns the current position of the media item in milliseconds.
236      */
getCurrentPosition()237     public long getCurrentPosition() {
238         return 0;
239     }
240 
241     /**
242      * Returns the current buffered position of the media item in milliseconds.
243      */
getBufferedPosition()244     public long getBufferedPosition() {
245         return 0;
246     }
247 
248     /**
249      * This method is called attached to associated {@link PlaybackGlueHost}.
250      * @param host
251      */
onAttachedToHost(PlaybackGlueHost host)252     public void onAttachedToHost(PlaybackGlueHost host) {
253     }
254 
255     /**
256      * This method is called when current associated {@link PlaybackGlueHost} is attached to a
257      * different {@link PlaybackGlue} or {@link PlaybackGlueHost} is destroyed. Subclass may
258      * override. A typical implementation will release resources (e.g. MediaPlayer or connection
259      * to playback service) in this method.
260      */
onDetachedFromHost()261     public void onDetachedFromHost() {
262     }
263 }
264