• 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 android.support.v17.leanback.media;
18 
19 import static android.support.v17.leanback.widget.PlaybackControlsRow.ShuffleAction.INDEX_OFF;
20 import static android.support.v17.leanback.widget.PlaybackControlsRow.ShuffleAction.INDEX_ON;
21 import static android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction.INDEX_NONE;
22 import static android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction.INDEX_ALL;
23 import static android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction.INDEX_ONE;
24 
25 /**
26  * Base class that wraps underlying media player. The class is used by PlaybackGlue, for example
27  * {@link PlaybackTransportControlGlue} is bound to a PlayerAdapter.
28  * This class is intended to be subclassed, {@link MediaPlayerAdapter} is a concrete subclass
29  * using {@link android.media.MediaPlayer}.
30  */
31 public abstract class PlayerAdapter {
32 
33     /**
34      * Client for client of PlayerAdapter.
35      */
36     public static class Callback {
37 
38         /**
39          * Client for Play/Pause state change. See {@link #isPlaying()}.
40          */
onPlayStateChanged(PlayerAdapter adapter)41         public void onPlayStateChanged(PlayerAdapter adapter) {
42         }
43 
44         /**
45          * Client for {@link #isPrepared()} changed.
46          * @param adapter The adapter that has changed ready state.
47          */
onPreparedStateChanged(PlayerAdapter adapter)48         public void onPreparedStateChanged(PlayerAdapter adapter) {
49         }
50 
51         /**
52          * Client when the current media is finished.
53          * @param adapter The adapter that has just finished current media.
54          */
onPlayCompleted(PlayerAdapter adapter)55         public void onPlayCompleted(PlayerAdapter adapter) {
56         }
57 
58         /**
59          * Event for {@link #getCurrentPosition()} changed.
60          * @param adapter The adapter whose {@link #getCurrentPosition()} changed.
61          */
onCurrentPositionChanged(PlayerAdapter adapter)62         public void onCurrentPositionChanged(PlayerAdapter adapter) {
63         }
64 
65         /**
66          * Event for {@link #getBufferedPosition()} changed.
67          * @param adapter The adapter whose {@link #getBufferedPosition()} changed.
68          */
onBufferedPositionChanged(PlayerAdapter adapter)69         public void onBufferedPositionChanged(PlayerAdapter adapter) {
70         }
71 
72         /**
73          * Event for {@link #getDuration()} changed. Usually the duration does not change
74          * after playing except for live stream.
75          * @param adapter The adapter whose {@link #getDuration()} changed.
76          */
onDurationChanged(PlayerAdapter adapter)77         public void onDurationChanged(PlayerAdapter adapter) {
78         }
79 
80         /**
81          * Event for video size changed.
82          * @param adapter The adapter whose video size has been detected or changed.
83          * @param width Intrinsic width of the video.
84          * @param height Intrinsic height of the video.
85          */
onVideoSizeChanged(PlayerAdapter adapter, int width, int height)86         public void onVideoSizeChanged(PlayerAdapter adapter, int width, int height) {
87         }
88 
89         /**
90          * Event for error.
91          * @param adapter The adapter that encounters error.
92          * @param errorCode Optional error code, specific to implementation.
93          * @param errorMessage Optional error message, specific to implementation.
94          */
onError(PlayerAdapter adapter, int errorCode, String errorMessage)95         public void onError(PlayerAdapter adapter, int errorCode, String errorMessage) {
96         }
97 
98         /**
99          * Event for buffering start or stop. Initial default value is false.
100          * @param adapter The adapter that begins buffering or finishes buffering.
101          * @param start True for buffering start, false otherwise.
102          */
onBufferingStateChanged(PlayerAdapter adapter, boolean start)103         public void onBufferingStateChanged(PlayerAdapter adapter, boolean start) {
104         }
105 
106         /**
107          * Event for meta data changed.
108          * @param adapter The adapter that finishes current media item.
109          */
onMetadataChanged(PlayerAdapter adapter)110         public void onMetadataChanged(PlayerAdapter adapter) {
111         }
112     }
113 
114     Callback mCallback;
115 
116     /**
117      * Sets callback for event of PlayerAdapter.
118      * @param callback Client for event of PlayerAdapter.
119      */
setCallback(Callback callback)120     public final void setCallback(Callback callback) {
121         mCallback = callback;
122     }
123 
124     /**
125      * Gets callback for event of PlayerAdapter.
126      * @return Client for event of PlayerAdapter.
127      */
getCallback()128     public final Callback getCallback() {
129         return mCallback;
130     }
131 
132     /**
133      * @return True if media is ready for playback, false otherwise.
134      */
isPrepared()135     public boolean isPrepared() {
136         return true;
137     }
138 
139     /**
140      * Starts the media player.
141      */
play()142     public abstract void play();
143 
144     /**
145      * Pauses the media player.
146      */
pause()147     public abstract void pause();
148 
149     /**
150      * Optional method. Override this method if {@link #getSupportedActions()} include
151      * {@link PlaybackBaseControlGlue#ACTION_SKIP_TO_NEXT} to skip
152      * to next item.
153      */
next()154     public void next() {
155     }
156 
157     /**
158      * Optional method. Override this method if {@link #getSupportedActions()} include
159      * {@link PlaybackBaseControlGlue#ACTION_SKIP_TO_PREVIOUS} to skip
160      * to previous item.
161      */
previous()162     public void previous() {
163     }
164 
165     /**
166      * Optional method. Override this method if {@link #getSupportedActions()} include
167      * {@link PlaybackBaseControlGlue#ACTION_FAST_FORWARD} to fast
168      * forward current media item.
169      */
fastForward()170     public void fastForward() {
171     }
172 
173     /**
174      * Optional method. Override this method if {@link #getSupportedActions()} include
175      * {@link PlaybackBaseControlGlue#ACTION_REWIND} to rewind in
176      * current media item.
177      */
rewind()178     public void rewind() {
179     }
180 
181     /**
182      * Seek to new position.
183      * @param positionInMs New position in milliseconds.
184      */
seekTo(long positionInMs)185     public void seekTo(long positionInMs) {
186     }
187 
188     /**
189      * Implement this method to enable or disable progress updating.
190      * @param enable True to enable progress updating, false otherwise.
191      */
setProgressUpdatingEnabled(boolean enable)192     public void setProgressUpdatingEnabled(boolean enable) {
193     }
194 
195     /**
196      * Optional method. Override this method if {@link #getSupportedActions()} include
197      * {@link PlaybackBaseControlGlue#ACTION_SHUFFLE} to set the shuffle action.
198      *
199      * @param shuffleActionIndex The repeat action. Must be one of the followings:
200      *                           {@link android.support.v17.leanback.widget.PlaybackControlsRow.ShuffleAction#INDEX_OFF}
201      *                           {@link android.support.v17.leanback.widget.PlaybackControlsRow.ShuffleAction#INDEX_ON}
202      */
setShuffleAction(int shuffleActionIndex)203     public void setShuffleAction(int shuffleActionIndex) {
204     }
205 
206     /**
207      * Optional method. Override this method if {@link #getSupportedActions()} include
208      * {@link PlaybackBaseControlGlue#ACTION_REPEAT} to set the repeat action.
209      *
210      * @param repeatActionIndex The shuffle action. Must be one of the followings:
211      *                          {@link android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_ONE}
212      *                          {@link android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_ALL},
213      *                          {@link android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction#INDEX_NONE},
214      */
setRepeatAction(int repeatActionIndex)215     public void setRepeatAction(int repeatActionIndex) {
216     }
217 
218     /**
219      * Returns true if media is currently playing.
220      */
isPlaying()221     public boolean isPlaying() {
222         return false;
223     }
224 
225     /**
226      * Returns the duration of the media item in milliseconds.
227      */
getDuration()228     public long getDuration() {
229         return 0;
230     }
231 
232     /**
233      * Return xor combination of values defined in PlaybackBaseControlGlue.
234      * Default is PLAY_PAUSE (unless subclass enforce to be 0)
235      */
getSupportedActions()236     public long getSupportedActions() {
237         return PlaybackBaseControlGlue.ACTION_PLAY_PAUSE;
238     }
239 
240     /**
241      * Returns the current position of the media item in milliseconds.
242      */
getCurrentPosition()243     public long getCurrentPosition() {
244         return 0;
245     }
246 
247     /**
248      * Returns the current buffered position of the media item in milliseconds.
249      */
getBufferedPosition()250     public long getBufferedPosition() {
251         return 0;
252     }
253 
254     /**
255      * This method is called attached to associated {@link PlaybackGlueHost}.
256      * @param host
257      */
onAttachedToHost(PlaybackGlueHost host)258     public void onAttachedToHost(PlaybackGlueHost host) {
259     }
260 
261     /**
262      * This method is called when current associated {@link PlaybackGlueHost} is attached to a
263      * different {@link PlaybackGlue} or {@link PlaybackGlueHost} is destroyed. Subclass may
264      * override. A typical implementation will release resources (e.g. MediaPlayer or connection
265      * to playback service) in this method.
266      */
onDetachedFromHost()267     public void onDetachedFromHost() {
268     }
269 }
270