The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.media; |
| 18 | |
Wei Jia | d93fcf4 | 2015-02-09 16:05:53 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 20 | import android.app.ActivityThread; |
| 21 | import android.app.AppOpsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.res.AssetFileDescriptor; |
| 25 | import android.net.Uri; |
| 26 | import android.os.Handler; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 27 | import android.os.HandlerThread; |
Andreas Huber | d2506a5 | 2014-01-29 10:32:46 -0800 | [diff] [blame] | 28 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.Looper; |
| 30 | import android.os.Message; |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 31 | import android.os.Parcel; |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 32 | import android.os.Parcelable; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 33 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.PowerManager; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 35 | import android.os.RemoteException; |
| 36 | import android.os.ServiceManager; |
Jeff Sharkey | 3b566b8 | 2014-11-12 10:39:56 -0800 | [diff] [blame] | 37 | import android.provider.Settings; |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 38 | import android.system.ErrnoException; |
| 39 | import android.system.OsConstants; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.util.Log; |
| 41 | import android.view.Surface; |
| 42 | import android.view.SurfaceHolder; |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 43 | import android.graphics.SurfaceTexture; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.media.AudioManager; |
Lajos Molnar | 1c56a67 | 2013-08-15 12:05:52 -0700 | [diff] [blame] | 45 | import android.media.MediaFormat; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 46 | import android.media.MediaTimeProvider; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 47 | import android.media.SubtitleController; |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 48 | import android.media.SubtitleController.Anchor; |
Chong Zhang | 83ddaf6 | 2013-08-06 09:43:22 -0700 | [diff] [blame] | 49 | import android.media.SubtitleData; |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 50 | import android.media.SubtitleTrack.RenderingWidget; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 52 | import com.android.internal.app.IAppOpsService; |
| 53 | |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 54 | import libcore.io.IoBridge; |
| 55 | import libcore.io.Libcore; |
| 56 | |
| 57 | import java.io.ByteArrayOutputStream; |
James Dong | e00b6f3 | 2012-04-11 21:18:43 -0700 | [diff] [blame] | 58 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import java.io.FileDescriptor; |
James Dong | e00b6f3 | 2012-04-11 21:18:43 -0700 | [diff] [blame] | 60 | import java.io.FileInputStream; |
Jeff Sharkey | 87d76f6 | 2014-12-09 15:27:19 -0800 | [diff] [blame] | 61 | import java.io.FileNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import java.io.IOException; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 63 | import java.io.InputStream; |
| 64 | import java.lang.Runnable; |
Wei Jia | d93fcf4 | 2015-02-09 16:05:53 -0800 | [diff] [blame] | 65 | import java.lang.annotation.Retention; |
| 66 | import java.lang.annotation.RetentionPolicy; |
John Grossman | 720aa28 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 67 | import java.net.InetSocketAddress; |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 68 | import java.util.Map; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 69 | import java.util.Scanner; |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 70 | import java.util.Set; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 71 | import java.util.Vector; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import java.lang.ref.WeakReference; |
| 73 | |
| 74 | /** |
| 75 | * MediaPlayer class can be used to control playback |
| 76 | * of audio/video files and streams. An example on how to use the methods in |
| 77 | * this class can be found in {@link android.widget.VideoView}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | * |
| 79 | * <p>Topics covered here are: |
| 80 | * <ol> |
| 81 | * <li><a href="#StateDiagram">State Diagram</a> |
| 82 | * <li><a href="#Valid_and_Invalid_States">Valid and Invalid States</a> |
| 83 | * <li><a href="#Permissions">Permissions</a> |
James Dong | 9ddb788 | 2011-06-14 14:01:57 -0700 | [diff] [blame] | 84 | * <li><a href="#Callbacks">Register informational and error callbacks</a> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | * </ol> |
| 86 | * |
Joe Fernandez | 61fd1e8 | 2011-10-26 13:39:11 -0700 | [diff] [blame] | 87 | * <div class="special reference"> |
| 88 | * <h3>Developer Guides</h3> |
| 89 | * <p>For more information about how to use MediaPlayer, read the |
| 90 | * <a href="{@docRoot}guide/topics/media/mediaplayer.html">Media Playback</a> developer guide.</p> |
| 91 | * </div> |
| 92 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | * <a name="StateDiagram"></a> |
| 94 | * <h3>State Diagram</h3> |
| 95 | * |
| 96 | * <p>Playback control of audio/video files and streams is managed as a state |
| 97 | * machine. The following diagram shows the life cycle and the states of a |
| 98 | * MediaPlayer object driven by the supported playback control operations. |
| 99 | * The ovals represent the states a MediaPlayer object may reside |
| 100 | * in. The arcs represent the playback control operations that drive the object |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 101 | * state transition. There are two types of arcs. The arcs with a single arrow |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | * head represent synchronous method calls, while those with |
| 103 | * a double arrow head represent asynchronous method calls.</p> |
| 104 | * |
| 105 | * <p><img src="../../../images/mediaplayer_state_diagram.gif" |
| 106 | * alt="MediaPlayer State diagram" |
| 107 | * border="0" /></p> |
| 108 | * |
| 109 | * <p>From this state diagram, one can see that a MediaPlayer object has the |
| 110 | * following states:</p> |
| 111 | * <ul> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 112 | * <li>When a MediaPlayer object is just created using <code>new</code> or |
| 113 | * after {@link #reset()} is called, it is in the <em>Idle</em> state; and after |
| 114 | * {@link #release()} is called, it is in the <em>End</em> state. Between these |
| 115 | * two states is the life cycle of the MediaPlayer object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | * <ul> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 117 | * <li>There is a subtle but important difference between a newly constructed |
| 118 | * MediaPlayer object and the MediaPlayer object after {@link #reset()} |
| 119 | * is called. It is a programming error to invoke methods such |
| 120 | * as {@link #getCurrentPosition()}, |
| 121 | * {@link #getDuration()}, {@link #getVideoHeight()}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | * {@link #getVideoWidth()}, {@link #setAudioStreamType(int)}, |
| 123 | * {@link #setLooping(boolean)}, |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 124 | * {@link #setVolume(float, float)}, {@link #pause()}, {@link #start()}, |
| 125 | * {@link #stop()}, {@link #seekTo(int)}, {@link #prepare()} or |
| 126 | * {@link #prepareAsync()} in the <em>Idle</em> state for both cases. If any of these |
| 127 | * methods is called right after a MediaPlayer object is constructed, |
| 128 | * the user supplied callback method OnErrorListener.onError() won't be |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | * called by the internal player engine and the object state remains |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 130 | * unchanged; but if these methods are called right after {@link #reset()}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | * the user supplied callback method OnErrorListener.onError() will be |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 132 | * invoked by the internal player engine and the object will be |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | * transfered to the <em>Error</em> state. </li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 134 | * <li>It is also recommended that once |
| 135 | * a MediaPlayer object is no longer being used, call {@link #release()} immediately |
| 136 | * so that resources used by the internal player engine associated with the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | * MediaPlayer object can be released immediately. Resource may include |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 138 | * singleton resources such as hardware acceleration components and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | * failure to call {@link #release()} may cause subsequent instances of |
| 140 | * MediaPlayer objects to fallback to software implementations or fail |
| 141 | * altogether. Once the MediaPlayer |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 142 | * object is in the <em>End</em> state, it can no longer be used and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | * there is no way to bring it back to any other state. </li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 144 | * <li>Furthermore, |
| 145 | * the MediaPlayer objects created using <code>new</code> is in the |
| 146 | * <em>Idle</em> state, while those created with one |
| 147 | * of the overloaded convenient <code>create</code> methods are <em>NOT</em> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | * in the <em>Idle</em> state. In fact, the objects are in the <em>Prepared</em> |
| 149 | * state if the creation using <code>create</code> method is successful. |
| 150 | * </li> |
| 151 | * </ul> |
| 152 | * </li> |
| 153 | * <li>In general, some playback control operation may fail due to various |
| 154 | * reasons, such as unsupported audio/video format, poorly interleaved |
| 155 | * audio/video, resolution too high, streaming timeout, and the like. |
| 156 | * Thus, error reporting and recovery is an important concern under |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 157 | * these circumstances. Sometimes, due to programming errors, invoking a playback |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | * control operation in an invalid state may also occur. Under all these |
| 159 | * error conditions, the internal player engine invokes a user supplied |
| 160 | * OnErrorListener.onError() method if an OnErrorListener has been |
| 161 | * registered beforehand via |
| 162 | * {@link #setOnErrorListener(android.media.MediaPlayer.OnErrorListener)}. |
| 163 | * <ul> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 164 | * <li>It is important to note that once an error occurs, the |
| 165 | * MediaPlayer object enters the <em>Error</em> state (except as noted |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | * above), even if an error listener has not been registered by the application.</li> |
| 167 | * <li>In order to reuse a MediaPlayer object that is in the <em> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 168 | * Error</em> state and recover from the error, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | * {@link #reset()} can be called to restore the object to its <em>Idle</em> |
| 170 | * state.</li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 171 | * <li>It is good programming practice to have your application |
| 172 | * register a OnErrorListener to look out for error notifications from |
| 173 | * the internal player engine.</li> |
hugh kennedy | c6f8ea4 | 2010-04-28 15:04:10 -0400 | [diff] [blame] | 174 | * <li>IllegalStateException is |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | * thrown to prevent programming errors such as calling {@link #prepare()}, |
| 176 | * {@link #prepareAsync()}, or one of the overloaded <code>setDataSource |
| 177 | * </code> methods in an invalid state. </li> |
| 178 | * </ul> |
| 179 | * </li> |
| 180 | * <li>Calling |
| 181 | * {@link #setDataSource(FileDescriptor)}, or |
| 182 | * {@link #setDataSource(String)}, or |
| 183 | * {@link #setDataSource(Context, Uri)}, or |
Chris Watkins | 4eaa293 | 2015-03-20 10:31:42 -0700 | [diff] [blame] | 184 | * {@link #setDataSource(FileDescriptor, long, long)}, or |
| 185 | * {@link #setDataSource(MediaDataSource)} transfers a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | * MediaPlayer object in the <em>Idle</em> state to the |
| 187 | * <em>Initialized</em> state. |
| 188 | * <ul> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 189 | * <li>An IllegalStateException is thrown if |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | * setDataSource() is called in any other state.</li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 191 | * <li>It is good programming |
| 192 | * practice to always look out for <code>IllegalArgumentException</code> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | * and <code>IOException</code> that may be thrown from the overloaded |
| 194 | * <code>setDataSource</code> methods.</li> |
| 195 | * </ul> |
| 196 | * </li> |
| 197 | * <li>A MediaPlayer object must first enter the <em>Prepared</em> state |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 198 | * before playback can be started. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | * <ul> |
| 200 | * <li>There are two ways (synchronous vs. |
| 201 | * asynchronous) that the <em>Prepared</em> state can be reached: |
| 202 | * either a call to {@link #prepare()} (synchronous) which |
| 203 | * transfers the object to the <em>Prepared</em> state once the method call |
| 204 | * returns, or a call to {@link #prepareAsync()} (asynchronous) which |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 205 | * first transfers the object to the <em>Preparing</em> state after the |
| 206 | * call returns (which occurs almost right way) while the internal |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | * player engine continues working on the rest of preparation work |
| 208 | * until the preparation work completes. When the preparation completes or when {@link #prepare()} call returns, |
| 209 | * the internal player engine then calls a user supplied callback method, |
| 210 | * onPrepared() of the OnPreparedListener interface, if an |
| 211 | * OnPreparedListener is registered beforehand via {@link |
| 212 | * #setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)}.</li> |
| 213 | * <li>It is important to note that |
| 214 | * the <em>Preparing</em> state is a transient state, and the behavior |
| 215 | * of calling any method with side effect while a MediaPlayer object is |
| 216 | * in the <em>Preparing</em> state is undefined.</li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 217 | * <li>An IllegalStateException is |
| 218 | * thrown if {@link #prepare()} or {@link #prepareAsync()} is called in |
| 219 | * any other state.</li> |
| 220 | * <li>While in the <em>Prepared</em> state, properties |
| 221 | * such as audio/sound volume, screenOnWhilePlaying, looping can be |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | * adjusted by invoking the corresponding set methods.</li> |
| 223 | * </ul> |
| 224 | * </li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 225 | * <li>To start the playback, {@link #start()} must be called. After |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | * {@link #start()} returns successfully, the MediaPlayer object is in the |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 227 | * <em>Started</em> state. {@link #isPlaying()} can be called to test |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | * whether the MediaPlayer object is in the <em>Started</em> state. |
| 229 | * <ul> |
| 230 | * <li>While in the <em>Started</em> state, the internal player engine calls |
| 231 | * a user supplied OnBufferingUpdateListener.onBufferingUpdate() callback |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 232 | * method if a OnBufferingUpdateListener has been registered beforehand |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | * via {@link #setOnBufferingUpdateListener(OnBufferingUpdateListener)}. |
| 234 | * This callback allows applications to keep track of the buffering status |
| 235 | * while streaming audio/video.</li> |
| 236 | * <li>Calling {@link #start()} has not effect |
| 237 | * on a MediaPlayer object that is already in the <em>Started</em> state.</li> |
| 238 | * </ul> |
| 239 | * </li> |
| 240 | * <li>Playback can be paused and stopped, and the current playback position |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 241 | * can be adjusted. Playback can be paused via {@link #pause()}. When the call to |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | * {@link #pause()} returns, the MediaPlayer object enters the |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 243 | * <em>Paused</em> state. Note that the transition from the <em>Started</em> |
| 244 | * state to the <em>Paused</em> state and vice versa happens |
| 245 | * asynchronously in the player engine. It may take some time before |
| 246 | * the state is updated in calls to {@link #isPlaying()}, and it can be |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | * a number of seconds in the case of streamed content. |
| 248 | * <ul> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 249 | * <li>Calling {@link #start()} to resume playback for a paused |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | * MediaPlayer object, and the resumed playback |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 251 | * position is the same as where it was paused. When the call to |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | * {@link #start()} returns, the paused MediaPlayer object goes back to |
| 253 | * the <em>Started</em> state.</li> |
| 254 | * <li>Calling {@link #pause()} has no effect on |
| 255 | * a MediaPlayer object that is already in the <em>Paused</em> state.</li> |
| 256 | * </ul> |
| 257 | * </li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 258 | * <li>Calling {@link #stop()} stops playback and causes a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | * MediaPlayer in the <em>Started</em>, <em>Paused</em>, <em>Prepared |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 260 | * </em> or <em>PlaybackCompleted</em> state to enter the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | * <em>Stopped</em> state. |
| 262 | * <ul> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 263 | * <li>Once in the <em>Stopped</em> state, playback cannot be started |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | * until {@link #prepare()} or {@link #prepareAsync()} are called to set |
| 265 | * the MediaPlayer object to the <em>Prepared</em> state again.</li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 266 | * <li>Calling {@link #stop()} has no effect on a MediaPlayer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | * object that is already in the <em>Stopped</em> state.</li> |
| 268 | * </ul> |
| 269 | * </li> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 270 | * <li>The playback position can be adjusted with a call to |
| 271 | * {@link #seekTo(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | * <ul> |
| 273 | * <li>Although the asynchronuous {@link #seekTo(int)} |
| 274 | * call returns right way, the actual seek operation may take a while to |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 275 | * finish, especially for audio/video being streamed. When the actual |
| 276 | * seek operation completes, the internal player engine calls a user |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | * supplied OnSeekComplete.onSeekComplete() if an OnSeekCompleteListener |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 278 | * has been registered beforehand via |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | * {@link #setOnSeekCompleteListener(OnSeekCompleteListener)}.</li> |
| 280 | * <li>Please |
| 281 | * note that {@link #seekTo(int)} can also be called in the other states, |
| 282 | * such as <em>Prepared</em>, <em>Paused</em> and <em>PlaybackCompleted |
| 283 | * </em> state.</li> |
| 284 | * <li>Furthermore, the actual current playback position |
| 285 | * can be retrieved with a call to {@link #getCurrentPosition()}, which |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 286 | * is helpful for applications such as a Music player that need to keep |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | * track of the playback progress.</li> |
| 288 | * </ul> |
| 289 | * </li> |
| 290 | * <li>When the playback reaches the end of stream, the playback completes. |
| 291 | * <ul> |
| 292 | * <li>If the looping mode was being set to <var>true</var>with |
| 293 | * {@link #setLooping(boolean)}, the MediaPlayer object shall remain in |
| 294 | * the <em>Started</em> state.</li> |
| 295 | * <li>If the looping mode was set to <var>false |
| 296 | * </var>, the player engine calls a user supplied callback method, |
| 297 | * OnCompletion.onCompletion(), if a OnCompletionListener is registered |
| 298 | * beforehand via {@link #setOnCompletionListener(OnCompletionListener)}. |
| 299 | * The invoke of the callback signals that the object is now in the <em> |
| 300 | * PlaybackCompleted</em> state.</li> |
| 301 | * <li>While in the <em>PlaybackCompleted</em> |
| 302 | * state, calling {@link #start()} can restart the playback from the |
| 303 | * beginning of the audio/video source.</li> |
| 304 | * </ul> |
| 305 | * |
| 306 | * |
| 307 | * <a name="Valid_and_Invalid_States"></a> |
| 308 | * <h3>Valid and invalid states</h3> |
| 309 | * |
| 310 | * <table border="0" cellspacing="0" cellpadding="0"> |
| 311 | * <tr><td>Method Name </p></td> |
| 312 | * <td>Valid Sates </p></td> |
| 313 | * <td>Invalid States </p></td> |
| 314 | * <td>Comments </p></td></tr> |
Eric Laurent | 17cb280 | 2010-08-03 03:50:02 -0700 | [diff] [blame] | 315 | * <tr><td>attachAuxEffect </p></td> |
| 316 | * <td>{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} </p></td> |
| 317 | * <td>{Idle, Error} </p></td> |
| 318 | * <td>This method must be called after setDataSource. |
| 319 | * Calling it does not change the object state. </p></td></tr> |
| 320 | * <tr><td>getAudioSessionId </p></td> |
| 321 | * <td>any </p></td> |
| 322 | * <td>{} </p></td> |
| 323 | * <td>This method can be called in any state and calling it does not change |
| 324 | * the object state. </p></td></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | * <tr><td>getCurrentPosition </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 326 | * <td>{Idle, Initialized, Prepared, Started, Paused, Stopped, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | * PlaybackCompleted} </p></td> |
| 328 | * <td>{Error}</p></td> |
| 329 | * <td>Successful invoke of this method in a valid state does not change the |
| 330 | * state. Calling this method in an invalid state transfers the object |
| 331 | * to the <em>Error</em> state. </p></td></tr> |
| 332 | * <tr><td>getDuration </p></td> |
| 333 | * <td>{Prepared, Started, Paused, Stopped, PlaybackCompleted} </p></td> |
| 334 | * <td>{Idle, Initialized, Error} </p></td> |
| 335 | * <td>Successful invoke of this method in a valid state does not change the |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 336 | * state. Calling this method in an invalid state transfers the object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | * to the <em>Error</em> state. </p></td></tr> |
| 338 | * <tr><td>getVideoHeight </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 339 | * <td>{Idle, Initialized, Prepared, Started, Paused, Stopped, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | * PlaybackCompleted}</p></td> |
| 341 | * <td>{Error}</p></td> |
| 342 | * <td>Successful invoke of this method in a valid state does not change the |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 343 | * state. Calling this method in an invalid state transfers the object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | * to the <em>Error</em> state. </p></td></tr> |
| 345 | * <tr><td>getVideoWidth </p></td> |
| 346 | * <td>{Idle, Initialized, Prepared, Started, Paused, Stopped, |
| 347 | * PlaybackCompleted}</p></td> |
| 348 | * <td>{Error}</p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 349 | * <td>Successful invoke of this method in a valid state does not change |
| 350 | * the state. Calling this method in an invalid state transfers the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | * object to the <em>Error</em> state. </p></td></tr> |
| 352 | * <tr><td>isPlaying </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 353 | * <td>{Idle, Initialized, Prepared, Started, Paused, Stopped, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | * PlaybackCompleted}</p></td> |
| 355 | * <td>{Error}</p></td> |
| 356 | * <td>Successful invoke of this method in a valid state does not change |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 357 | * the state. Calling this method in an invalid state transfers the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | * object to the <em>Error</em> state. </p></td></tr> |
| 359 | * <tr><td>pause </p></td> |
kmccormick | b04c07f | 2013-03-29 15:48:45 -0700 | [diff] [blame] | 360 | * <td>{Started, Paused, PlaybackCompleted}</p></td> |
| 361 | * <td>{Idle, Initialized, Prepared, Stopped, Error}</p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 362 | * <td>Successful invoke of this method in a valid state transfers the |
| 363 | * object to the <em>Paused</em> state. Calling this method in an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | * invalid state transfers the object to the <em>Error</em> state.</p></td></tr> |
| 365 | * <tr><td>prepare </p></td> |
| 366 | * <td>{Initialized, Stopped} </p></td> |
| 367 | * <td>{Idle, Prepared, Started, Paused, PlaybackCompleted, Error} </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 368 | * <td>Successful invoke of this method in a valid state transfers the |
| 369 | * object to the <em>Prepared</em> state. Calling this method in an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | * invalid state throws an IllegalStateException.</p></td></tr> |
| 371 | * <tr><td>prepareAsync </p></td> |
| 372 | * <td>{Initialized, Stopped} </p></td> |
| 373 | * <td>{Idle, Prepared, Started, Paused, PlaybackCompleted, Error} </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 374 | * <td>Successful invoke of this method in a valid state transfers the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | * object to the <em>Preparing</em> state. Calling this method in an |
| 376 | * invalid state throws an IllegalStateException.</p></td></tr> |
| 377 | * <tr><td>release </p></td> |
| 378 | * <td>any </p></td> |
| 379 | * <td>{} </p></td> |
| 380 | * <td>After {@link #release()}, the object is no longer available. </p></td></tr> |
| 381 | * <tr><td>reset </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 382 | * <td>{Idle, Initialized, Prepared, Started, Paused, Stopped, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | * PlaybackCompleted, Error}</p></td> |
| 384 | * <td>{}</p></td> |
| 385 | * <td>After {@link #reset()}, the object is like being just created.</p></td></tr> |
| 386 | * <tr><td>seekTo </p></td> |
| 387 | * <td>{Prepared, Started, Paused, PlaybackCompleted} </p></td> |
| 388 | * <td>{Idle, Initialized, Stopped, Error}</p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 389 | * <td>Successful invoke of this method in a valid state does not change |
| 390 | * the state. Calling this method in an invalid state transfers the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | * object to the <em>Error</em> state. </p></td></tr> |
Jean-Michel Trivi | 79f5cd1 | 2014-07-15 15:42:25 -0700 | [diff] [blame] | 392 | * <tr><td>setAudioAttributes </p></td> |
| 393 | * <td>{Idle, Initialized, Stopped, Prepared, Started, Paused, |
| 394 | * PlaybackCompleted}</p></td> |
| 395 | * <td>{Error}</p></td> |
| 396 | * <td>Successful invoke of this method does not change the state. In order for the |
| 397 | * target audio attributes type to become effective, this method must be called before |
| 398 | * prepare() or prepareAsync().</p></td></tr> |
Eric Laurent | 17cb280 | 2010-08-03 03:50:02 -0700 | [diff] [blame] | 399 | * <tr><td>setAudioSessionId </p></td> |
| 400 | * <td>{Idle} </p></td> |
| 401 | * <td>{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, |
| 402 | * Error} </p></td> |
| 403 | * <td>This method must be called in idle state as the audio session ID must be known before |
| 404 | * calling setDataSource. Calling it does not change the object state. </p></td></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | * <tr><td>setAudioStreamType </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 406 | * <td>{Idle, Initialized, Stopped, Prepared, Started, Paused, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | * PlaybackCompleted}</p></td> |
| 408 | * <td>{Error}</p></td> |
James Dong | 9d96354 | 2010-06-03 10:36:04 -0700 | [diff] [blame] | 409 | * <td>Successful invoke of this method does not change the state. In order for the |
| 410 | * target audio stream type to become effective, this method must be called before |
| 411 | * prepare() or prepareAsync().</p></td></tr> |
Eric Laurent | 17cb280 | 2010-08-03 03:50:02 -0700 | [diff] [blame] | 412 | * <tr><td>setAuxEffectSendLevel </p></td> |
| 413 | * <td>any</p></td> |
| 414 | * <td>{} </p></td> |
| 415 | * <td>Calling this method does not change the object state. </p></td></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | * <tr><td>setDataSource </p></td> |
| 417 | * <td>{Idle} </p></td> |
| 418 | * <td>{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, |
| 419 | * Error} </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 420 | * <td>Successful invoke of this method in a valid state transfers the |
| 421 | * object to the <em>Initialized</em> state. Calling this method in an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | * invalid state throws an IllegalStateException.</p></td></tr> |
| 423 | * <tr><td>setDisplay </p></td> |
| 424 | * <td>any </p></td> |
| 425 | * <td>{} </p></td> |
| 426 | * <td>This method can be called in any state and calling it does not change |
| 427 | * the object state. </p></td></tr> |
Jamie Gennis | b283dc6 | 2011-08-28 16:28:08 -0700 | [diff] [blame] | 428 | * <tr><td>setSurface </p></td> |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 429 | * <td>any </p></td> |
| 430 | * <td>{} </p></td> |
| 431 | * <td>This method can be called in any state and calling it does not change |
| 432 | * the object state. </p></td></tr> |
James Dong | 454014e | 2012-04-28 16:03:55 -0700 | [diff] [blame] | 433 | * <tr><td>setVideoScalingMode </p></td> |
| 434 | * <td>{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} </p></td> |
| 435 | * <td>{Idle, Error}</p></td> |
| 436 | * <td>Successful invoke of this method does not change the state.</p></td></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | * <tr><td>setLooping </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 438 | * <td>{Idle, Initialized, Stopped, Prepared, Started, Paused, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | * PlaybackCompleted}</p></td> |
| 440 | * <td>{Error}</p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 441 | * <td>Successful invoke of this method in a valid state does not change |
| 442 | * the state. Calling this method in an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | * invalid state transfers the object to the <em>Error</em> state.</p></td></tr> |
| 444 | * <tr><td>isLooping </p></td> |
| 445 | * <td>any </p></td> |
| 446 | * <td>{} </p></td> |
| 447 | * <td>This method can be called in any state and calling it does not change |
| 448 | * the object state. </p></td></tr> |
| 449 | * <tr><td>setOnBufferingUpdateListener </p></td> |
| 450 | * <td>any </p></td> |
| 451 | * <td>{} </p></td> |
| 452 | * <td>This method can be called in any state and calling it does not change |
| 453 | * the object state. </p></td></tr> |
| 454 | * <tr><td>setOnCompletionListener </p></td> |
| 455 | * <td>any </p></td> |
| 456 | * <td>{} </p></td> |
| 457 | * <td>This method can be called in any state and calling it does not change |
| 458 | * the object state. </p></td></tr> |
| 459 | * <tr><td>setOnErrorListener </p></td> |
| 460 | * <td>any </p></td> |
| 461 | * <td>{} </p></td> |
| 462 | * <td>This method can be called in any state and calling it does not change |
| 463 | * the object state. </p></td></tr> |
| 464 | * <tr><td>setOnPreparedListener </p></td> |
| 465 | * <td>any </p></td> |
| 466 | * <td>{} </p></td> |
| 467 | * <td>This method can be called in any state and calling it does not change |
| 468 | * the object state. </p></td></tr> |
| 469 | * <tr><td>setOnSeekCompleteListener </p></td> |
| 470 | * <td>any </p></td> |
| 471 | * <td>{} </p></td> |
| 472 | * <td>This method can be called in any state and calling it does not change |
| 473 | * the object state. </p></td></tr> |
| 474 | * <tr><td>setScreenOnWhilePlaying</></td> |
| 475 | * <td>any </p></td> |
| 476 | * <td>{} </p></td> |
| 477 | * <td>This method can be called in any state and calling it does not change |
| 478 | * the object state. </p></td></tr> |
Wei Jia | d93fcf4 | 2015-02-09 16:05:53 -0800 | [diff] [blame] | 479 | * <tr><td>setPlaybackRate</p></td> |
| 480 | * <td>any </p></td> |
| 481 | * <td>{} </p></td> |
| 482 | * <td>This method can be called in any state and calling it does not change |
| 483 | * the object state. </p></td></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | * <tr><td>setVolume </p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 485 | * <td>{Idle, Initialized, Stopped, Prepared, Started, Paused, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | * PlaybackCompleted}</p></td> |
| 487 | * <td>{Error}</p></td> |
| 488 | * <td>Successful invoke of this method does not change the state. |
| 489 | * <tr><td>setWakeMode </p></td> |
| 490 | * <td>any </p></td> |
| 491 | * <td>{} </p></td> |
| 492 | * <td>This method can be called in any state and calling it does not change |
| 493 | * the object state.</p></td></tr> |
| 494 | * <tr><td>start </p></td> |
| 495 | * <td>{Prepared, Started, Paused, PlaybackCompleted}</p></td> |
| 496 | * <td>{Idle, Initialized, Stopped, Error}</p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 497 | * <td>Successful invoke of this method in a valid state transfers the |
| 498 | * object to the <em>Started</em> state. Calling this method in an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | * invalid state transfers the object to the <em>Error</em> state.</p></td></tr> |
| 500 | * <tr><td>stop </p></td> |
| 501 | * <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td> |
| 502 | * <td>{Idle, Initialized, Error}</p></td> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 503 | * <td>Successful invoke of this method in a valid state transfers the |
| 504 | * object to the <em>Stopped</em> state. Calling this method in an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | * invalid state transfers the object to the <em>Error</em> state.</p></td></tr> |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 506 | * <tr><td>getTrackInfo </p></td> |
| 507 | * <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td> |
| 508 | * <td>{Idle, Initialized, Error}</p></td> |
| 509 | * <td>Successful invoke of this method does not change the state.</p></td></tr> |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 510 | * <tr><td>addTimedTextSource </p></td> |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 511 | * <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td> |
| 512 | * <td>{Idle, Initialized, Error}</p></td> |
| 513 | * <td>Successful invoke of this method does not change the state.</p></td></tr> |
| 514 | * <tr><td>selectTrack </p></td> |
| 515 | * <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td> |
| 516 | * <td>{Idle, Initialized, Error}</p></td> |
| 517 | * <td>Successful invoke of this method does not change the state.</p></td></tr> |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 518 | * <tr><td>deselectTrack </p></td> |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 519 | * <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td> |
| 520 | * <td>{Idle, Initialized, Error}</p></td> |
| 521 | * <td>Successful invoke of this method does not change the state.</p></td></tr> |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 522 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | * </table> |
| 524 | * |
| 525 | * <a name="Permissions"></a> |
| 526 | * <h3>Permissions</h3> |
| 527 | * <p>One may need to declare a corresponding WAKE_LOCK permission {@link |
| 528 | * android.R.styleable#AndroidManifestUsesPermission <uses-permission>} |
| 529 | * element. |
| 530 | * |
Dave Burke | fc301b0 | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 531 | * <p>This class requires the {@link android.Manifest.permission#INTERNET} permission |
| 532 | * when used with network-based content. |
| 533 | * |
James Dong | 9ddb788 | 2011-06-14 14:01:57 -0700 | [diff] [blame] | 534 | * <a name="Callbacks"></a> |
| 535 | * <h3>Callbacks</h3> |
| 536 | * <p>Applications may want to register for informational and error |
| 537 | * events in order to be informed of some internal state update and |
| 538 | * possible runtime errors during playback or streaming. Registration for |
| 539 | * these events is done by properly setting the appropriate listeners (via calls |
| 540 | * to |
| 541 | * {@link #setOnPreparedListener(OnPreparedListener)}setOnPreparedListener, |
| 542 | * {@link #setOnVideoSizeChangedListener(OnVideoSizeChangedListener)}setOnVideoSizeChangedListener, |
| 543 | * {@link #setOnSeekCompleteListener(OnSeekCompleteListener)}setOnSeekCompleteListener, |
| 544 | * {@link #setOnCompletionListener(OnCompletionListener)}setOnCompletionListener, |
James Dong | d52ad9c | 2011-06-14 18:20:53 -0700 | [diff] [blame] | 545 | * {@link #setOnBufferingUpdateListener(OnBufferingUpdateListener)}setOnBufferingUpdateListener, |
James Dong | 9ddb788 | 2011-06-14 14:01:57 -0700 | [diff] [blame] | 546 | * {@link #setOnInfoListener(OnInfoListener)}setOnInfoListener, |
| 547 | * {@link #setOnErrorListener(OnErrorListener)}setOnErrorListener, etc). |
| 548 | * In order to receive the respective callback |
| 549 | * associated with these listeners, applications are required to create |
| 550 | * MediaPlayer objects on a thread with its own Looper running (main UI |
| 551 | * thread by default has a Looper running). |
| 552 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | */ |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 554 | public class MediaPlayer implements SubtitleController.Listener |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 555 | { |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 556 | /** |
| 557 | Constant to retrieve only the new metadata since the last |
| 558 | call. |
| 559 | // FIXME: unhide. |
| 560 | // FIXME: add link to getMetadata(boolean, boolean) |
| 561 | {@hide} |
| 562 | */ |
| 563 | public static final boolean METADATA_UPDATE_ONLY = true; |
| 564 | |
| 565 | /** |
| 566 | Constant to retrieve all the metadata. |
| 567 | // FIXME: unhide. |
| 568 | // FIXME: add link to getMetadata(boolean, boolean) |
| 569 | {@hide} |
| 570 | */ |
| 571 | public static final boolean METADATA_ALL = false; |
| 572 | |
| 573 | /** |
| 574 | Constant to enable the metadata filter during retrieval. |
| 575 | // FIXME: unhide. |
| 576 | // FIXME: add link to getMetadata(boolean, boolean) |
| 577 | {@hide} |
| 578 | */ |
| 579 | public static final boolean APPLY_METADATA_FILTER = true; |
| 580 | |
| 581 | /** |
| 582 | Constant to disable the metadata filter during retrieval. |
| 583 | // FIXME: unhide. |
| 584 | // FIXME: add link to getMetadata(boolean, boolean) |
| 585 | {@hide} |
| 586 | */ |
| 587 | public static final boolean BYPASS_METADATA_FILTER = false; |
| 588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | static { |
| 590 | System.loadLibrary("media_jni"); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 591 | native_init(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 593 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | private final static String TAG = "MediaPlayer"; |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 595 | // Name of the remote interface for the media player. Must be kept |
| 596 | // in sync with the 2nd parameter of the IMPLEMENT_META_INTERFACE |
| 597 | // macro invocation in IMediaPlayer.cpp |
| 598 | private final static String IMEDIA_PLAYER = "android.media.IMediaPlayer"; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 599 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 600 | private long mNativeContext; // accessed by native methods |
| 601 | private long mNativeSurfaceTexture; // accessed by native methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | private int mListenerContext; // accessed by native methods |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 603 | private SurfaceHolder mSurfaceHolder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | private EventHandler mEventHandler; |
| 605 | private PowerManager.WakeLock mWakeLock = null; |
| 606 | private boolean mScreenOnWhilePlaying; |
| 607 | private boolean mStayAwake; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 608 | private final IAppOpsService mAppOps; |
| 609 | private int mStreamType = AudioManager.USE_DEFAULT_STREAM_TYPE; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 610 | private int mUsage = -1; |
John Spurlock | bbfd31a | 2015-02-18 11:58:14 -0500 | [diff] [blame] | 611 | private boolean mBypassInterruptionPolicy; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 612 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | /** |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 614 | * Default constructor. Consider using one of the create() methods for |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | * synchronously instantiating a MediaPlayer from a Uri or resource. |
| 616 | * <p>When done with the MediaPlayer, you should call {@link #release()}, |
| 617 | * to free the resources. If not released, too many MediaPlayer instances may |
| 618 | * result in an exception.</p> |
| 619 | */ |
| 620 | public MediaPlayer() { |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 621 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | Looper looper; |
| 623 | if ((looper = Looper.myLooper()) != null) { |
| 624 | mEventHandler = new EventHandler(this, looper); |
| 625 | } else if ((looper = Looper.getMainLooper()) != null) { |
| 626 | mEventHandler = new EventHandler(this, looper); |
| 627 | } else { |
| 628 | mEventHandler = null; |
| 629 | } |
| 630 | |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 631 | mTimeProvider = new TimeProvider(this); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 632 | mOutOfBandSubtitleTracks = new Vector<SubtitleTrack>(); |
| 633 | mOpenSubtitleSources = new Vector<InputStream>(); |
| 634 | mInbandSubtitleTracks = new SubtitleTrack[0]; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 635 | IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE); |
| 636 | mAppOps = IAppOpsService.Stub.asInterface(b); |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 637 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | /* Native setup requires a weak reference to our object. |
| 639 | * It's easier to create it here than in C++. |
| 640 | */ |
| 641 | native_setup(new WeakReference<MediaPlayer>(this)); |
| 642 | } |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 643 | |
| 644 | /* |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 645 | * Update the MediaPlayer SurfaceTexture. |
| 646 | * Call after setting a new display surface. |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 647 | */ |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 648 | private native void _setVideoSurface(Surface surface); |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 649 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 650 | /* Do not change these values (starting with INVOKE_ID) without updating |
| 651 | * their counterparts in include/media/mediaplayer.h! |
| 652 | */ |
| 653 | private static final int INVOKE_ID_GET_TRACK_INFO = 1; |
| 654 | private static final int INVOKE_ID_ADD_EXTERNAL_SOURCE = 2; |
| 655 | private static final int INVOKE_ID_ADD_EXTERNAL_SOURCE_FD = 3; |
| 656 | private static final int INVOKE_ID_SELECT_TRACK = 4; |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 657 | private static final int INVOKE_ID_DESELECT_TRACK = 5; |
James Dong | 454014e | 2012-04-28 16:03:55 -0700 | [diff] [blame] | 658 | private static final int INVOKE_ID_SET_VIDEO_SCALE_MODE = 6; |
Robert Shih | 464da70 | 2014-05-29 10:54:32 -0700 | [diff] [blame] | 659 | private static final int INVOKE_ID_GET_SELECTED_TRACK = 7; |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | /** |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 662 | * Create a request parcel which can be routed to the native media |
| 663 | * player using {@link #invoke(Parcel, Parcel)}. The Parcel |
| 664 | * returned has the proper InterfaceToken set. The caller should |
| 665 | * not overwrite that token, i.e it can only append data to the |
| 666 | * Parcel. |
| 667 | * |
| 668 | * @return A parcel suitable to hold a request for the native |
| 669 | * player. |
Marco Nelissen | 2d2cd32 | 2013-06-14 09:42:26 -0700 | [diff] [blame] | 670 | * {@hide} |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 671 | */ |
Marco Nelissen | 2d2cd32 | 2013-06-14 09:42:26 -0700 | [diff] [blame] | 672 | public Parcel newRequest() { |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 673 | Parcel parcel = Parcel.obtain(); |
| 674 | parcel.writeInterfaceToken(IMEDIA_PLAYER); |
| 675 | return parcel; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Invoke a generic method on the native player using opaque |
| 680 | * parcels for the request and reply. Both payloads' format is a |
| 681 | * convention between the java caller and the native player. |
| 682 | * Must be called after setDataSource to make sure a native player |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 683 | * exists. On failure, a RuntimeException is thrown. |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 684 | * |
| 685 | * @param request Parcel with the data for the extension. The |
| 686 | * caller must use {@link #newRequest()} to get one. |
| 687 | * |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 688 | * @param reply Output parcel with the data returned by the |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 689 | * native player. |
Marco Nelissen | 2d2cd32 | 2013-06-14 09:42:26 -0700 | [diff] [blame] | 690 | * {@hide} |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 691 | */ |
Marco Nelissen | 2d2cd32 | 2013-06-14 09:42:26 -0700 | [diff] [blame] | 692 | public void invoke(Parcel request, Parcel reply) { |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 693 | int retcode = native_invoke(request, reply); |
| 694 | reply.setDataPosition(0); |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 695 | if (retcode != 0) { |
| 696 | throw new RuntimeException("failure code: " + retcode); |
| 697 | } |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /** |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 701 | * Sets the {@link SurfaceHolder} to use for displaying the video |
Gloria Wang | 91784c99 | 2011-08-09 15:29:34 -0700 | [diff] [blame] | 702 | * portion of the media. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 703 | * |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 704 | * Either a surface holder or surface must be set if a display or video sink |
Jamie Gennis | b283dc6 | 2011-08-28 16:28:08 -0700 | [diff] [blame] | 705 | * is needed. Not calling this method or {@link #setSurface(Surface)} |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 706 | * when playing back a video will result in only the audio track being played. |
Gloria Wang | 91784c99 | 2011-08-09 15:29:34 -0700 | [diff] [blame] | 707 | * A null surface holder or surface will result in only the audio track being |
| 708 | * played. |
Glenn Kasten | 5c2faf3 | 2011-06-27 10:10:19 -0700 | [diff] [blame] | 709 | * |
| 710 | * @param sh the SurfaceHolder to use for video display |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 711 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | public void setDisplay(SurfaceHolder sh) { |
| 713 | mSurfaceHolder = sh; |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 714 | Surface surface; |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 715 | if (sh != null) { |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 716 | surface = sh.getSurface(); |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 717 | } else { |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 718 | surface = null; |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 719 | } |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 720 | _setVideoSurface(surface); |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 721 | updateSurfaceScreenOn(); |
| 722 | } |
| 723 | |
| 724 | /** |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 725 | * Sets the {@link Surface} to be used as the sink for the video portion of |
Jamie Gennis | b283dc6 | 2011-08-28 16:28:08 -0700 | [diff] [blame] | 726 | * the media. This is similar to {@link #setDisplay(SurfaceHolder)}, but |
| 727 | * does not support {@link #setScreenOnWhilePlaying(boolean)}. Setting a |
| 728 | * Surface will un-set any Surface or SurfaceHolder that was previously set. |
Gloria Wang | 91784c99 | 2011-08-09 15:29:34 -0700 | [diff] [blame] | 729 | * A null surface will result in only the audio track being played. |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 730 | * |
Jamie Gennis | b283dc6 | 2011-08-28 16:28:08 -0700 | [diff] [blame] | 731 | * If the Surface sends frames to a {@link SurfaceTexture}, the timestamps |
| 732 | * returned from {@link SurfaceTexture#getTimestamp()} will have an |
| 733 | * unspecified zero point. These timestamps cannot be directly compared |
| 734 | * between different media sources, different instances of the same media |
| 735 | * source, or multiple runs of the same program. The timestamp is normally |
| 736 | * monotonically increasing and is unaffected by time-of-day adjustments, |
| 737 | * but it is reset when the position is set. |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 738 | * |
Jamie Gennis | b283dc6 | 2011-08-28 16:28:08 -0700 | [diff] [blame] | 739 | * @param surface The {@link Surface} to be used for the video portion of |
| 740 | * the media. |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 741 | */ |
| 742 | public void setSurface(Surface surface) { |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 743 | if (mScreenOnWhilePlaying && surface != null) { |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 744 | Log.w(TAG, "setScreenOnWhilePlaying(true) is ineffective for Surface"); |
| 745 | } |
| 746 | mSurfaceHolder = null; |
Ted Bonkenburg | 1ee6011 | 2011-07-26 09:51:18 -0700 | [diff] [blame] | 747 | _setVideoSurface(surface); |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 748 | updateSurfaceScreenOn(); |
| 749 | } |
| 750 | |
James Dong | 454014e | 2012-04-28 16:03:55 -0700 | [diff] [blame] | 751 | /* Do not change these video scaling mode values below without updating |
| 752 | * their counterparts in system/window.h! Please do not forget to update |
| 753 | * {@link #isVideoScalingModeSupported} when new video scaling modes |
| 754 | * are added. |
| 755 | */ |
| 756 | /** |
| 757 | * Specifies a video scaling mode. The content is stretched to the |
| 758 | * surface rendering area. When the surface has the same aspect ratio |
| 759 | * as the content, the aspect ratio of the content is maintained; |
| 760 | * otherwise, the aspect ratio of the content is not maintained when video |
Jeff Smith | a45746e | 2012-07-19 14:19:24 -0500 | [diff] [blame] | 761 | * is being rendered. Unlike {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING}, |
James Dong | 454014e | 2012-04-28 16:03:55 -0700 | [diff] [blame] | 762 | * there is no content cropping with this video scaling mode. |
| 763 | */ |
| 764 | public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT = 1; |
| 765 | |
| 766 | /** |
| 767 | * Specifies a video scaling mode. The content is scaled, maintaining |
| 768 | * its aspect ratio. The whole surface area is always used. When the |
| 769 | * aspect ratio of the content is the same as the surface, no content |
| 770 | * is cropped; otherwise, content is cropped to fit the surface. |
| 771 | */ |
| 772 | public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING = 2; |
| 773 | /** |
| 774 | * Sets video scaling mode. To make the target video scaling mode |
| 775 | * effective during playback, this method must be called after |
| 776 | * data source is set. If not called, the default video |
| 777 | * scaling mode is {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT}. |
| 778 | * |
| 779 | * <p> The supported video scaling modes are: |
| 780 | * <ul> |
| 781 | * <li> {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT} |
| 782 | * <li> {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING} |
| 783 | * </ul> |
| 784 | * |
| 785 | * @param mode target video scaling mode. Most be one of the supported |
| 786 | * video scaling modes; otherwise, IllegalArgumentException will be thrown. |
| 787 | * |
| 788 | * @see MediaPlayer#VIDEO_SCALING_MODE_SCALE_TO_FIT |
| 789 | * @see MediaPlayer#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING |
| 790 | */ |
| 791 | public void setVideoScalingMode(int mode) { |
Bryan Mawhinney | 79a9cd4 | 2012-05-18 00:15:56 +0100 | [diff] [blame] | 792 | if (!isVideoScalingModeSupported(mode)) { |
James Dong | 454014e | 2012-04-28 16:03:55 -0700 | [diff] [blame] | 793 | final String msg = "Scaling mode " + mode + " is not supported"; |
| 794 | throw new IllegalArgumentException(msg); |
| 795 | } |
| 796 | Parcel request = Parcel.obtain(); |
| 797 | Parcel reply = Parcel.obtain(); |
Insun Kang | be0ea96 | 2012-05-04 20:52:57 +0900 | [diff] [blame] | 798 | try { |
| 799 | request.writeInterfaceToken(IMEDIA_PLAYER); |
| 800 | request.writeInt(INVOKE_ID_SET_VIDEO_SCALE_MODE); |
Bryan Mawhinney | c3a5cf9 | 2012-05-27 17:42:46 +0100 | [diff] [blame] | 801 | request.writeInt(mode); |
Insun Kang | be0ea96 | 2012-05-04 20:52:57 +0900 | [diff] [blame] | 802 | invoke(request, reply); |
| 803 | } finally { |
| 804 | request.recycle(); |
| 805 | reply.recycle(); |
| 806 | } |
James Dong | 454014e | 2012-04-28 16:03:55 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 809 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | * Convenience method to create a MediaPlayer for a given Uri. |
| 811 | * On success, {@link #prepare()} will already have been called and must not be called again. |
| 812 | * <p>When done with the MediaPlayer, you should call {@link #release()}, |
| 813 | * to free the resources. If not released, too many MediaPlayer instances will |
| 814 | * result in an exception.</p> |
Jean-Michel Trivi | 79f5cd1 | 2014-07-15 15:42:25 -0700 | [diff] [blame] | 815 | * <p>Note that since {@link #prepare()} is called automatically in this method, |
| 816 | * you cannot change the audio stream type (see {@link #setAudioStreamType(int)}), audio |
| 817 | * session ID (see {@link #setAudioSessionId(int)}) or audio attributes |
| 818 | * (see {@link #setAudioAttributes(AudioAttributes)} of the new MediaPlayer.</p> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 819 | * |
| 820 | * @param context the Context to use |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | * @param uri the Uri from which to get the datasource |
| 822 | * @return a MediaPlayer object, or null if creation failed |
| 823 | */ |
| 824 | public static MediaPlayer create(Context context, Uri uri) { |
| 825 | return create (context, uri, null); |
| 826 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | /** |
| 829 | * Convenience method to create a MediaPlayer for a given Uri. |
| 830 | * On success, {@link #prepare()} will already have been called and must not be called again. |
| 831 | * <p>When done with the MediaPlayer, you should call {@link #release()}, |
| 832 | * to free the resources. If not released, too many MediaPlayer instances will |
| 833 | * result in an exception.</p> |
Jean-Michel Trivi | 79f5cd1 | 2014-07-15 15:42:25 -0700 | [diff] [blame] | 834 | * <p>Note that since {@link #prepare()} is called automatically in this method, |
| 835 | * you cannot change the audio stream type (see {@link #setAudioStreamType(int)}), audio |
| 836 | * session ID (see {@link #setAudioSessionId(int)}) or audio attributes |
| 837 | * (see {@link #setAudioAttributes(AudioAttributes)} of the new MediaPlayer.</p> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 838 | * |
| 839 | * @param context the Context to use |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | * @param uri the Uri from which to get the datasource |
| 841 | * @param holder the SurfaceHolder to use for displaying the video |
| 842 | * @return a MediaPlayer object, or null if creation failed |
| 843 | */ |
| 844 | public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder) { |
Jean-Michel Trivi | d70ad7a | 2014-07-15 16:34:17 -0700 | [diff] [blame] | 845 | int s = AudioSystem.newAudioSessionId(); |
| 846 | return create(context, uri, holder, null, s > 0 ? s : 0); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Same factory method as {@link #create(Context, Uri, SurfaceHolder)} but that lets you specify |
| 851 | * the audio attributes and session ID to be used by the new MediaPlayer instance. |
| 852 | * @param context the Context to use |
| 853 | * @param uri the Uri from which to get the datasource |
| 854 | * @param holder the SurfaceHolder to use for displaying the video, may be null. |
| 855 | * @param audioAttributes the {@link AudioAttributes} to be used by the media player. |
| 856 | * @param audioSessionId the audio session ID to be used by the media player, |
Jean-Michel Trivi | 289cc8e | 2014-07-18 18:45:32 -0700 | [diff] [blame] | 857 | * see {@link AudioManager#generateAudioSessionId()} to obtain a new session. |
Jean-Michel Trivi | d70ad7a | 2014-07-15 16:34:17 -0700 | [diff] [blame] | 858 | * @return a MediaPlayer object, or null if creation failed |
| 859 | */ |
| 860 | public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder, |
| 861 | AudioAttributes audioAttributes, int audioSessionId) { |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 862 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | try { |
| 864 | MediaPlayer mp = new MediaPlayer(); |
Jean-Michel Trivi | d70ad7a | 2014-07-15 16:34:17 -0700 | [diff] [blame] | 865 | final AudioAttributes aa = audioAttributes != null ? audioAttributes : |
| 866 | new AudioAttributes.Builder().build(); |
| 867 | mp.setAudioAttributes(aa); |
| 868 | mp.setAudioSessionId(audioSessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | mp.setDataSource(context, uri); |
| 870 | if (holder != null) { |
| 871 | mp.setDisplay(holder); |
| 872 | } |
| 873 | mp.prepare(); |
| 874 | return mp; |
| 875 | } catch (IOException ex) { |
| 876 | Log.d(TAG, "create failed:", ex); |
| 877 | // fall through |
| 878 | } catch (IllegalArgumentException ex) { |
| 879 | Log.d(TAG, "create failed:", ex); |
| 880 | // fall through |
| 881 | } catch (SecurityException ex) { |
| 882 | Log.d(TAG, "create failed:", ex); |
| 883 | // fall through |
| 884 | } |
| 885 | |
| 886 | return null; |
| 887 | } |
| 888 | |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 889 | // Note no convenience method to create a MediaPlayer with SurfaceTexture sink. |
| 890 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | /** |
| 892 | * Convenience method to create a MediaPlayer for a given resource id. |
| 893 | * On success, {@link #prepare()} will already have been called and must not be called again. |
| 894 | * <p>When done with the MediaPlayer, you should call {@link #release()}, |
| 895 | * to free the resources. If not released, too many MediaPlayer instances will |
| 896 | * result in an exception.</p> |
Jean-Michel Trivi | 79f5cd1 | 2014-07-15 15:42:25 -0700 | [diff] [blame] | 897 | * <p>Note that since {@link #prepare()} is called automatically in this method, |
| 898 | * you cannot change the audio stream type (see {@link #setAudioStreamType(int)}), audio |
| 899 | * session ID (see {@link #setAudioSessionId(int)}) or audio attributes |
| 900 | * (see {@link #setAudioAttributes(AudioAttributes)} of the new MediaPlayer.</p> |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 901 | * |
| 902 | * @param context the Context to use |
| 903 | * @param resid the raw resource id (<var>R.raw.<something></var>) for |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | * the resource to use as the datasource |
| 905 | * @return a MediaPlayer object, or null if creation failed |
| 906 | */ |
| 907 | public static MediaPlayer create(Context context, int resid) { |
Jean-Michel Trivi | d70ad7a | 2014-07-15 16:34:17 -0700 | [diff] [blame] | 908 | int s = AudioSystem.newAudioSessionId(); |
| 909 | return create(context, resid, null, s > 0 ? s : 0); |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Same factory method as {@link #create(Context, int)} but that lets you specify the audio |
| 914 | * attributes and session ID to be used by the new MediaPlayer instance. |
| 915 | * @param context the Context to use |
| 916 | * @param resid the raw resource id (<var>R.raw.<something></var>) for |
| 917 | * the resource to use as the datasource |
| 918 | * @param audioAttributes the {@link AudioAttributes} to be used by the media player. |
| 919 | * @param audioSessionId the audio session ID to be used by the media player, |
Jean-Michel Trivi | 289cc8e | 2014-07-18 18:45:32 -0700 | [diff] [blame] | 920 | * see {@link AudioManager#generateAudioSessionId()} to obtain a new session. |
Jean-Michel Trivi | d70ad7a | 2014-07-15 16:34:17 -0700 | [diff] [blame] | 921 | * @return a MediaPlayer object, or null if creation failed |
| 922 | */ |
| 923 | public static MediaPlayer create(Context context, int resid, |
| 924 | AudioAttributes audioAttributes, int audioSessionId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | try { |
| 926 | AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid); |
| 927 | if (afd == null) return null; |
| 928 | |
| 929 | MediaPlayer mp = new MediaPlayer(); |
Jean-Michel Trivi | d70ad7a | 2014-07-15 16:34:17 -0700 | [diff] [blame] | 930 | |
| 931 | final AudioAttributes aa = audioAttributes != null ? audioAttributes : |
| 932 | new AudioAttributes.Builder().build(); |
| 933 | mp.setAudioAttributes(aa); |
| 934 | mp.setAudioSessionId(audioSessionId); |
| 935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); |
| 937 | afd.close(); |
| 938 | mp.prepare(); |
| 939 | return mp; |
| 940 | } catch (IOException ex) { |
| 941 | Log.d(TAG, "create failed:", ex); |
| 942 | // fall through |
| 943 | } catch (IllegalArgumentException ex) { |
| 944 | Log.d(TAG, "create failed:", ex); |
| 945 | // fall through |
| 946 | } catch (SecurityException ex) { |
| 947 | Log.d(TAG, "create failed:", ex); |
| 948 | // fall through |
| 949 | } |
| 950 | return null; |
| 951 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 952 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | /** |
| 954 | * Sets the data source as a content Uri. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 955 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | * @param context the Context to use when resolving the Uri |
| 957 | * @param uri the Content URI of the data you want to play |
| 958 | * @throws IllegalStateException if it is called in an invalid state |
| 959 | */ |
| 960 | public void setDataSource(Context context, Uri uri) |
| 961 | throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 962 | setDataSource(context, uri, null); |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Sets the data source as a content Uri. |
| 967 | * |
| 968 | * @param context the Context to use when resolving the Uri |
| 969 | * @param uri the Content URI of the data you want to play |
| 970 | * @param headers the headers to be sent together with the request for the data |
Ronghua Wu | 48db783 | 2014-08-25 14:47:58 -0700 | [diff] [blame] | 971 | * Note that the cross domain redirection is allowed by default, but that can be |
| 972 | * changed with key/value pairs through the headers parameter with |
| 973 | * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value |
| 974 | * to disallow or allow cross domain redirection. |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 975 | * @throws IllegalStateException if it is called in an invalid state |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 976 | */ |
| 977 | public void setDataSource(Context context, Uri uri, Map<String, String> headers) |
Jeff Sharkey | 3b566b8 | 2014-11-12 10:39:56 -0800 | [diff] [blame] | 978 | throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { |
| 979 | final String scheme = uri.getScheme(); |
| 980 | if (ContentResolver.SCHEME_FILE.equals(scheme)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | setDataSource(uri.getPath()); |
| 982 | return; |
Jeff Sharkey | 3b566b8 | 2014-11-12 10:39:56 -0800 | [diff] [blame] | 983 | } else if (ContentResolver.SCHEME_CONTENT.equals(scheme) |
| 984 | && Settings.AUTHORITY.equals(uri.getAuthority())) { |
| 985 | // Redirect ringtones to go directly to underlying provider |
| 986 | uri = RingtoneManager.getActualDefaultRingtoneUri(context, |
| 987 | RingtoneManager.getDefaultType(uri)); |
Jeff Sharkey | 87d76f6 | 2014-12-09 15:27:19 -0800 | [diff] [blame] | 988 | if (uri == null) { |
| 989 | throw new FileNotFoundException("Failed to resolve default ringtone"); |
| 990 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | AssetFileDescriptor fd = null; |
| 994 | try { |
| 995 | ContentResolver resolver = context.getContentResolver(); |
| 996 | fd = resolver.openAssetFileDescriptor(uri, "r"); |
| 997 | if (fd == null) { |
| 998 | return; |
| 999 | } |
| 1000 | // Note: using getDeclaredLength so that our behavior is the same |
| 1001 | // as previous versions when the content provider is returning |
| 1002 | // a full file. |
| 1003 | if (fd.getDeclaredLength() < 0) { |
| 1004 | setDataSource(fd.getFileDescriptor()); |
| 1005 | } else { |
| 1006 | setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength()); |
| 1007 | } |
| 1008 | return; |
| 1009 | } catch (SecurityException ex) { |
| 1010 | } catch (IOException ex) { |
| 1011 | } finally { |
| 1012 | if (fd != null) { |
| 1013 | fd.close(); |
| 1014 | } |
| 1015 | } |
Dave Burke | fc301b0 | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 1016 | |
Marco Nelissen | a2df605 | 2009-06-09 15:49:23 -0700 | [diff] [blame] | 1017 | Log.d(TAG, "Couldn't open file on client side, trying server side"); |
Andreas Huber | d5f9fa5 | 2013-05-28 14:39:39 -0700 | [diff] [blame] | 1018 | |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 1019 | setDataSource(uri.toString(), headers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * Sets the data source (file-path or http/rtsp URL) to use. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1024 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | * @param path the path of the file, or the http/rtsp URL of the stream you want to play |
| 1026 | * @throws IllegalStateException if it is called in an invalid state |
Glenn Kasten | 55d1eea | 2012-03-09 11:02:31 -0800 | [diff] [blame] | 1027 | * |
| 1028 | * <p>When <code>path</code> refers to a local file, the file may actually be opened by a |
| 1029 | * process other than the calling application. This implies that the pathname |
| 1030 | * should be an absolute path (as any other process runs with unspecified current working |
| 1031 | * directory), and that the pathname should reference a world-readable file. |
| 1032 | * As an alternative, the application could first open the file for reading, |
| 1033 | * and then use the file descriptor form {@link #setDataSource(FileDescriptor)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | */ |
James Dong | e00b6f3 | 2012-04-11 21:18:43 -0700 | [diff] [blame] | 1035 | public void setDataSource(String path) |
| 1036 | throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { |
| 1037 | setDataSource(path, null, null); |
| 1038 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | |
| 1040 | /** |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 1041 | * Sets the data source (file-path or http/rtsp URL) to use. |
| 1042 | * |
| 1043 | * @param path the path of the file, or the http/rtsp URL of the stream you want to play |
| 1044 | * @param headers the headers associated with the http request for the stream you want to play |
| 1045 | * @throws IllegalStateException if it is called in an invalid state |
| 1046 | * @hide pending API council |
| 1047 | */ |
James Dong | 17524dc | 2011-05-04 13:41:58 -0700 | [diff] [blame] | 1048 | public void setDataSource(String path, Map<String, String> headers) |
Dave Burke | fc301b0 | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 1049 | throws IOException, IllegalArgumentException, SecurityException, IllegalStateException |
James Dong | 17524dc | 2011-05-04 13:41:58 -0700 | [diff] [blame] | 1050 | { |
Andreas Huber | e4e7b48 | 2011-05-11 16:54:41 -0700 | [diff] [blame] | 1051 | String[] keys = null; |
| 1052 | String[] values = null; |
| 1053 | |
| 1054 | if (headers != null) { |
| 1055 | keys = new String[headers.size()]; |
| 1056 | values = new String[headers.size()]; |
| 1057 | |
| 1058 | int i = 0; |
| 1059 | for (Map.Entry<String, String> entry: headers.entrySet()) { |
| 1060 | keys[i] = entry.getKey(); |
| 1061 | values[i] = entry.getValue(); |
| 1062 | ++i; |
| 1063 | } |
James Dong | 17524dc | 2011-05-04 13:41:58 -0700 | [diff] [blame] | 1064 | } |
James Dong | e00b6f3 | 2012-04-11 21:18:43 -0700 | [diff] [blame] | 1065 | setDataSource(path, keys, values); |
| 1066 | } |
| 1067 | |
| 1068 | private void setDataSource(String path, String[] keys, String[] values) |
| 1069 | throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { |
Jeff Sharkey | c058a38 | 2013-02-19 14:44:41 -0800 | [diff] [blame] | 1070 | final Uri uri = Uri.parse(path); |
Lajos Molnar | d504ab1 | 2014-08-18 14:26:55 -0700 | [diff] [blame] | 1071 | final String scheme = uri.getScheme(); |
| 1072 | if ("file".equals(scheme)) { |
Jeff Sharkey | c058a38 | 2013-02-19 14:44:41 -0800 | [diff] [blame] | 1073 | path = uri.getPath(); |
Lajos Molnar | d504ab1 | 2014-08-18 14:26:55 -0700 | [diff] [blame] | 1074 | } else if (scheme != null) { |
| 1075 | // handle non-file sources |
| 1076 | nativeSetDataSource( |
| 1077 | MediaHTTPService.createHttpServiceBinderIfNecessary(path), |
| 1078 | path, |
| 1079 | keys, |
| 1080 | values); |
| 1081 | return; |
Jeff Sharkey | c058a38 | 2013-02-19 14:44:41 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | final File file = new File(path); |
James Dong | e00b6f3 | 2012-04-11 21:18:43 -0700 | [diff] [blame] | 1085 | if (file.exists()) { |
| 1086 | FileInputStream is = new FileInputStream(file); |
| 1087 | FileDescriptor fd = is.getFD(); |
| 1088 | setDataSource(fd); |
| 1089 | is.close(); |
| 1090 | } else { |
Lajos Molnar | d504ab1 | 2014-08-18 14:26:55 -0700 | [diff] [blame] | 1091 | throw new IOException("setDataSource failed."); |
James Dong | e00b6f3 | 2012-04-11 21:18:43 -0700 | [diff] [blame] | 1092 | } |
James Dong | 17524dc | 2011-05-04 13:41:58 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Andreas Huber | d2506a5 | 2014-01-29 10:32:46 -0800 | [diff] [blame] | 1095 | private native void nativeSetDataSource( |
| 1096 | IBinder httpServiceBinder, String path, String[] keys, String[] values) |
Dave Burke | fc301b0 | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 1097 | throws IOException, IllegalArgumentException, SecurityException, IllegalStateException; |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 1098 | |
| 1099 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | * Sets the data source (FileDescriptor) to use. It is the caller's responsibility |
| 1101 | * to close the file descriptor. It is safe to do so as soon as this call returns. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1102 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | * @param fd the FileDescriptor for the file you want to play |
| 1104 | * @throws IllegalStateException if it is called in an invalid state |
| 1105 | */ |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1106 | public void setDataSource(FileDescriptor fd) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | throws IOException, IllegalArgumentException, IllegalStateException { |
| 1108 | // intentionally less than LONG_MAX |
| 1109 | setDataSource(fd, 0, 0x7ffffffffffffffL); |
| 1110 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | /** |
Kenny Root | 6ab180a | 2009-06-13 06:16:23 -0500 | [diff] [blame] | 1113 | * Sets the data source (FileDescriptor) to use. The FileDescriptor must be |
| 1114 | * seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | * to close the file descriptor. It is safe to do so as soon as this call returns. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1116 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | * @param fd the FileDescriptor for the file you want to play |
| 1118 | * @param offset the offset into the file where the data to be played starts, in bytes |
| 1119 | * @param length the length in bytes of the data to be played |
| 1120 | * @throws IllegalStateException if it is called in an invalid state |
| 1121 | */ |
Andreas Huber | d5f9fa5 | 2013-05-28 14:39:39 -0700 | [diff] [blame] | 1122 | public void setDataSource(FileDescriptor fd, long offset, long length) |
| 1123 | throws IOException, IllegalArgumentException, IllegalStateException { |
Andreas Huber | d5f9fa5 | 2013-05-28 14:39:39 -0700 | [diff] [blame] | 1124 | _setDataSource(fd, offset, length); |
| 1125 | } |
| 1126 | |
| 1127 | private native void _setDataSource(FileDescriptor fd, long offset, long length) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | throws IOException, IllegalArgumentException, IllegalStateException; |
| 1129 | |
| 1130 | /** |
Chris Watkins | 4eaa293 | 2015-03-20 10:31:42 -0700 | [diff] [blame] | 1131 | * Sets the data source (MediaDataSource) to use. |
| 1132 | * |
| 1133 | * @param dataSource the MediaDataSource for the media you want to play |
| 1134 | * @throws IllegalStateException if it is called in an invalid state |
| 1135 | */ |
| 1136 | public void setDataSource(MediaDataSource dataSource) |
| 1137 | throws IllegalArgumentException, IllegalStateException { |
| 1138 | _setDataSource(dataSource); |
| 1139 | } |
| 1140 | |
| 1141 | private native void _setDataSource(MediaDataSource dataSource) |
| 1142 | throws IllegalArgumentException, IllegalStateException; |
| 1143 | |
| 1144 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | * Prepares the player for playback, synchronously. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1146 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | * After setting the datasource and the display surface, you need to either |
| 1148 | * call prepare() or prepareAsync(). For files, it is OK to call prepare(), |
| 1149 | * which blocks until MediaPlayer is ready for playback. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1150 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | * @throws IllegalStateException if it is called in an invalid state |
| 1152 | */ |
Robert Shih | c42a96d | 2014-07-25 11:08:50 -0700 | [diff] [blame] | 1153 | public void prepare() throws IOException, IllegalStateException { |
| 1154 | _prepare(); |
| 1155 | scanInternalSubtitleTracks(); |
| 1156 | } |
| 1157 | |
| 1158 | private native void _prepare() throws IOException, IllegalStateException; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | /** |
| 1161 | * Prepares the player for playback, asynchronously. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1162 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | * After setting the datasource and the display surface, you need to either |
| 1164 | * call prepare() or prepareAsync(). For streams, you should call prepareAsync(), |
| 1165 | * which returns immediately, rather than blocking until enough data has been |
| 1166 | * buffered. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1167 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | * @throws IllegalStateException if it is called in an invalid state |
| 1169 | */ |
| 1170 | public native void prepareAsync() throws IllegalStateException; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1171 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | /** |
| 1173 | * Starts or resumes playback. If playback had previously been paused, |
| 1174 | * playback will continue from where it was paused. If playback had |
| 1175 | * been stopped, or never started before, playback will start at the |
| 1176 | * beginning. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1177 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | * @throws IllegalStateException if it is called in an invalid state |
| 1179 | */ |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1180 | public void start() throws IllegalStateException { |
| 1181 | if (isRestricted()) { |
| 1182 | _setVolume(0, 0); |
| 1183 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1184 | stayAwake(true); |
| 1185 | _start(); |
| 1186 | } |
| 1187 | |
| 1188 | private native void _start() throws IllegalStateException; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1189 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1190 | private boolean isRestricted() { |
John Spurlock | bbfd31a | 2015-02-18 11:58:14 -0500 | [diff] [blame] | 1191 | if (mBypassInterruptionPolicy) { |
| 1192 | return false; |
| 1193 | } |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1194 | try { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1195 | final int usage = mUsage != -1 ? mUsage |
| 1196 | : AudioAttributes.usageForLegacyStreamType(getAudioStreamType()); |
| 1197 | final int mode = mAppOps.checkAudioOperation(AppOpsManager.OP_PLAY_AUDIO, usage, |
| 1198 | Process.myUid(), ActivityThread.currentPackageName()); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1199 | return mode != AppOpsManager.MODE_ALLOWED; |
| 1200 | } catch (RemoteException e) { |
| 1201 | return false; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | private int getAudioStreamType() { |
| 1206 | if (mStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 1207 | mStreamType = _getAudioStreamType(); |
| 1208 | } |
| 1209 | return mStreamType; |
| 1210 | } |
| 1211 | |
| 1212 | private native int _getAudioStreamType() throws IllegalStateException; |
| 1213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | /** |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1215 | * Stops playback after playback has been stopped or paused. |
| 1216 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | * @throws IllegalStateException if the internal player engine has not been |
| 1218 | * initialized. |
| 1219 | */ |
| 1220 | public void stop() throws IllegalStateException { |
| 1221 | stayAwake(false); |
| 1222 | _stop(); |
| 1223 | } |
| 1224 | |
| 1225 | private native void _stop() throws IllegalStateException; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1227 | /** |
| 1228 | * Pauses playback. Call start() to resume. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1229 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | * @throws IllegalStateException if the internal player engine has not been |
| 1231 | * initialized. |
| 1232 | */ |
| 1233 | public void pause() throws IllegalStateException { |
| 1234 | stayAwake(false); |
| 1235 | _pause(); |
| 1236 | } |
| 1237 | |
| 1238 | private native void _pause() throws IllegalStateException; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 | /** |
| 1241 | * Set the low-level power management behavior for this MediaPlayer. This |
| 1242 | * can be used when the MediaPlayer is not playing through a SurfaceHolder |
| 1243 | * set with {@link #setDisplay(SurfaceHolder)} and thus can use the |
| 1244 | * high-level {@link #setScreenOnWhilePlaying(boolean)} feature. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1245 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | * <p>This function has the MediaPlayer access the low-level power manager |
| 1247 | * service to control the device's power usage while playing is occurring. |
| 1248 | * The parameter is a combination of {@link android.os.PowerManager} wake flags. |
| 1249 | * Use of this method requires {@link android.Manifest.permission#WAKE_LOCK} |
| 1250 | * permission. |
| 1251 | * By default, no attempt is made to keep the device awake during playback. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1252 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | * @param context the Context to use |
| 1254 | * @param mode the power/wake mode to set |
| 1255 | * @see android.os.PowerManager |
| 1256 | */ |
| 1257 | public void setWakeMode(Context context, int mode) { |
| 1258 | boolean washeld = false; |
| 1259 | if (mWakeLock != null) { |
| 1260 | if (mWakeLock.isHeld()) { |
| 1261 | washeld = true; |
| 1262 | mWakeLock.release(); |
| 1263 | } |
| 1264 | mWakeLock = null; |
| 1265 | } |
| 1266 | |
| 1267 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 1268 | mWakeLock = pm.newWakeLock(mode|PowerManager.ON_AFTER_RELEASE, MediaPlayer.class.getName()); |
| 1269 | mWakeLock.setReferenceCounted(false); |
| 1270 | if (washeld) { |
| 1271 | mWakeLock.acquire(); |
| 1272 | } |
| 1273 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1275 | /** |
| 1276 | * Control whether we should use the attached SurfaceHolder to keep the |
| 1277 | * screen on while video playback is occurring. This is the preferred |
| 1278 | * method over {@link #setWakeMode} where possible, since it doesn't |
| 1279 | * require that the application have permission for low-level wake lock |
| 1280 | * access. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1281 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1282 | * @param screenOn Supply true to keep the screen on, false to allow it |
| 1283 | * to turn off. |
| 1284 | */ |
| 1285 | public void setScreenOnWhilePlaying(boolean screenOn) { |
| 1286 | if (mScreenOnWhilePlaying != screenOn) { |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 1287 | if (screenOn && mSurfaceHolder == null) { |
| 1288 | Log.w(TAG, "setScreenOnWhilePlaying(true) is ineffective without a SurfaceHolder"); |
Glenn Kasten | 817c161 | 2011-04-13 08:11:47 -0700 | [diff] [blame] | 1289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | mScreenOnWhilePlaying = screenOn; |
| 1291 | updateSurfaceScreenOn(); |
| 1292 | } |
| 1293 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1294 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | private void stayAwake(boolean awake) { |
| 1296 | if (mWakeLock != null) { |
| 1297 | if (awake && !mWakeLock.isHeld()) { |
| 1298 | mWakeLock.acquire(); |
| 1299 | } else if (!awake && mWakeLock.isHeld()) { |
| 1300 | mWakeLock.release(); |
| 1301 | } |
| 1302 | } |
| 1303 | mStayAwake = awake; |
| 1304 | updateSurfaceScreenOn(); |
| 1305 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1306 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | private void updateSurfaceScreenOn() { |
| 1308 | if (mSurfaceHolder != null) { |
| 1309 | mSurfaceHolder.setKeepScreenOn(mScreenOnWhilePlaying && mStayAwake); |
| 1310 | } |
| 1311 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | /** |
| 1314 | * Returns the width of the video. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1315 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1316 | * @return the width of the video, or 0 if there is no video, |
Jean-Baptiste Queru | ea5f767 | 2009-09-16 15:06:25 -0700 | [diff] [blame] | 1317 | * no display surface was set, or the width has not been determined |
| 1318 | * yet. The OnVideoSizeChangedListener can be registered via |
| 1319 | * {@link #setOnVideoSizeChangedListener(OnVideoSizeChangedListener)} |
| 1320 | * to provide a notification when the width is available. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | */ |
| 1322 | public native int getVideoWidth(); |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1324 | /** |
| 1325 | * Returns the height of the video. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1326 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1327 | * @return the height of the video, or 0 if there is no video, |
Jean-Baptiste Queru | ea5f767 | 2009-09-16 15:06:25 -0700 | [diff] [blame] | 1328 | * no display surface was set, or the height has not been determined |
| 1329 | * yet. The OnVideoSizeChangedListener can be registered via |
| 1330 | * {@link #setOnVideoSizeChangedListener(OnVideoSizeChangedListener)} |
| 1331 | * to provide a notification when the height is available. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | */ |
| 1333 | public native int getVideoHeight(); |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | /** |
| 1336 | * Checks whether the MediaPlayer is playing. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1337 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | * @return true if currently playing, false otherwise |
Scott Main | 099fd80 | 2012-07-03 16:32:11 -0700 | [diff] [blame] | 1339 | * @throws IllegalStateException if the internal player engine has not been |
| 1340 | * initialized or has been released. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1341 | */ |
| 1342 | public native boolean isPlaying(); |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1343 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | /** |
Wei Jia | d93fcf4 | 2015-02-09 16:05:53 -0800 | [diff] [blame] | 1345 | * Specifies resampling as audio mode for variable rate playback, i.e., |
| 1346 | * resample the waveform based on the requested playback rate to get |
| 1347 | * a new waveform, and play back the new waveform at the original sampling |
| 1348 | * frequency. |
| 1349 | * When rate is larger than 1.0, pitch becomes higher. |
| 1350 | * When rate is smaller than 1.0, pitch becomes lower. |
| 1351 | */ |
| 1352 | public static final int PLAYBACK_RATE_AUDIO_MODE_RESAMPLE = 0; |
| 1353 | |
| 1354 | /** |
| 1355 | * Specifies time stretching as audio mode for variable rate playback. |
| 1356 | * Time stretching changes the duration of the audio samples without |
| 1357 | * affecting its pitch. |
| 1358 | * FIXME: implement time strectching. |
| 1359 | * @hide |
| 1360 | */ |
| 1361 | public static final int PLAYBACK_RATE_AUDIO_MODE_STRETCH = 1; |
| 1362 | |
| 1363 | /** @hide */ |
| 1364 | @IntDef( |
| 1365 | value = { |
| 1366 | PLAYBACK_RATE_AUDIO_MODE_RESAMPLE, |
| 1367 | PLAYBACK_RATE_AUDIO_MODE_STRETCH }) |
| 1368 | @Retention(RetentionPolicy.SOURCE) |
| 1369 | public @interface PlaybackRateAudioMode {} |
| 1370 | |
| 1371 | /** |
| 1372 | * Sets playback rate and audio mode. |
| 1373 | * |
| 1374 | * <p> The supported audio modes are: |
| 1375 | * <ul> |
| 1376 | * <li> {@link #PLAYBACK_RATE_AUDIO_MODE_RESAMPLE} |
| 1377 | * </ul> |
| 1378 | * |
| 1379 | * @param rate the ratio between desired playback rate and normal one. |
| 1380 | * @param audioMode audio playback mode. Must be one of the supported |
| 1381 | * audio modes. |
| 1382 | * |
| 1383 | * @throws IllegalStateException if the internal player engine has not been |
| 1384 | * initialized. |
| 1385 | * @throws IllegalArgumentException if audioMode is not supported. |
| 1386 | */ |
| 1387 | public void setPlaybackRate(float rate, @PlaybackRateAudioMode int audioMode) { |
| 1388 | if (!isAudioPlaybackModeSupported(audioMode)) { |
| 1389 | final String msg = "Audio playback mode " + audioMode + " is not supported"; |
| 1390 | throw new IllegalArgumentException(msg); |
| 1391 | } |
| 1392 | _setPlaybackRate(rate); |
| 1393 | } |
| 1394 | |
| 1395 | private native void _setPlaybackRate(float rate) throws IllegalStateException; |
| 1396 | |
| 1397 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | * Seeks to specified time position. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1399 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | * @param msec the offset in milliseconds from the start to seek to |
| 1401 | * @throws IllegalStateException if the internal player engine has not been |
| 1402 | * initialized |
| 1403 | */ |
| 1404 | public native void seekTo(int msec) throws IllegalStateException; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | /** |
| 1407 | * Gets the current playback position. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1408 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | * @return the current position in milliseconds |
| 1410 | */ |
| 1411 | public native int getCurrentPosition(); |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | /** |
| 1414 | * Gets the duration of the file. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1415 | * |
Andreas Huber | 7cd281c | 2013-04-11 11:07:03 -0700 | [diff] [blame] | 1416 | * @return the duration in milliseconds, if no duration is available |
| 1417 | * (for example, if streaming live content), -1 is returned. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | */ |
| 1419 | public native int getDuration(); |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1420 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | /** |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 1422 | * Gets the media metadata. |
| 1423 | * |
| 1424 | * @param update_only controls whether the full set of available |
| 1425 | * metadata is returned or just the set that changed since the |
| 1426 | * last call. See {@see #METADATA_UPDATE_ONLY} and {@see |
| 1427 | * #METADATA_ALL}. |
| 1428 | * |
| 1429 | * @param apply_filter if true only metadata that matches the |
| 1430 | * filter is returned. See {@see #APPLY_METADATA_FILTER} and {@see |
| 1431 | * #BYPASS_METADATA_FILTER}. |
| 1432 | * |
| 1433 | * @return The metadata, possibly empty. null if an error occured. |
| 1434 | // FIXME: unhide. |
| 1435 | * {@hide} |
| 1436 | */ |
| 1437 | public Metadata getMetadata(final boolean update_only, |
| 1438 | final boolean apply_filter) { |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1439 | Parcel reply = Parcel.obtain(); |
| 1440 | Metadata data = new Metadata(); |
| 1441 | |
| 1442 | if (!native_getMetadata(update_only, apply_filter, reply)) { |
| 1443 | reply.recycle(); |
| 1444 | return null; |
| 1445 | } |
| 1446 | |
| 1447 | // Metadata takes over the parcel, don't recycle it unless |
| 1448 | // there is an error. |
| 1449 | if (!data.parse(reply)) { |
| 1450 | reply.recycle(); |
| 1451 | return null; |
| 1452 | } |
| 1453 | return data; |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * Set a filter for the metadata update notification and update |
| 1458 | * retrieval. The caller provides 2 set of metadata keys, allowed |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1459 | * and blocked. The blocked set always takes precedence over the |
| 1460 | * allowed one. |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 1461 | * Metadata.MATCH_ALL and Metadata.MATCH_NONE are 2 sets available as |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1462 | * shorthands to allow/block all or no metadata. |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 1463 | * |
| 1464 | * By default, there is no filter set. |
| 1465 | * |
| 1466 | * @param allow Is the set of metadata the client is interested |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1467 | * in receiving new notifications for. |
| 1468 | * @param block Is the set of metadata the client is not interested |
| 1469 | * in receiving new notifications for. |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 1470 | * @return The call status code. |
| 1471 | * |
| 1472 | // FIXME: unhide. |
| 1473 | * {@hide} |
| 1474 | */ |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1475 | public int setMetadataFilter(Set<Integer> allow, Set<Integer> block) { |
| 1476 | // Do our serialization manually instead of calling |
| 1477 | // Parcel.writeArray since the sets are made of the same type |
| 1478 | // we avoid paying the price of calling writeValue (used by |
| 1479 | // writeArray) which burns an extra int per element to encode |
| 1480 | // the type. |
| 1481 | Parcel request = newRequest(); |
| 1482 | |
| 1483 | // The parcel starts already with an interface token. There |
| 1484 | // are 2 filters. Each one starts with a 4bytes number to |
| 1485 | // store the len followed by a number of int (4 bytes as well) |
| 1486 | // representing the metadata type. |
| 1487 | int capacity = request.dataSize() + 4 * (1 + allow.size() + 1 + block.size()); |
| 1488 | |
| 1489 | if (request.dataCapacity() < capacity) { |
| 1490 | request.setDataCapacity(capacity); |
| 1491 | } |
| 1492 | |
| 1493 | request.writeInt(allow.size()); |
| 1494 | for(Integer t: allow) { |
| 1495 | request.writeInt(t); |
| 1496 | } |
| 1497 | request.writeInt(block.size()); |
| 1498 | for(Integer t: block) { |
| 1499 | request.writeInt(t); |
| 1500 | } |
| 1501 | return native_setMetadataFilter(request); |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | /** |
Marco Nelissen | 84b8320 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1505 | * Set the MediaPlayer to start when this MediaPlayer finishes playback |
| 1506 | * (i.e. reaches the end of the stream). |
| 1507 | * The media framework will attempt to transition from this player to |
| 1508 | * the next as seamlessly as possible. The next player can be set at |
| 1509 | * any time before completion. The next player must be prepared by the |
| 1510 | * app, and the application should not call start() on it. |
| 1511 | * The next MediaPlayer must be different from 'this'. An exception |
| 1512 | * will be thrown if next == this. |
| 1513 | * The application may call setNextMediaPlayer(null) to indicate no |
| 1514 | * next player should be started at the end of playback. |
| 1515 | * If the current player is looping, it will keep looping and the next |
| 1516 | * player will not be started. |
| 1517 | * |
| 1518 | * @param next the player to start after this one completes playback. |
| 1519 | * |
Marco Nelissen | 84b8320 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1520 | */ |
| 1521 | public native void setNextMediaPlayer(MediaPlayer next); |
| 1522 | |
| 1523 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | * Releases resources associated with this MediaPlayer object. |
| 1525 | * It is considered good practice to call this method when you're |
James Dong | 89ca698 | 2011-11-14 19:01:26 -0800 | [diff] [blame] | 1526 | * done using the MediaPlayer. In particular, whenever an Activity |
| 1527 | * of an application is paused (its onPause() method is called), |
| 1528 | * or stopped (its onStop() method is called), this method should be |
| 1529 | * invoked to release the MediaPlayer object, unless the application |
| 1530 | * has a special need to keep the object around. In addition to |
| 1531 | * unnecessary resources (such as memory and instances of codecs) |
| 1532 | * being held, failure to call this method immediately if a |
| 1533 | * MediaPlayer object is no longer needed may also lead to |
| 1534 | * continuous battery consumption for mobile devices, and playback |
| 1535 | * failure for other applications if no multiple instances of the |
| 1536 | * same codec are supported on a device. Even if multiple instances |
| 1537 | * of the same codec are supported, some performance degradation |
| 1538 | * may be expected when unnecessary multiple instances are used |
| 1539 | * at the same time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1540 | */ |
| 1541 | public void release() { |
| 1542 | stayAwake(false); |
| 1543 | updateSurfaceScreenOn(); |
| 1544 | mOnPreparedListener = null; |
| 1545 | mOnBufferingUpdateListener = null; |
| 1546 | mOnCompletionListener = null; |
| 1547 | mOnSeekCompleteListener = null; |
| 1548 | mOnErrorListener = null; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1549 | mOnInfoListener = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1550 | mOnVideoSizeChangedListener = null; |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 1551 | mOnTimedTextListener = null; |
Lajos Molnar | a67a443 | 2013-09-06 06:30:35 -0700 | [diff] [blame] | 1552 | if (mTimeProvider != null) { |
| 1553 | mTimeProvider.close(); |
| 1554 | mTimeProvider = null; |
| 1555 | } |
Chong Zhang | 83ddaf6 | 2013-08-06 09:43:22 -0700 | [diff] [blame] | 1556 | mOnSubtitleDataListener = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1557 | _release(); |
| 1558 | } |
| 1559 | |
| 1560 | private native void _release(); |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1561 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1562 | /** |
| 1563 | * Resets the MediaPlayer to its uninitialized state. After calling |
| 1564 | * this method, you will have to initialize it again by setting the |
| 1565 | * data source and calling prepare(). |
| 1566 | */ |
| 1567 | public void reset() { |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 1568 | mSelectedSubtitleTrackIndex = -1; |
| 1569 | synchronized(mOpenSubtitleSources) { |
| 1570 | for (final InputStream is: mOpenSubtitleSources) { |
| 1571 | try { |
| 1572 | is.close(); |
| 1573 | } catch (IOException e) { |
| 1574 | } |
| 1575 | } |
| 1576 | mOpenSubtitleSources.clear(); |
| 1577 | } |
| 1578 | mOutOfBandSubtitleTracks.clear(); |
| 1579 | mInbandSubtitleTracks = new SubtitleTrack[0]; |
| 1580 | if (mSubtitleController != null) { |
| 1581 | mSubtitleController.reset(); |
| 1582 | } |
Lajos Molnar | a67a443 | 2013-09-06 06:30:35 -0700 | [diff] [blame] | 1583 | if (mTimeProvider != null) { |
| 1584 | mTimeProvider.close(); |
| 1585 | mTimeProvider = null; |
| 1586 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 1587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | stayAwake(false); |
| 1589 | _reset(); |
| 1590 | // make sure none of the listeners get called anymore |
Lajos Molnar | a67a443 | 2013-09-06 06:30:35 -0700 | [diff] [blame] | 1591 | if (mEventHandler != null) { |
| 1592 | mEventHandler.removeCallbacksAndMessages(null); |
| 1593 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | private native void _reset(); |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | /** |
| 1599 | * Sets the audio stream type for this MediaPlayer. See {@link AudioManager} |
James Dong | 9d96354 | 2010-06-03 10:36:04 -0700 | [diff] [blame] | 1600 | * for a list of stream types. Must call this method before prepare() or |
| 1601 | * prepareAsync() in order for the target stream type to become effective |
| 1602 | * thereafter. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1603 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | * @param streamtype the audio stream type |
| 1605 | * @see android.media.AudioManager |
| 1606 | */ |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1607 | public void setAudioStreamType(int streamtype) { |
| 1608 | _setAudioStreamType(streamtype); |
| 1609 | mStreamType = streamtype; |
| 1610 | } |
| 1611 | |
| 1612 | private native void _setAudioStreamType(int streamtype); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1613 | |
Jean-Michel Trivi | 8df982d | 2014-06-26 12:05:16 -0700 | [diff] [blame] | 1614 | // Keep KEY_PARAMETER_* in sync with include/media/mediaplayer.h |
| 1615 | private final static int KEY_PARAMETER_AUDIO_ATTRIBUTES = 1400; |
| 1616 | /** |
| 1617 | * Sets the parameter indicated by key. |
| 1618 | * @param key key indicates the parameter to be set. |
| 1619 | * @param value value of the parameter to be set. |
| 1620 | * @return true if the parameter is set successfully, false otherwise |
| 1621 | * {@hide} |
| 1622 | */ |
| 1623 | private native boolean setParameter(int key, Parcel value); |
| 1624 | |
| 1625 | /** |
Jean-Michel Trivi | 79f5cd1 | 2014-07-15 15:42:25 -0700 | [diff] [blame] | 1626 | * Sets the audio attributes for this MediaPlayer. |
| 1627 | * See {@link AudioAttributes} for how to build and configure an instance of this class. |
| 1628 | * You must call this method before {@link #prepare()} or {@link #prepareAsync()} in order |
| 1629 | * for the audio attributes to become effective thereafter. |
Jean-Michel Trivi | 8df982d | 2014-06-26 12:05:16 -0700 | [diff] [blame] | 1630 | * @param attributes a non-null set of audio attributes |
| 1631 | */ |
| 1632 | public void setAudioAttributes(AudioAttributes attributes) throws IllegalArgumentException { |
| 1633 | if (attributes == null) { |
Jean-Michel Trivi | 79f5cd1 | 2014-07-15 15:42:25 -0700 | [diff] [blame] | 1634 | final String msg = "Cannot set AudioAttributes to null"; |
Jean-Michel Trivi | 8df982d | 2014-06-26 12:05:16 -0700 | [diff] [blame] | 1635 | throw new IllegalArgumentException(msg); |
| 1636 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1637 | mUsage = attributes.getUsage(); |
John Spurlock | bbfd31a | 2015-02-18 11:58:14 -0500 | [diff] [blame] | 1638 | mBypassInterruptionPolicy = (attributes.getFlags() |
| 1639 | & AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY) != 0; |
Jean-Michel Trivi | 8df982d | 2014-06-26 12:05:16 -0700 | [diff] [blame] | 1640 | Parcel pattributes = Parcel.obtain(); |
| 1641 | attributes.writeToParcel(pattributes, AudioAttributes.FLATTEN_TAGS); |
| 1642 | setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, pattributes); |
| 1643 | pattributes.recycle(); |
| 1644 | } |
| 1645 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | /** |
| 1647 | * Sets the player to be looping or non-looping. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1648 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | * @param looping whether to loop or not |
| 1650 | */ |
| 1651 | public native void setLooping(boolean looping); |
| 1652 | |
| 1653 | /** |
| 1654 | * Checks whether the MediaPlayer is looping or non-looping. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1655 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | * @return true if the MediaPlayer is currently looping, false otherwise |
| 1657 | */ |
| 1658 | public native boolean isLooping(); |
| 1659 | |
| 1660 | /** |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 1661 | * Sets the volume on this player. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | * This API is recommended for balancing the output of audio streams |
| 1663 | * within an application. Unless you are writing an application to |
| 1664 | * control user settings, this API should be used in preference to |
| 1665 | * {@link AudioManager#setStreamVolume(int, int, int)} which sets the volume of ALL streams of |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 1666 | * a particular type. Note that the passed volume values are raw scalars in range 0.0 to 1.0. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | * UI controls should be scaled logarithmically. |
| 1668 | * |
| 1669 | * @param leftVolume left volume scalar |
| 1670 | * @param rightVolume right volume scalar |
| 1671 | */ |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 1672 | /* |
| 1673 | * FIXME: Merge this into javadoc comment above when setVolume(float) is not @hide. |
| 1674 | * The single parameter form below is preferred if the channel volumes don't need |
| 1675 | * to be set independently. |
| 1676 | */ |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1677 | public void setVolume(float leftVolume, float rightVolume) { |
| 1678 | if (isRestricted()) { |
| 1679 | return; |
| 1680 | } |
| 1681 | _setVolume(leftVolume, rightVolume); |
| 1682 | } |
| 1683 | |
| 1684 | private native void _setVolume(float leftVolume, float rightVolume); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1685 | |
| 1686 | /** |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 1687 | * Similar, excepts sets volume of all channels to same value. |
| 1688 | * @hide |
| 1689 | */ |
| 1690 | public void setVolume(float volume) { |
| 1691 | setVolume(volume, volume); |
| 1692 | } |
| 1693 | |
| 1694 | /** |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1695 | * Sets the audio session ID. |
| 1696 | * |
Eric Laurent | 17cb280 | 2010-08-03 03:50:02 -0700 | [diff] [blame] | 1697 | * @param sessionId the audio session ID. |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1698 | * The audio session ID is a system wide unique identifier for the audio stream played by |
| 1699 | * this MediaPlayer instance. |
| 1700 | * The primary use of the audio session ID is to associate audio effects to a particular |
| 1701 | * instance of MediaPlayer: if an audio session ID is provided when creating an audio effect, |
| 1702 | * this effect will be applied only to the audio content of media players within the same |
| 1703 | * audio session and not to the output mix. |
| 1704 | * When created, a MediaPlayer instance automatically generates its own audio session ID. |
| 1705 | * However, it is possible to force this player to be part of an already existing audio session |
| 1706 | * by calling this method. |
| 1707 | * This method must be called before one of the overloaded <code> setDataSource </code> methods. |
| 1708 | * @throws IllegalStateException if it is called in an invalid state |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1709 | */ |
| 1710 | public native void setAudioSessionId(int sessionId) throws IllegalArgumentException, IllegalStateException; |
| 1711 | |
| 1712 | /** |
| 1713 | * Returns the audio session ID. |
| 1714 | * |
Eric Laurent | 17cb280 | 2010-08-03 03:50:02 -0700 | [diff] [blame] | 1715 | * @return the audio session ID. {@see #setAudioSessionId(int)} |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1716 | * Note that the audio session ID is 0 only if a problem occured when the MediaPlayer was contructed. |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1717 | */ |
| 1718 | public native int getAudioSessionId(); |
| 1719 | |
| 1720 | /** |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1721 | * Attaches an auxiliary effect to the player. A typical auxiliary effect is a reverberation |
| 1722 | * effect which can be applied on any sound source that directs a certain amount of its |
| 1723 | * energy to this effect. This amount is defined by setAuxEffectSendLevel(). |
Ryan Lothian | 1e88cf0 | 2014-11-26 18:04:45 +0000 | [diff] [blame] | 1724 | * See {@link #setAuxEffectSendLevel(float)}. |
Eric Laurent | 1a5149e | 2010-09-21 18:18:20 -0700 | [diff] [blame] | 1725 | * <p>After creating an auxiliary effect (e.g. |
| 1726 | * {@link android.media.audiofx.EnvironmentalReverb}), retrieve its ID with |
| 1727 | * {@link android.media.audiofx.AudioEffect#getId()} and use it when calling this method |
| 1728 | * to attach the player to the effect. |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1729 | * <p>To detach the effect from the player, call this method with a null effect id. |
| 1730 | * <p>This method must be called after one of the overloaded <code> setDataSource </code> |
| 1731 | * methods. |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1732 | * @param effectId system wide unique id of the effect to attach |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1733 | */ |
| 1734 | public native void attachAuxEffect(int effectId); |
| 1735 | |
Gloria Wang | d01ec6e | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1736 | |
| 1737 | /** |
Ryan Lothian | 1e88cf0 | 2014-11-26 18:04:45 +0000 | [diff] [blame] | 1738 | * Sets the send level of the player to the attached auxiliary effect. |
| 1739 | * See {@link #attachAuxEffect(int)}. The level value range is 0 to 1.0. |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1740 | * <p>By default the send level is 0, so even if an effect is attached to the player |
| 1741 | * this method must be called for the effect to be applied. |
| 1742 | * <p>Note that the passed level value is a raw scalar. UI controls should be scaled |
| 1743 | * logarithmically: the gain applied by audio framework ranges from -72dB to 0dB, |
| 1744 | * so an appropriate conversion from linear UI input x to level is: |
| 1745 | * x == 0 -> level = 0 |
| 1746 | * 0 < x <= R -> level = 10^(72*(x-R)/20/R) |
| 1747 | * @param level send level scalar |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1748 | */ |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1749 | public void setAuxEffectSendLevel(float level) { |
| 1750 | if (isRestricted()) { |
| 1751 | return; |
| 1752 | } |
| 1753 | _setAuxEffectSendLevel(level); |
| 1754 | } |
| 1755 | |
| 1756 | private native void _setAuxEffectSendLevel(float level); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1757 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1758 | /* |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 1759 | * @param request Parcel destinated to the media player. The |
| 1760 | * Interface token must be set to the IMediaPlayer |
| 1761 | * one to be routed correctly through the system. |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1762 | * @param reply[out] Parcel that will contain the reply. |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 1763 | * @return The status code. |
| 1764 | */ |
| 1765 | private native final int native_invoke(Parcel request, Parcel reply); |
| 1766 | |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1767 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1768 | /* |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1769 | * @param update_only If true fetch only the set of metadata that have |
| 1770 | * changed since the last invocation of getMetadata. |
| 1771 | * The set is built using the unfiltered |
| 1772 | * notifications the native player sent to the |
| 1773 | * MediaPlayerService during that period of |
| 1774 | * time. If false, all the metadatas are considered. |
| 1775 | * @param apply_filter If true, once the metadata set has been built based on |
| 1776 | * the value update_only, the current filter is applied. |
| 1777 | * @param reply[out] On return contains the serialized |
| 1778 | * metadata. Valid only if the call was successful. |
| 1779 | * @return The status code. |
| 1780 | */ |
| 1781 | private native final boolean native_getMetadata(boolean update_only, |
| 1782 | boolean apply_filter, |
| 1783 | Parcel reply); |
| 1784 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1785 | /* |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1786 | * @param request Parcel with the 2 serialized lists of allowed |
| 1787 | * metadata types followed by the one to be |
| 1788 | * dropped. Each list starts with an integer |
| 1789 | * indicating the number of metadata type elements. |
| 1790 | * @return The status code. |
| 1791 | */ |
| 1792 | private native final int native_setMetadataFilter(Parcel request); |
| 1793 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 1794 | private static native final void native_init(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | private native final void native_setup(Object mediaplayer_this); |
| 1796 | private native final void native_finalize(); |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 1797 | |
Gloria Wang | d211f41 | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1798 | /** |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1799 | * Class for MediaPlayer to return each audio/video/subtitle track's metadata. |
| 1800 | * |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 1801 | * @see android.media.MediaPlayer#getTrackInfo |
Gloria Wang | c6091dd | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 1802 | */ |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1803 | static public class TrackInfo implements Parcelable { |
| 1804 | /** |
| 1805 | * Gets the track type. |
| 1806 | * @return TrackType which indicates if the track is video, audio, timed text. |
| 1807 | */ |
| 1808 | public int getTrackType() { |
| 1809 | return mTrackType; |
Gloria Wang | c6091dd | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 1810 | } |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1811 | |
| 1812 | /** |
| 1813 | * Gets the language code of the track. |
| 1814 | * @return a language code in either way of ISO-639-1 or ISO-639-2. |
| 1815 | * When the language is unknown or could not be determined, |
| 1816 | * ISO-639-2 language code, "und", is returned. |
| 1817 | */ |
| 1818 | public String getLanguage() { |
Lajos Molnar | 1c56a67 | 2013-08-15 12:05:52 -0700 | [diff] [blame] | 1819 | String language = mFormat.getString(MediaFormat.KEY_LANGUAGE); |
| 1820 | return language == null ? "und" : language; |
| 1821 | } |
| 1822 | |
| 1823 | /** |
| 1824 | * Gets the {@link MediaFormat} of the track. If the format is |
| 1825 | * unknown or could not be determined, null is returned. |
| 1826 | */ |
| 1827 | public MediaFormat getFormat() { |
Chong Zhang | f8ca13b | 2013-09-11 13:41:00 -0700 | [diff] [blame] | 1828 | if (mTrackType == MEDIA_TRACK_TYPE_TIMEDTEXT |
| 1829 | || mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) { |
Lajos Molnar | 1c56a67 | 2013-08-15 12:05:52 -0700 | [diff] [blame] | 1830 | return mFormat; |
| 1831 | } |
| 1832 | return null; |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | public static final int MEDIA_TRACK_TYPE_UNKNOWN = 0; |
| 1836 | public static final int MEDIA_TRACK_TYPE_VIDEO = 1; |
| 1837 | public static final int MEDIA_TRACK_TYPE_AUDIO = 2; |
| 1838 | public static final int MEDIA_TRACK_TYPE_TIMEDTEXT = 3; |
Chong Zhang | 83ddaf6 | 2013-08-06 09:43:22 -0700 | [diff] [blame] | 1839 | public static final int MEDIA_TRACK_TYPE_SUBTITLE = 4; |
Robert Shih | ac033f0 | 2015-04-10 11:15:02 -0700 | [diff] [blame] | 1840 | public static final int MEDIA_TRACK_TYPE_METADATA = 5; |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1841 | |
| 1842 | final int mTrackType; |
Lajos Molnar | 1c56a67 | 2013-08-15 12:05:52 -0700 | [diff] [blame] | 1843 | final MediaFormat mFormat; |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1844 | |
| 1845 | TrackInfo(Parcel in) { |
| 1846 | mTrackType = in.readInt(); |
Lajos Molnar | 1c56a67 | 2013-08-15 12:05:52 -0700 | [diff] [blame] | 1847 | // TODO: parcel in the full MediaFormat |
| 1848 | String language = in.readString(); |
| 1849 | |
| 1850 | if (mTrackType == MEDIA_TRACK_TYPE_TIMEDTEXT) { |
| 1851 | mFormat = MediaFormat.createSubtitleFormat( |
| 1852 | MEDIA_MIMETYPE_TEXT_SUBRIP, language); |
Chong Zhang | f8ca13b | 2013-09-11 13:41:00 -0700 | [diff] [blame] | 1853 | } else if (mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) { |
Chong Zhang | 079fa96 | 2014-06-11 15:09:13 -0700 | [diff] [blame] | 1854 | String mime = in.readString(); |
| 1855 | mFormat = MediaFormat.createSubtitleFormat(mime, language); |
Lajos Molnar | d486f96 | 2013-09-11 16:26:18 -0700 | [diff] [blame] | 1856 | mFormat.setInteger(MediaFormat.KEY_IS_AUTOSELECT, in.readInt()); |
| 1857 | mFormat.setInteger(MediaFormat.KEY_IS_DEFAULT, in.readInt()); |
| 1858 | mFormat.setInteger(MediaFormat.KEY_IS_FORCED_SUBTITLE, in.readInt()); |
Lajos Molnar | 1c56a67 | 2013-08-15 12:05:52 -0700 | [diff] [blame] | 1859 | } else { |
| 1860 | mFormat = new MediaFormat(); |
| 1861 | mFormat.setString(MediaFormat.KEY_LANGUAGE, language); |
| 1862 | } |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1863 | } |
| 1864 | |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 1865 | /** @hide */ |
| 1866 | TrackInfo(int type, MediaFormat format) { |
| 1867 | mTrackType = type; |
| 1868 | mFormat = format; |
| 1869 | } |
| 1870 | |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 1871 | /** |
| 1872 | * {@inheritDoc} |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1873 | */ |
| 1874 | @Override |
| 1875 | public int describeContents() { |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 1879 | /** |
| 1880 | * {@inheritDoc} |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1881 | */ |
| 1882 | @Override |
| 1883 | public void writeToParcel(Parcel dest, int flags) { |
| 1884 | dest.writeInt(mTrackType); |
Lajos Molnar | 1c56a67 | 2013-08-15 12:05:52 -0700 | [diff] [blame] | 1885 | dest.writeString(getLanguage()); |
Chong Zhang | f8ca13b | 2013-09-11 13:41:00 -0700 | [diff] [blame] | 1886 | |
| 1887 | if (mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) { |
Chong Zhang | 079fa96 | 2014-06-11 15:09:13 -0700 | [diff] [blame] | 1888 | dest.writeString(mFormat.getString(MediaFormat.KEY_MIME)); |
Lajos Molnar | d486f96 | 2013-09-11 16:26:18 -0700 | [diff] [blame] | 1889 | dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_AUTOSELECT)); |
| 1890 | dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_DEFAULT)); |
| 1891 | dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_FORCED_SUBTITLE)); |
Chong Zhang | f8ca13b | 2013-09-11 13:41:00 -0700 | [diff] [blame] | 1892 | } |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1893 | } |
| 1894 | |
Chong Zhang | 079fa96 | 2014-06-11 15:09:13 -0700 | [diff] [blame] | 1895 | @Override |
| 1896 | public String toString() { |
| 1897 | StringBuilder out = new StringBuilder(128); |
| 1898 | out.append(getClass().getName()); |
| 1899 | out.append('{'); |
| 1900 | switch (mTrackType) { |
| 1901 | case MEDIA_TRACK_TYPE_VIDEO: |
| 1902 | out.append("VIDEO"); |
| 1903 | break; |
| 1904 | case MEDIA_TRACK_TYPE_AUDIO: |
| 1905 | out.append("AUDIO"); |
| 1906 | break; |
| 1907 | case MEDIA_TRACK_TYPE_TIMEDTEXT: |
| 1908 | out.append("TIMEDTEXT"); |
| 1909 | break; |
| 1910 | case MEDIA_TRACK_TYPE_SUBTITLE: |
| 1911 | out.append("SUBTITLE"); |
| 1912 | break; |
| 1913 | default: |
| 1914 | out.append("UNKNOWN"); |
| 1915 | break; |
| 1916 | } |
| 1917 | out.append(", " + mFormat.toString()); |
| 1918 | out.append("}"); |
| 1919 | return out.toString(); |
| 1920 | } |
| 1921 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1922 | /** |
| 1923 | * Used to read a TrackInfo from a Parcel. |
| 1924 | */ |
| 1925 | static final Parcelable.Creator<TrackInfo> CREATOR |
| 1926 | = new Parcelable.Creator<TrackInfo>() { |
| 1927 | @Override |
| 1928 | public TrackInfo createFromParcel(Parcel in) { |
| 1929 | return new TrackInfo(in); |
| 1930 | } |
| 1931 | |
| 1932 | @Override |
| 1933 | public TrackInfo[] newArray(int size) { |
| 1934 | return new TrackInfo[size]; |
| 1935 | } |
| 1936 | }; |
| 1937 | |
| 1938 | }; |
| 1939 | |
| 1940 | /** |
| 1941 | * Returns an array of track information. |
| 1942 | * |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 1943 | * @return Array of track info. The total number of tracks is the array length. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 1944 | * Must be called again if an external timed text source has been added after any of the |
| 1945 | * addTimedTextSource methods are called. |
| 1946 | * @throws IllegalStateException if it is called in an invalid state. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1947 | */ |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 1948 | public TrackInfo[] getTrackInfo() throws IllegalStateException { |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 1949 | TrackInfo trackInfo[] = getInbandTrackInfo(); |
| 1950 | // add out-of-band tracks |
| 1951 | TrackInfo allTrackInfo[] = new TrackInfo[trackInfo.length + mOutOfBandSubtitleTracks.size()]; |
| 1952 | System.arraycopy(trackInfo, 0, allTrackInfo, 0, trackInfo.length); |
| 1953 | int i = trackInfo.length; |
| 1954 | for (SubtitleTrack track: mOutOfBandSubtitleTracks) { |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 1955 | int type = track.isTimedText() |
| 1956 | ? TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT |
| 1957 | : TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE; |
| 1958 | allTrackInfo[i] = new TrackInfo(type, track.getFormat()); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 1959 | ++i; |
| 1960 | } |
| 1961 | return allTrackInfo; |
| 1962 | } |
| 1963 | |
| 1964 | private TrackInfo[] getInbandTrackInfo() throws IllegalStateException { |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1965 | Parcel request = Parcel.obtain(); |
| 1966 | Parcel reply = Parcel.obtain(); |
Insun Kang | be0ea96 | 2012-05-04 20:52:57 +0900 | [diff] [blame] | 1967 | try { |
| 1968 | request.writeInterfaceToken(IMEDIA_PLAYER); |
| 1969 | request.writeInt(INVOKE_ID_GET_TRACK_INFO); |
| 1970 | invoke(request, reply); |
| 1971 | TrackInfo trackInfo[] = reply.createTypedArray(TrackInfo.CREATOR); |
| 1972 | return trackInfo; |
| 1973 | } finally { |
| 1974 | request.recycle(); |
| 1975 | reply.recycle(); |
| 1976 | } |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1977 | } |
| 1978 | |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 1979 | /* Do not change these values without updating their counterparts |
| 1980 | * in include/media/stagefright/MediaDefs.h and media/libstagefright/MediaDefs.cpp! |
| 1981 | */ |
| 1982 | /** |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 1983 | * MIME type for SubRip (SRT) container. Used in addTimedTextSource APIs. |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 1984 | */ |
| 1985 | public static final String MEDIA_MIMETYPE_TEXT_SUBRIP = "application/x-subrip"; |
| 1986 | |
Chong Zhang | f8ca13b | 2013-09-11 13:41:00 -0700 | [diff] [blame] | 1987 | /** |
| 1988 | * MIME type for WebVTT subtitle data. |
| 1989 | * @hide |
| 1990 | */ |
| 1991 | public static final String MEDIA_MIMETYPE_TEXT_VTT = "text/vtt"; |
| 1992 | |
Chong Zhang | bdfd910 | 2014-06-11 15:10:23 -0700 | [diff] [blame] | 1993 | /** |
| 1994 | * MIME type for CEA-608 closed caption data. |
| 1995 | * @hide |
| 1996 | */ |
| 1997 | public static final String MEDIA_MIMETYPE_TEXT_CEA_608 = "text/cea-608"; |
| 1998 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 1999 | /* |
| 2000 | * A helper function to check if the mime type is supported by media framework. |
| 2001 | */ |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2002 | private static boolean availableMimeTypeForExternalSource(String mimeType) { |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 2003 | if (MEDIA_MIMETYPE_TEXT_SUBRIP.equals(mimeType)) { |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2004 | return true; |
| 2005 | } |
| 2006 | return false; |
| 2007 | } |
| 2008 | |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2009 | private SubtitleController mSubtitleController; |
| 2010 | |
| 2011 | /** @hide */ |
| 2012 | public void setSubtitleAnchor( |
| 2013 | SubtitleController controller, |
| 2014 | SubtitleController.Anchor anchor) { |
| 2015 | // TODO: create SubtitleController in MediaPlayer |
| 2016 | mSubtitleController = controller; |
| 2017 | mSubtitleController.setAnchor(anchor); |
| 2018 | } |
| 2019 | |
Robert Shih | 8c4d53c | 2014-09-05 13:33:31 -0700 | [diff] [blame] | 2020 | private final Object mInbandSubtitleLock = new Object(); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2021 | private SubtitleTrack[] mInbandSubtitleTracks; |
| 2022 | private int mSelectedSubtitleTrackIndex = -1; |
| 2023 | private Vector<SubtitleTrack> mOutOfBandSubtitleTracks; |
| 2024 | private Vector<InputStream> mOpenSubtitleSources; |
| 2025 | |
| 2026 | private OnSubtitleDataListener mSubtitleDataListener = new OnSubtitleDataListener() { |
| 2027 | @Override |
| 2028 | public void onSubtitleData(MediaPlayer mp, SubtitleData data) { |
| 2029 | int index = data.getTrackIndex(); |
| 2030 | if (index >= mInbandSubtitleTracks.length) { |
| 2031 | return; |
| 2032 | } |
| 2033 | SubtitleTrack track = mInbandSubtitleTracks[index]; |
| 2034 | if (track != null) { |
Robert Shih | 38c209b | 2014-06-10 17:28:37 -0700 | [diff] [blame] | 2035 | track.onData(data); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | }; |
| 2039 | |
| 2040 | /** @hide */ |
| 2041 | @Override |
| 2042 | public void onSubtitleTrackSelected(SubtitleTrack track) { |
| 2043 | if (mSelectedSubtitleTrackIndex >= 0) { |
Lajos Molnar | d486f96 | 2013-09-11 16:26:18 -0700 | [diff] [blame] | 2044 | try { |
| 2045 | selectOrDeselectInbandTrack(mSelectedSubtitleTrackIndex, false); |
| 2046 | } catch (IllegalStateException e) { |
| 2047 | } |
| 2048 | mSelectedSubtitleTrackIndex = -1; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2049 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2050 | setOnSubtitleDataListener(null); |
Lajos Molnar | 29f5183 | 2013-09-20 08:45:31 -0700 | [diff] [blame] | 2051 | if (track == null) { |
| 2052 | return; |
| 2053 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2054 | for (int i = 0; i < mInbandSubtitleTracks.length; i++) { |
| 2055 | if (mInbandSubtitleTracks[i] == track) { |
| 2056 | Log.v(TAG, "Selecting subtitle track " + i); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2057 | mSelectedSubtitleTrackIndex = i; |
Lajos Molnar | d486f96 | 2013-09-11 16:26:18 -0700 | [diff] [blame] | 2058 | try { |
| 2059 | selectOrDeselectInbandTrack(mSelectedSubtitleTrackIndex, true); |
| 2060 | } catch (IllegalStateException e) { |
| 2061 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2062 | setOnSubtitleDataListener(mSubtitleDataListener); |
| 2063 | break; |
| 2064 | } |
| 2065 | } |
| 2066 | // no need to select out-of-band tracks |
| 2067 | } |
| 2068 | |
| 2069 | /** @hide */ |
| 2070 | public void addSubtitleSource(InputStream is, MediaFormat format) |
| 2071 | throws IllegalStateException |
| 2072 | { |
| 2073 | final InputStream fIs = is; |
| 2074 | final MediaFormat fFormat = format; |
| 2075 | |
| 2076 | // Ensure all input streams are closed. It is also a handy |
| 2077 | // way to implement timeouts in the future. |
| 2078 | synchronized(mOpenSubtitleSources) { |
| 2079 | mOpenSubtitleSources.add(is); |
| 2080 | } |
| 2081 | |
| 2082 | // process each subtitle in its own thread |
| 2083 | final HandlerThread thread = new HandlerThread("SubtitleReadThread", |
| 2084 | Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE); |
| 2085 | thread.start(); |
| 2086 | Handler handler = new Handler(thread.getLooper()); |
| 2087 | handler.post(new Runnable() { |
| 2088 | private int addTrack() { |
| 2089 | if (fIs == null || mSubtitleController == null) { |
| 2090 | return MEDIA_INFO_UNSUPPORTED_SUBTITLE; |
| 2091 | } |
| 2092 | |
| 2093 | SubtitleTrack track = mSubtitleController.addTrack(fFormat); |
| 2094 | if (track == null) { |
| 2095 | return MEDIA_INFO_UNSUPPORTED_SUBTITLE; |
| 2096 | } |
| 2097 | |
| 2098 | // TODO: do the conversion in the subtitle track |
| 2099 | Scanner scanner = new Scanner(fIs, "UTF-8"); |
| 2100 | String contents = scanner.useDelimiter("\\A").next(); |
| 2101 | synchronized(mOpenSubtitleSources) { |
| 2102 | mOpenSubtitleSources.remove(fIs); |
| 2103 | } |
| 2104 | scanner.close(); |
| 2105 | mOutOfBandSubtitleTracks.add(track); |
Chong Zhang | 079fa96 | 2014-06-11 15:09:13 -0700 | [diff] [blame] | 2106 | track.onData(contents.getBytes(), true /* eos */, ~0 /* runID: keep forever */); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2107 | return MEDIA_INFO_EXTERNAL_METADATA_UPDATE; |
| 2108 | } |
| 2109 | |
| 2110 | public void run() { |
| 2111 | int res = addTrack(); |
| 2112 | if (mEventHandler != null) { |
| 2113 | Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null); |
| 2114 | mEventHandler.sendMessage(m); |
| 2115 | } |
| 2116 | thread.getLooper().quitSafely(); |
| 2117 | } |
| 2118 | }); |
| 2119 | } |
| 2120 | |
| 2121 | private void scanInternalSubtitleTracks() { |
| 2122 | if (mSubtitleController == null) { |
| 2123 | Log.e(TAG, "Should have subtitle controller already set"); |
| 2124 | return; |
| 2125 | } |
| 2126 | |
| 2127 | TrackInfo[] tracks = getInbandTrackInfo(); |
Robert Shih | 8c4d53c | 2014-09-05 13:33:31 -0700 | [diff] [blame] | 2128 | synchronized (mInbandSubtitleLock) { |
| 2129 | SubtitleTrack[] inbandTracks = new SubtitleTrack[tracks.length]; |
| 2130 | for (int i=0; i < tracks.length; i++) { |
| 2131 | if (tracks[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) { |
| 2132 | if (i < mInbandSubtitleTracks.length) { |
| 2133 | inbandTracks[i] = mInbandSubtitleTracks[i]; |
| 2134 | } else { |
| 2135 | SubtitleTrack track = mSubtitleController.addTrack( |
| 2136 | tracks[i].getFormat()); |
| 2137 | inbandTracks[i] = track; |
| 2138 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2139 | } |
| 2140 | } |
Robert Shih | 8c4d53c | 2014-09-05 13:33:31 -0700 | [diff] [blame] | 2141 | mInbandSubtitleTracks = inbandTracks; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2142 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2143 | mSubtitleController.selectDefaultTrack(); |
| 2144 | } |
| 2145 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2146 | /* TODO: Limit the total number of external timed text source to a reasonable number. |
| 2147 | */ |
| 2148 | /** |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2149 | * Adds an external timed text source file. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2150 | * |
| 2151 | * Currently supported format is SubRip with the file extension .srt, case insensitive. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2152 | * Note that a single external timed text source may contain multiple tracks in it. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2153 | * One can find the total number of available tracks using {@link #getTrackInfo()} to see what |
| 2154 | * additional tracks become available after this method call. |
| 2155 | * |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2156 | * @param path The file path of external timed text source file. |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2157 | * @param mimeType The mime type of the file. Must be one of the mime types listed above. |
| 2158 | * @throws IOException if the file cannot be accessed or is corrupted. |
| 2159 | * @throws IllegalArgumentException if the mimeType is not supported. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2160 | * @throws IllegalStateException if called in an invalid state. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2161 | */ |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2162 | public void addTimedTextSource(String path, String mimeType) |
| 2163 | throws IOException, IllegalArgumentException, IllegalStateException { |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2164 | if (!availableMimeTypeForExternalSource(mimeType)) { |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2165 | final String msg = "Illegal mimeType for timed text source: " + mimeType; |
James Dong | c4c0284 | 2012-04-12 19:49:02 -0700 | [diff] [blame] | 2166 | throw new IllegalArgumentException(msg); |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2167 | } |
| 2168 | |
James Dong | c4c0284 | 2012-04-12 19:49:02 -0700 | [diff] [blame] | 2169 | File file = new File(path); |
| 2170 | if (file.exists()) { |
| 2171 | FileInputStream is = new FileInputStream(file); |
| 2172 | FileDescriptor fd = is.getFD(); |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2173 | addTimedTextSource(fd, mimeType); |
James Dong | c4c0284 | 2012-04-12 19:49:02 -0700 | [diff] [blame] | 2174 | is.close(); |
| 2175 | } else { |
| 2176 | // We do not support the case where the path is not a file. |
| 2177 | throw new IOException(path); |
| 2178 | } |
Gloria Wang | c6091dd | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /** |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2182 | * Adds an external timed text source file (Uri). |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2183 | * |
| 2184 | * Currently supported format is SubRip with the file extension .srt, case insensitive. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2185 | * Note that a single external timed text source may contain multiple tracks in it. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2186 | * One can find the total number of available tracks using {@link #getTrackInfo()} to see what |
| 2187 | * additional tracks become available after this method call. |
| 2188 | * |
| 2189 | * @param context the Context to use when resolving the Uri |
| 2190 | * @param uri the Content URI of the data you want to play |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2191 | * @param mimeType The mime type of the file. Must be one of the mime types listed above. |
| 2192 | * @throws IOException if the file cannot be accessed or is corrupted. |
| 2193 | * @throws IllegalArgumentException if the mimeType is not supported. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2194 | * @throws IllegalStateException if called in an invalid state. |
Gloria Wang | c6091dd | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 2195 | */ |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2196 | public void addTimedTextSource(Context context, Uri uri, String mimeType) |
| 2197 | throws IOException, IllegalArgumentException, IllegalStateException { |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2198 | String scheme = uri.getScheme(); |
| 2199 | if(scheme == null || scheme.equals("file")) { |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2200 | addTimedTextSource(uri.getPath(), mimeType); |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2201 | return; |
| 2202 | } |
| 2203 | |
| 2204 | AssetFileDescriptor fd = null; |
| 2205 | try { |
| 2206 | ContentResolver resolver = context.getContentResolver(); |
| 2207 | fd = resolver.openAssetFileDescriptor(uri, "r"); |
| 2208 | if (fd == null) { |
| 2209 | return; |
| 2210 | } |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2211 | addTimedTextSource(fd.getFileDescriptor(), mimeType); |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2212 | return; |
| 2213 | } catch (SecurityException ex) { |
| 2214 | } catch (IOException ex) { |
| 2215 | } finally { |
| 2216 | if (fd != null) { |
| 2217 | fd.close(); |
| 2218 | } |
| 2219 | } |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2220 | } |
| 2221 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2222 | /** |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2223 | * Adds an external timed text source file (FileDescriptor). |
| 2224 | * |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2225 | * It is the caller's responsibility to close the file descriptor. |
| 2226 | * It is safe to do so as soon as this call returns. |
| 2227 | * |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2228 | * Currently supported format is SubRip. Note that a single external timed text source may |
| 2229 | * contain multiple tracks in it. One can find the total number of available tracks |
| 2230 | * using {@link #getTrackInfo()} to see what additional tracks become available |
| 2231 | * after this method call. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2232 | * |
| 2233 | * @param fd the FileDescriptor for the file you want to play |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2234 | * @param mimeType The mime type of the file. Must be one of the mime types listed above. |
| 2235 | * @throws IllegalArgumentException if the mimeType is not supported. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2236 | * @throws IllegalStateException if called in an invalid state. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2237 | */ |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2238 | public void addTimedTextSource(FileDescriptor fd, String mimeType) |
| 2239 | throws IllegalArgumentException, IllegalStateException { |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2240 | // intentionally less than LONG_MAX |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2241 | addTimedTextSource(fd, 0, 0x7ffffffffffffffL, mimeType); |
Gloria Wang | c6091dd | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | /** |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2245 | * Adds an external timed text file (FileDescriptor). |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2246 | * |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2247 | * It is the caller's responsibility to close the file descriptor. |
| 2248 | * It is safe to do so as soon as this call returns. |
| 2249 | * |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2250 | * Currently supported format is SubRip. Note that a single external timed text source may |
| 2251 | * contain multiple tracks in it. One can find the total number of available tracks |
| 2252 | * using {@link #getTrackInfo()} to see what additional tracks become available |
| 2253 | * after this method call. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2254 | * |
| 2255 | * @param fd the FileDescriptor for the file you want to play |
| 2256 | * @param offset the offset into the file where the data to be played starts, in bytes |
| 2257 | * @param length the length in bytes of the data to be played |
Ying Wang | 546f3bf | 2014-07-29 16:23:15 -0700 | [diff] [blame] | 2258 | * @param mime The mime type of the file. Must be one of the mime types listed above. |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2259 | * @throws IllegalArgumentException if the mimeType is not supported. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2260 | * @throws IllegalStateException if called in an invalid state. |
Gloria Wang | c6091dd | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 2261 | */ |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 2262 | public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime) |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2263 | throws IllegalArgumentException, IllegalStateException { |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 2264 | if (!availableMimeTypeForExternalSource(mime)) { |
| 2265 | throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mime); |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2266 | } |
James Dong | c4c0284 | 2012-04-12 19:49:02 -0700 | [diff] [blame] | 2267 | |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 2268 | FileDescriptor fd2; |
Insun Kang | be0ea96 | 2012-05-04 20:52:57 +0900 | [diff] [blame] | 2269 | try { |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 2270 | fd2 = Libcore.os.dup(fd); |
| 2271 | } catch (ErrnoException ex) { |
| 2272 | Log.e(TAG, ex.getMessage(), ex); |
| 2273 | throw new RuntimeException(ex); |
Insun Kang | be0ea96 | 2012-05-04 20:52:57 +0900 | [diff] [blame] | 2274 | } |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 2275 | |
| 2276 | final MediaFormat fFormat = new MediaFormat(); |
| 2277 | fFormat.setString(MediaFormat.KEY_MIME, mime); |
| 2278 | fFormat.setInteger(MediaFormat.KEY_IS_TIMED_TEXT, 1); |
| 2279 | |
| 2280 | Context context = ActivityThread.currentApplication(); |
| 2281 | // A MediaPlayer created by a VideoView should already have its mSubtitleController set. |
| 2282 | if (mSubtitleController == null) { |
| 2283 | mSubtitleController = new SubtitleController(context, mTimeProvider, this); |
| 2284 | mSubtitleController.setAnchor(new Anchor() { |
| 2285 | @Override |
| 2286 | public void setSubtitleWidget(RenderingWidget subtitleWidget) { |
| 2287 | } |
| 2288 | |
| 2289 | @Override |
| 2290 | public Looper getSubtitleLooper() { |
| 2291 | return Looper.getMainLooper(); |
| 2292 | } |
| 2293 | }); |
| 2294 | } |
| 2295 | |
| 2296 | if (!mSubtitleController.hasRendererFor(fFormat)) { |
| 2297 | // test and add not atomic |
| 2298 | mSubtitleController.registerRenderer(new SRTRenderer(context, mEventHandler)); |
| 2299 | } |
| 2300 | final SubtitleTrack track = mSubtitleController.addTrack(fFormat); |
| 2301 | mOutOfBandSubtitleTracks.add(track); |
| 2302 | |
| 2303 | final FileDescriptor fd3 = fd2; |
| 2304 | final long offset2 = offset; |
| 2305 | final long length2 = length; |
| 2306 | final HandlerThread thread = new HandlerThread( |
| 2307 | "TimedTextReadThread", |
| 2308 | Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE); |
| 2309 | thread.start(); |
| 2310 | Handler handler = new Handler(thread.getLooper()); |
| 2311 | handler.post(new Runnable() { |
| 2312 | private int addTrack() { |
| 2313 | InputStream is = null; |
| 2314 | final ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 2315 | try { |
| 2316 | Libcore.os.lseek(fd3, offset2, OsConstants.SEEK_SET); |
| 2317 | byte[] buffer = new byte[4096]; |
Robert Shih | 8c4d53c | 2014-09-05 13:33:31 -0700 | [diff] [blame] | 2318 | for (long total = 0; total < length2;) { |
| 2319 | int bytesToRead = (int) Math.min(buffer.length, length2 - total); |
| 2320 | int bytes = IoBridge.read(fd3, buffer, 0, bytesToRead); |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 2321 | if (bytes < 0) { |
| 2322 | break; |
| 2323 | } else { |
| 2324 | bos.write(buffer, 0, bytes); |
| 2325 | total += bytes; |
| 2326 | } |
| 2327 | } |
| 2328 | track.onData(bos.toByteArray(), true /* eos */, ~0 /* runID: keep forever */); |
| 2329 | return MEDIA_INFO_EXTERNAL_METADATA_UPDATE; |
| 2330 | } catch (Exception e) { |
| 2331 | Log.e(TAG, e.getMessage(), e); |
| 2332 | return MEDIA_INFO_TIMED_TEXT_ERROR; |
| 2333 | } finally { |
| 2334 | if (is != null) { |
| 2335 | try { |
| 2336 | is.close(); |
| 2337 | } catch (IOException e) { |
| 2338 | Log.e(TAG, e.getMessage(), e); |
| 2339 | } |
| 2340 | } |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | public void run() { |
| 2345 | int res = addTrack(); |
| 2346 | if (mEventHandler != null) { |
| 2347 | Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null); |
| 2348 | mEventHandler.sendMessage(m); |
| 2349 | } |
| 2350 | thread.getLooper().quitSafely(); |
| 2351 | } |
| 2352 | }); |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | /** |
Robert Shih | 464da70 | 2014-05-29 10:54:32 -0700 | [diff] [blame] | 2356 | * Returns the index of the audio, video, or subtitle track currently selected for playback, |
| 2357 | * The return value is an index into the array returned by {@link #getTrackInfo()}, and can |
| 2358 | * be used in calls to {@link #selectTrack(int)} or {@link #deselectTrack(int)}. |
| 2359 | * |
| 2360 | * @param trackType should be one of {@link TrackInfo#MEDIA_TRACK_TYPE_VIDEO}, |
| 2361 | * {@link TrackInfo#MEDIA_TRACK_TYPE_AUDIO}, or |
| 2362 | * {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE} |
| 2363 | * @return index of the audio, video, or subtitle track currently selected for playback; |
| 2364 | * a negative integer is returned when there is no selected track for {@code trackType} or |
| 2365 | * when {@code trackType} is not one of audio, video, or subtitle. |
| 2366 | * @throws IllegalStateException if called after {@link #release()} |
| 2367 | * |
Ryan Lothian | 1e88cf0 | 2014-11-26 18:04:45 +0000 | [diff] [blame] | 2368 | * @see #getTrackInfo() |
| 2369 | * @see #selectTrack(int) |
| 2370 | * @see #deselectTrack(int) |
Robert Shih | 464da70 | 2014-05-29 10:54:32 -0700 | [diff] [blame] | 2371 | */ |
| 2372 | public int getSelectedTrack(int trackType) throws IllegalStateException { |
| 2373 | if (trackType == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE && mSubtitleController != null) { |
| 2374 | SubtitleTrack subtitleTrack = mSubtitleController.getSelectedTrack(); |
| 2375 | if (subtitleTrack != null) { |
| 2376 | int index = mOutOfBandSubtitleTracks.indexOf(subtitleTrack); |
| 2377 | if (index >= 0) { |
| 2378 | return mInbandSubtitleTracks.length + index; |
| 2379 | } |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | Parcel request = Parcel.obtain(); |
| 2384 | Parcel reply = Parcel.obtain(); |
| 2385 | try { |
| 2386 | request.writeInterfaceToken(IMEDIA_PLAYER); |
| 2387 | request.writeInt(INVOKE_ID_GET_SELECTED_TRACK); |
| 2388 | request.writeInt(trackType); |
| 2389 | invoke(request, reply); |
| 2390 | int selectedTrack = reply.readInt(); |
| 2391 | return selectedTrack; |
| 2392 | } finally { |
| 2393 | request.recycle(); |
| 2394 | reply.recycle(); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | /** |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2399 | * Selects a track. |
| 2400 | * <p> |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2401 | * If a MediaPlayer is in invalid state, it throws an IllegalStateException exception. |
| 2402 | * If a MediaPlayer is in <em>Started</em> state, the selected track is presented immediately. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2403 | * If a MediaPlayer is not in Started state, it just marks the track to be played. |
| 2404 | * </p> |
| 2405 | * <p> |
| 2406 | * In any valid state, if it is called multiple times on the same type of track (ie. Video, |
| 2407 | * Audio, Timed Text), the most recent one will be chosen. |
| 2408 | * </p> |
| 2409 | * <p> |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2410 | * The first audio and video tracks are selected by default if available, even though |
| 2411 | * this method is not called. However, no timed text track will be selected until |
| 2412 | * this function is called. |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2413 | * </p> |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2414 | * <p> |
| 2415 | * Currently, only timed text tracks or audio tracks can be selected via this method. |
| 2416 | * In addition, the support for selecting an audio track at runtime is pretty limited |
| 2417 | * in that an audio track can only be selected in the <em>Prepared</em> state. |
| 2418 | * </p> |
| 2419 | * @param index the index of the track to be selected. The valid range of the index |
| 2420 | * is 0..total number of track - 1. The total number of tracks as well as the type of |
| 2421 | * each individual track can be found by calling {@link #getTrackInfo()} method. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2422 | * @throws IllegalStateException if called in an invalid state. |
| 2423 | * |
| 2424 | * @see android.media.MediaPlayer#getTrackInfo |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2425 | */ |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2426 | public void selectTrack(int index) throws IllegalStateException { |
| 2427 | selectOrDeselectTrack(index, true /* select */); |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | /** |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2431 | * Deselect a track. |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2432 | * <p> |
| 2433 | * Currently, the track must be a timed text track and no audio or video tracks can be |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2434 | * deselected. If the timed text track identified by index has not been |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2435 | * selected before, it throws an exception. |
| 2436 | * </p> |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2437 | * @param index the index of the track to be deselected. The valid range of the index |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2438 | * is 0..total number of tracks - 1. The total number of tracks as well as the type of |
| 2439 | * each individual track can be found by calling {@link #getTrackInfo()} method. |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2440 | * @throws IllegalStateException if called in an invalid state. |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2441 | * |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2442 | * @see android.media.MediaPlayer#getTrackInfo |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2443 | */ |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2444 | public void deselectTrack(int index) throws IllegalStateException { |
| 2445 | selectOrDeselectTrack(index, false /* select */); |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
James Dong | 7a9734d | 2012-04-19 13:30:20 -0700 | [diff] [blame] | 2448 | private void selectOrDeselectTrack(int index, boolean select) |
| 2449 | throws IllegalStateException { |
Lajos Molnar | d486f96 | 2013-09-11 16:26:18 -0700 | [diff] [blame] | 2450 | // handle subtitle track through subtitle controller |
| 2451 | SubtitleTrack track = null; |
Robert Shih | 8c4d53c | 2014-09-05 13:33:31 -0700 | [diff] [blame] | 2452 | synchronized (mInbandSubtitleLock) { |
| 2453 | if (mInbandSubtitleTracks.length == 0) { |
| 2454 | TrackInfo[] tracks = getInbandTrackInfo(); |
| 2455 | mInbandSubtitleTracks = new SubtitleTrack[tracks.length]; |
| 2456 | for (int i=0; i < tracks.length; i++) { |
| 2457 | if (tracks[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) { |
| 2458 | mInbandSubtitleTracks[i] = mSubtitleController.addTrack(tracks[i].getFormat()); |
| 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | |
Lajos Molnar | d486f96 | 2013-09-11 16:26:18 -0700 | [diff] [blame] | 2464 | if (index < mInbandSubtitleTracks.length) { |
| 2465 | track = mInbandSubtitleTracks[index]; |
| 2466 | } else if (index < mInbandSubtitleTracks.length + mOutOfBandSubtitleTracks.size()) { |
| 2467 | track = mOutOfBandSubtitleTracks.get(index - mInbandSubtitleTracks.length); |
| 2468 | } |
| 2469 | |
| 2470 | if (mSubtitleController != null && track != null) { |
| 2471 | if (select) { |
Robert Shih | 3cdf7c5 | 2014-07-23 17:18:46 -0700 | [diff] [blame] | 2472 | if (track.isTimedText()) { |
| 2473 | int ttIndex = getSelectedTrack(TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT); |
| 2474 | if (ttIndex >= 0 && ttIndex < mInbandSubtitleTracks.length) { |
| 2475 | // deselect inband counterpart |
| 2476 | selectOrDeselectInbandTrack(ttIndex, false); |
| 2477 | } |
| 2478 | } |
Lajos Molnar | d486f96 | 2013-09-11 16:26:18 -0700 | [diff] [blame] | 2479 | mSubtitleController.selectTrack(track); |
| 2480 | } else if (mSubtitleController.getSelectedTrack() == track) { |
| 2481 | mSubtitleController.selectTrack(null); |
| 2482 | } else { |
| 2483 | Log.w(TAG, "trying to deselect track that was not selected"); |
| 2484 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2485 | return; |
| 2486 | } |
| 2487 | |
Lajos Molnar | d486f96 | 2013-09-11 16:26:18 -0700 | [diff] [blame] | 2488 | selectOrDeselectInbandTrack(index, select); |
| 2489 | } |
| 2490 | |
| 2491 | private void selectOrDeselectInbandTrack(int index, boolean select) |
| 2492 | throws IllegalStateException { |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2493 | Parcel request = Parcel.obtain(); |
| 2494 | Parcel reply = Parcel.obtain(); |
Insun Kang | be0ea96 | 2012-05-04 20:52:57 +0900 | [diff] [blame] | 2495 | try { |
| 2496 | request.writeInterfaceToken(IMEDIA_PLAYER); |
| 2497 | request.writeInt(select? INVOKE_ID_SELECT_TRACK: INVOKE_ID_DESELECT_TRACK); |
| 2498 | request.writeInt(index); |
| 2499 | invoke(request, reply); |
| 2500 | } finally { |
| 2501 | request.recycle(); |
| 2502 | reply.recycle(); |
| 2503 | } |
Gloria Wang | c6091dd | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 2504 | } |
| 2505 | |
James Dong | 831f0a9 | 2012-04-18 18:12:12 -0700 | [diff] [blame] | 2506 | |
Gloria Wang | c6091dd | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 2507 | /** |
Gloria Wang | d211f41 | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2508 | * @param reply Parcel with audio/video duration info for battery |
| 2509 | tracking usage |
| 2510 | * @return The status code. |
| 2511 | * {@hide} |
| 2512 | */ |
| 2513 | public native static int native_pullBatteryData(Parcel reply); |
| 2514 | |
John Grossman | 720aa28 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 2515 | /** |
| 2516 | * Sets the target UDP re-transmit endpoint for the low level player. |
| 2517 | * Generally, the address portion of the endpoint is an IP multicast |
| 2518 | * address, although a unicast address would be equally valid. When a valid |
| 2519 | * retransmit endpoint has been set, the media player will not decode and |
| 2520 | * render the media presentation locally. Instead, the player will attempt |
| 2521 | * to re-multiplex its media data using the Android@Home RTP profile and |
| 2522 | * re-transmit to the target endpoint. Receiver devices (which may be |
| 2523 | * either the same as the transmitting device or different devices) may |
| 2524 | * instantiate, prepare, and start a receiver player using a setDataSource |
| 2525 | * URL of the form... |
| 2526 | * |
| 2527 | * aahRX://<multicastIP>:<port> |
| 2528 | * |
| 2529 | * to receive, decode and render the re-transmitted content. |
| 2530 | * |
| 2531 | * setRetransmitEndpoint may only be called before setDataSource has been |
| 2532 | * called; while the player is in the Idle state. |
| 2533 | * |
| 2534 | * @param endpoint the address and UDP port of the re-transmission target or |
| 2535 | * null if no re-transmission is to be performed. |
| 2536 | * @throws IllegalStateException if it is called in an invalid state |
| 2537 | * @throws IllegalArgumentException if the retransmit endpoint is supplied, |
| 2538 | * but invalid. |
| 2539 | * |
| 2540 | * {@hide} pending API council |
| 2541 | */ |
| 2542 | public void setRetransmitEndpoint(InetSocketAddress endpoint) |
| 2543 | throws IllegalStateException, IllegalArgumentException |
| 2544 | { |
| 2545 | String addrString = null; |
| 2546 | int port = 0; |
| 2547 | |
| 2548 | if (null != endpoint) { |
| 2549 | addrString = endpoint.getAddress().getHostAddress(); |
| 2550 | port = endpoint.getPort(); |
| 2551 | } |
| 2552 | |
| 2553 | int ret = native_setRetransmitEndpoint(addrString, port); |
| 2554 | if (ret != 0) { |
| 2555 | throw new IllegalArgumentException("Illegal re-transmit endpoint; native ret " + ret); |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | private native final int native_setRetransmitEndpoint(String addrString, int port); |
| 2560 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2561 | @Override |
| 2562 | protected void finalize() { native_finalize(); } |
| 2563 | |
| 2564 | /* Do not change these values without updating their counterparts |
| 2565 | * in include/media/mediaplayer.h! |
| 2566 | */ |
| 2567 | private static final int MEDIA_NOP = 0; // interface test message |
| 2568 | private static final int MEDIA_PREPARED = 1; |
| 2569 | private static final int MEDIA_PLAYBACK_COMPLETE = 2; |
| 2570 | private static final int MEDIA_BUFFERING_UPDATE = 3; |
| 2571 | private static final int MEDIA_SEEK_COMPLETE = 4; |
| 2572 | private static final int MEDIA_SET_VIDEO_SIZE = 5; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 2573 | private static final int MEDIA_STARTED = 6; |
| 2574 | private static final int MEDIA_PAUSED = 7; |
| 2575 | private static final int MEDIA_STOPPED = 8; |
Lajos Molnar | d58b122 | 2013-09-24 18:04:23 -0700 | [diff] [blame] | 2576 | private static final int MEDIA_SKIPPED = 9; |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2577 | private static final int MEDIA_TIMED_TEXT = 99; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2578 | private static final int MEDIA_ERROR = 100; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 2579 | private static final int MEDIA_INFO = 200; |
Chong Zhang | 83ddaf6 | 2013-08-06 09:43:22 -0700 | [diff] [blame] | 2580 | private static final int MEDIA_SUBTITLE_DATA = 201; |
Robert Shih | ac033f0 | 2015-04-10 11:15:02 -0700 | [diff] [blame] | 2581 | private static final int MEDIA_META_DATA = 202; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2582 | |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 2583 | private TimeProvider mTimeProvider; |
| 2584 | |
| 2585 | /** @hide */ |
| 2586 | public MediaTimeProvider getMediaTimeProvider() { |
Lajos Molnar | a67a443 | 2013-09-06 06:30:35 -0700 | [diff] [blame] | 2587 | if (mTimeProvider == null) { |
| 2588 | mTimeProvider = new TimeProvider(this); |
| 2589 | } |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 2590 | return mTimeProvider; |
| 2591 | } |
| 2592 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2593 | private class EventHandler extends Handler |
| 2594 | { |
| 2595 | private MediaPlayer mMediaPlayer; |
| 2596 | |
| 2597 | public EventHandler(MediaPlayer mp, Looper looper) { |
| 2598 | super(looper); |
| 2599 | mMediaPlayer = mp; |
| 2600 | } |
| 2601 | |
| 2602 | @Override |
| 2603 | public void handleMessage(Message msg) { |
| 2604 | if (mMediaPlayer.mNativeContext == 0) { |
| 2605 | Log.w(TAG, "mediaplayer went away with unhandled events"); |
| 2606 | return; |
| 2607 | } |
| 2608 | switch(msg.what) { |
| 2609 | case MEDIA_PREPARED: |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2610 | scanInternalSubtitleTracks(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2611 | if (mOnPreparedListener != null) |
| 2612 | mOnPreparedListener.onPrepared(mMediaPlayer); |
| 2613 | return; |
| 2614 | |
| 2615 | case MEDIA_PLAYBACK_COMPLETE: |
| 2616 | if (mOnCompletionListener != null) |
| 2617 | mOnCompletionListener.onCompletion(mMediaPlayer); |
| 2618 | stayAwake(false); |
| 2619 | return; |
| 2620 | |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 2621 | case MEDIA_STOPPED: |
Wei Jia | af2d2ac | 2015-03-30 11:16:29 -0700 | [diff] [blame] | 2622 | { |
| 2623 | TimeProvider timeProvider = mTimeProvider; |
| 2624 | if (timeProvider != null) { |
| 2625 | timeProvider.onStopped(); |
| 2626 | } |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 2627 | } |
| 2628 | break; |
| 2629 | |
| 2630 | case MEDIA_STARTED: |
| 2631 | case MEDIA_PAUSED: |
Wei Jia | af2d2ac | 2015-03-30 11:16:29 -0700 | [diff] [blame] | 2632 | { |
| 2633 | TimeProvider timeProvider = mTimeProvider; |
| 2634 | if (timeProvider != null) { |
| 2635 | timeProvider.onPaused(msg.what == MEDIA_PAUSED); |
| 2636 | } |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 2637 | } |
| 2638 | break; |
| 2639 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2640 | case MEDIA_BUFFERING_UPDATE: |
| 2641 | if (mOnBufferingUpdateListener != null) |
| 2642 | mOnBufferingUpdateListener.onBufferingUpdate(mMediaPlayer, msg.arg1); |
| 2643 | return; |
| 2644 | |
| 2645 | case MEDIA_SEEK_COMPLETE: |
Wei Jia | af2d2ac | 2015-03-30 11:16:29 -0700 | [diff] [blame] | 2646 | if (mOnSeekCompleteListener != null) { |
| 2647 | mOnSeekCompleteListener.onSeekComplete(mMediaPlayer); |
| 2648 | } |
| 2649 | // fall through |
Lajos Molnar | d58b122 | 2013-09-24 18:04:23 -0700 | [diff] [blame] | 2650 | |
| 2651 | case MEDIA_SKIPPED: |
Wei Jia | af2d2ac | 2015-03-30 11:16:29 -0700 | [diff] [blame] | 2652 | { |
| 2653 | TimeProvider timeProvider = mTimeProvider; |
| 2654 | if (timeProvider != null) { |
| 2655 | timeProvider.onSeekComplete(mMediaPlayer); |
| 2656 | } |
| 2657 | } |
| 2658 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2659 | |
| 2660 | case MEDIA_SET_VIDEO_SIZE: |
Wei Jia | af2d2ac | 2015-03-30 11:16:29 -0700 | [diff] [blame] | 2661 | if (mOnVideoSizeChangedListener != null) { |
| 2662 | mOnVideoSizeChangedListener.onVideoSizeChanged( |
| 2663 | mMediaPlayer, msg.arg1, msg.arg2); |
| 2664 | } |
| 2665 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2666 | |
| 2667 | case MEDIA_ERROR: |
| 2668 | Log.e(TAG, "Error (" + msg.arg1 + "," + msg.arg2 + ")"); |
| 2669 | boolean error_was_handled = false; |
| 2670 | if (mOnErrorListener != null) { |
| 2671 | error_was_handled = mOnErrorListener.onError(mMediaPlayer, msg.arg1, msg.arg2); |
| 2672 | } |
| 2673 | if (mOnCompletionListener != null && ! error_was_handled) { |
| 2674 | mOnCompletionListener.onCompletion(mMediaPlayer); |
| 2675 | } |
| 2676 | stayAwake(false); |
| 2677 | return; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 2678 | |
| 2679 | case MEDIA_INFO: |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2680 | switch (msg.arg1) { |
| 2681 | case MEDIA_INFO_VIDEO_TRACK_LAGGING: |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 2682 | Log.i(TAG, "Info (" + msg.arg1 + "," + msg.arg2 + ")"); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2683 | break; |
| 2684 | case MEDIA_INFO_METADATA_UPDATE: |
| 2685 | scanInternalSubtitleTracks(); |
Lajos Molnar | 9d48089 | 2013-09-11 22:10:01 -0700 | [diff] [blame] | 2686 | // fall through |
| 2687 | |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2688 | case MEDIA_INFO_EXTERNAL_METADATA_UPDATE: |
| 2689 | msg.arg1 = MEDIA_INFO_METADATA_UPDATE; |
Lajos Molnar | 9d48089 | 2013-09-11 22:10:01 -0700 | [diff] [blame] | 2690 | // update default track selection |
Chong Zhang | 7026020 | 2014-06-24 10:53:26 -0700 | [diff] [blame] | 2691 | if (mSubtitleController != null) { |
| 2692 | mSubtitleController.selectDefaultTrack(); |
| 2693 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2694 | break; |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 2695 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 2696 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 2697 | if (mOnInfoListener != null) { |
| 2698 | mOnInfoListener.onInfo(mMediaPlayer, msg.arg1, msg.arg2); |
| 2699 | } |
| 2700 | // No real default action so far. |
| 2701 | return; |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2702 | case MEDIA_TIMED_TEXT: |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2703 | if (mOnTimedTextListener == null) |
| 2704 | return; |
| 2705 | if (msg.obj == null) { |
| 2706 | mOnTimedTextListener.onTimedText(mMediaPlayer, null); |
| 2707 | } else { |
Insun Kang | 8902097 | 2012-05-01 14:13:19 +0900 | [diff] [blame] | 2708 | if (msg.obj instanceof Parcel) { |
| 2709 | Parcel parcel = (Parcel)msg.obj; |
| 2710 | TimedText text = new TimedText(parcel); |
Insun Kang | 333c099 | 2012-07-10 12:47:03 +0900 | [diff] [blame] | 2711 | parcel.recycle(); |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2712 | mOnTimedTextListener.onTimedText(mMediaPlayer, text); |
Gloria Wang | eaa5d8f | 2011-05-31 16:08:47 -0700 | [diff] [blame] | 2713 | } |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2714 | } |
| 2715 | return; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 2716 | |
Chong Zhang | 83ddaf6 | 2013-08-06 09:43:22 -0700 | [diff] [blame] | 2717 | case MEDIA_SUBTITLE_DATA: |
| 2718 | if (mOnSubtitleDataListener == null) { |
| 2719 | return; |
| 2720 | } |
| 2721 | if (msg.obj instanceof Parcel) { |
| 2722 | Parcel parcel = (Parcel) msg.obj; |
| 2723 | SubtitleData data = new SubtitleData(parcel); |
| 2724 | parcel.recycle(); |
| 2725 | mOnSubtitleDataListener.onSubtitleData(mMediaPlayer, data); |
| 2726 | } |
| 2727 | return; |
| 2728 | |
Robert Shih | ac033f0 | 2015-04-10 11:15:02 -0700 | [diff] [blame] | 2729 | case MEDIA_META_DATA: |
| 2730 | if (mOnTimedMetaDataListener == null) { |
| 2731 | return; |
| 2732 | } |
| 2733 | if (msg.obj instanceof Parcel) { |
| 2734 | Parcel parcel = (Parcel) msg.obj; |
| 2735 | TimedMetaData data = TimedMetaData.createTimedMetaDataFromParcel(parcel); |
| 2736 | parcel.recycle(); |
| 2737 | mOnTimedMetaDataListener.onTimedMetaData(mMediaPlayer, data); |
| 2738 | } |
| 2739 | return; |
| 2740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2741 | case MEDIA_NOP: // interface test message - ignore |
| 2742 | break; |
| 2743 | |
| 2744 | default: |
| 2745 | Log.e(TAG, "Unknown message type " + msg.what); |
| 2746 | return; |
| 2747 | } |
| 2748 | } |
| 2749 | } |
| 2750 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 2751 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2752 | * Called from native code when an interesting event happens. This method |
| 2753 | * just uses the EventHandler system to post the event back to the main app thread. |
| 2754 | * We use a weak reference to the original MediaPlayer object so that the native |
| 2755 | * code is safe from the object disappearing from underneath it. (This is |
| 2756 | * the cookie passed to native_setup().) |
| 2757 | */ |
| 2758 | private static void postEventFromNative(Object mediaplayer_ref, |
| 2759 | int what, int arg1, int arg2, Object obj) |
| 2760 | { |
| 2761 | MediaPlayer mp = (MediaPlayer)((WeakReference)mediaplayer_ref).get(); |
| 2762 | if (mp == null) { |
| 2763 | return; |
| 2764 | } |
| 2765 | |
Marco Nelissen | 84b8320 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2766 | if (what == MEDIA_INFO && arg1 == MEDIA_INFO_STARTED_AS_NEXT) { |
| 2767 | // this acquires the wakelock if needed, and sets the client side state |
| 2768 | mp.start(); |
| 2769 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2770 | if (mp.mEventHandler != null) { |
| 2771 | Message m = mp.mEventHandler.obtainMessage(what, arg1, arg2, obj); |
| 2772 | mp.mEventHandler.sendMessage(m); |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * Interface definition for a callback to be invoked when the media |
| 2778 | * source is ready for playback. |
| 2779 | */ |
| 2780 | public interface OnPreparedListener |
| 2781 | { |
| 2782 | /** |
| 2783 | * Called when the media file is ready for playback. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2784 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2785 | * @param mp the MediaPlayer that is ready for playback |
| 2786 | */ |
| 2787 | void onPrepared(MediaPlayer mp); |
| 2788 | } |
| 2789 | |
| 2790 | /** |
| 2791 | * Register a callback to be invoked when the media source is ready |
| 2792 | * for playback. |
| 2793 | * |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2794 | * @param listener the callback that will be run |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2795 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2796 | public void setOnPreparedListener(OnPreparedListener listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2797 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2798 | mOnPreparedListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | private OnPreparedListener mOnPreparedListener; |
| 2802 | |
| 2803 | /** |
| 2804 | * Interface definition for a callback to be invoked when playback of |
| 2805 | * a media source has completed. |
| 2806 | */ |
| 2807 | public interface OnCompletionListener |
| 2808 | { |
| 2809 | /** |
| 2810 | * Called when the end of a media source is reached during playback. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2811 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2812 | * @param mp the MediaPlayer that reached the end of the file |
| 2813 | */ |
| 2814 | void onCompletion(MediaPlayer mp); |
| 2815 | } |
| 2816 | |
| 2817 | /** |
| 2818 | * Register a callback to be invoked when the end of a media source |
| 2819 | * has been reached during playback. |
| 2820 | * |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2821 | * @param listener the callback that will be run |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2822 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2823 | public void setOnCompletionListener(OnCompletionListener listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2824 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2825 | mOnCompletionListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | private OnCompletionListener mOnCompletionListener; |
| 2829 | |
| 2830 | /** |
| 2831 | * Interface definition of a callback to be invoked indicating buffering |
| 2832 | * status of a media resource being streamed over the network. |
| 2833 | */ |
| 2834 | public interface OnBufferingUpdateListener |
| 2835 | { |
| 2836 | /** |
Jean-Michel Trivi | ea76306 | 2011-04-06 10:34:09 -0700 | [diff] [blame] | 2837 | * Called to update status in buffering a media stream received through |
| 2838 | * progressive HTTP download. The received buffering percentage |
| 2839 | * indicates how much of the content has been buffered or played. |
| 2840 | * For example a buffering update of 80 percent when half the content |
| 2841 | * has already been played indicates that the next 30 percent of the |
| 2842 | * content to play has been buffered. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2843 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2844 | * @param mp the MediaPlayer the update pertains to |
Jean-Michel Trivi | ea76306 | 2011-04-06 10:34:09 -0700 | [diff] [blame] | 2845 | * @param percent the percentage (0-100) of the content |
| 2846 | * that has been buffered or played thus far |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2847 | */ |
| 2848 | void onBufferingUpdate(MediaPlayer mp, int percent); |
| 2849 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2850 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2851 | /** |
| 2852 | * Register a callback to be invoked when the status of a network |
| 2853 | * stream's buffer has changed. |
| 2854 | * |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2855 | * @param listener the callback that will be run. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2856 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2857 | public void setOnBufferingUpdateListener(OnBufferingUpdateListener listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2858 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2859 | mOnBufferingUpdateListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | private OnBufferingUpdateListener mOnBufferingUpdateListener; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2863 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2864 | /** |
| 2865 | * Interface definition of a callback to be invoked indicating |
| 2866 | * the completion of a seek operation. |
| 2867 | */ |
| 2868 | public interface OnSeekCompleteListener |
| 2869 | { |
| 2870 | /** |
| 2871 | * Called to indicate the completion of a seek operation. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2872 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2873 | * @param mp the MediaPlayer that issued the seek operation |
| 2874 | */ |
| 2875 | public void onSeekComplete(MediaPlayer mp); |
| 2876 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2877 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2878 | /** |
| 2879 | * Register a callback to be invoked when a seek operation has been |
| 2880 | * completed. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2881 | * |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2882 | * @param listener the callback that will be run |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2883 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2884 | public void setOnSeekCompleteListener(OnSeekCompleteListener listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2885 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2886 | mOnSeekCompleteListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2887 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2888 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2889 | private OnSeekCompleteListener mOnSeekCompleteListener; |
| 2890 | |
| 2891 | /** |
| 2892 | * Interface definition of a callback to be invoked when the |
| 2893 | * video size is first known or updated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2894 | */ |
| 2895 | public interface OnVideoSizeChangedListener |
| 2896 | { |
| 2897 | /** |
| 2898 | * Called to indicate the video size |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2899 | * |
James Dong | ca402cd | 2011-12-06 17:55:38 -0800 | [diff] [blame] | 2900 | * The video size (width and height) could be 0 if there was no video, |
| 2901 | * no display surface was set, or the value was not determined yet. |
| 2902 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2903 | * @param mp the MediaPlayer associated with this callback |
| 2904 | * @param width the width of the video |
| 2905 | * @param height the height of the video |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2906 | */ |
| 2907 | public void onVideoSizeChanged(MediaPlayer mp, int width, int height); |
| 2908 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2910 | /** |
| 2911 | * Register a callback to be invoked when the video size is |
| 2912 | * known or updated. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2913 | * |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2914 | * @param listener the callback that will be run |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2915 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2916 | public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2917 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2918 | mOnVideoSizeChangedListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2919 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 2920 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2921 | private OnVideoSizeChangedListener mOnVideoSizeChangedListener; |
| 2922 | |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2923 | /** |
| 2924 | * Interface definition of a callback to be invoked when a |
| 2925 | * timed text is available for display. |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2926 | */ |
| 2927 | public interface OnTimedTextListener |
| 2928 | { |
| 2929 | /** |
Gloria Wang | eaa5d8f | 2011-05-31 16:08:47 -0700 | [diff] [blame] | 2930 | * Called to indicate an avaliable timed text |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2931 | * |
| 2932 | * @param mp the MediaPlayer associated with this callback |
Gloria Wang | eaa5d8f | 2011-05-31 16:08:47 -0700 | [diff] [blame] | 2933 | * @param text the timed text sample which contains the text |
| 2934 | * needed to be displayed and the display format. |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2935 | */ |
Gloria Wang | eaa5d8f | 2011-05-31 16:08:47 -0700 | [diff] [blame] | 2936 | public void onTimedText(MediaPlayer mp, TimedText text); |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | /** |
| 2940 | * Register a callback to be invoked when a timed text is available |
| 2941 | * for display. |
| 2942 | * |
| 2943 | * @param listener the callback that will be run |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2944 | */ |
| 2945 | public void setOnTimedTextListener(OnTimedTextListener listener) |
| 2946 | { |
| 2947 | mOnTimedTextListener = listener; |
| 2948 | } |
| 2949 | |
| 2950 | private OnTimedTextListener mOnTimedTextListener; |
| 2951 | |
Chong Zhang | 83ddaf6 | 2013-08-06 09:43:22 -0700 | [diff] [blame] | 2952 | /** |
| 2953 | * Interface definition of a callback to be invoked when a |
| 2954 | * track has data available. |
| 2955 | * |
| 2956 | * @hide |
| 2957 | */ |
| 2958 | public interface OnSubtitleDataListener |
| 2959 | { |
| 2960 | public void onSubtitleData(MediaPlayer mp, SubtitleData data); |
| 2961 | } |
| 2962 | |
| 2963 | /** |
| 2964 | * Register a callback to be invoked when a track has data available. |
| 2965 | * |
| 2966 | * @param listener the callback that will be run |
| 2967 | * |
| 2968 | * @hide |
| 2969 | */ |
| 2970 | public void setOnSubtitleDataListener(OnSubtitleDataListener listener) |
| 2971 | { |
| 2972 | mOnSubtitleDataListener = listener; |
| 2973 | } |
| 2974 | |
| 2975 | private OnSubtitleDataListener mOnSubtitleDataListener; |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 2976 | |
Robert Shih | ac033f0 | 2015-04-10 11:15:02 -0700 | [diff] [blame] | 2977 | /** |
| 2978 | * Interface definition of a callback to be invoked when a |
| 2979 | * track has timed metadata available. |
| 2980 | * |
| 2981 | * @see MediaPlayer#setOnTimedMetaDataListener(OnTimedMetaDataListener) |
| 2982 | */ |
| 2983 | public interface OnTimedMetaDataListener |
| 2984 | { |
| 2985 | /** |
| 2986 | * Called to indicate avaliable timed metadata |
| 2987 | * <p> |
| 2988 | * This method will be called as timed metadata is extracted from the media, |
| 2989 | * in the same order as it occurs in the media. The timing of this event is |
| 2990 | * not controlled by the associated timestamp. |
| 2991 | * |
| 2992 | * @param mp the MediaPlayer associated with this callback |
| 2993 | * @param data the timed metadata sample associated with this event |
| 2994 | */ |
| 2995 | public void onTimedMetaData(MediaPlayer mp, TimedMetaData data); |
| 2996 | } |
| 2997 | |
| 2998 | /** |
| 2999 | * Register a callback to be invoked when a selected track has timed metadata available. |
| 3000 | * <p> |
| 3001 | * Currently only HTTP live streaming data URI's embedded with timed ID3 tags generates |
| 3002 | * {@link TimedMetaData}. |
| 3003 | * |
| 3004 | * @see MediaPlayer#selectTrack(int) |
| 3005 | * @see MediaPlayer.OnTimedMetaDataListener |
| 3006 | * @see TimedMetaData |
| 3007 | * |
| 3008 | * @param listener the callback that will be run |
| 3009 | */ |
| 3010 | public void setOnTimedMetaDataListener(OnTimedMetaDataListener listener) |
| 3011 | { |
| 3012 | mOnTimedMetaDataListener = listener; |
| 3013 | } |
| 3014 | |
| 3015 | private OnTimedMetaDataListener mOnTimedMetaDataListener; |
| 3016 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3017 | /* Do not change these values without updating their counterparts |
| 3018 | * in include/media/mediaplayer.h! |
| 3019 | */ |
| 3020 | /** Unspecified media player error. |
| 3021 | * @see android.media.MediaPlayer.OnErrorListener |
| 3022 | */ |
| 3023 | public static final int MEDIA_ERROR_UNKNOWN = 1; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3024 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3025 | /** Media server died. In this case, the application must release the |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 3026 | * MediaPlayer object and instantiate a new one. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3027 | * @see android.media.MediaPlayer.OnErrorListener |
| 3028 | */ |
| 3029 | public static final int MEDIA_ERROR_SERVER_DIED = 100; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 3030 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3031 | /** The video is streamed and its container is not valid for progressive |
| 3032 | * playback i.e the video's index (e.g moov atom) is not at the start of the |
| 3033 | * file. |
Niko Catania | 4a0029f | 2009-03-24 19:51:09 -0700 | [diff] [blame] | 3034 | * @see android.media.MediaPlayer.OnErrorListener |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3035 | */ |
| 3036 | public static final int MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3037 | |
James Dong | 2adac49 | 2012-08-26 13:00:30 -0700 | [diff] [blame] | 3038 | /** File or network related operation errors. */ |
| 3039 | public static final int MEDIA_ERROR_IO = -1004; |
| 3040 | /** Bitstream is not conforming to the related coding standard or file spec. */ |
| 3041 | public static final int MEDIA_ERROR_MALFORMED = -1007; |
| 3042 | /** Bitstream is conforming to the related coding standard or file spec, but |
| 3043 | * the media framework does not support the feature. */ |
| 3044 | public static final int MEDIA_ERROR_UNSUPPORTED = -1010; |
| 3045 | /** Some operation takes too long to complete, usually more than 3-5 seconds. */ |
| 3046 | public static final int MEDIA_ERROR_TIMED_OUT = -110; |
| 3047 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3048 | /** |
| 3049 | * Interface definition of a callback to be invoked when there |
| 3050 | * has been an error during an asynchronous operation (other errors |
| 3051 | * will throw exceptions at method call time). |
| 3052 | */ |
| 3053 | public interface OnErrorListener |
| 3054 | { |
| 3055 | /** |
| 3056 | * Called to indicate an error. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 3057 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3058 | * @param mp the MediaPlayer the error pertains to |
| 3059 | * @param what the type of error that has occurred: |
| 3060 | * <ul> |
| 3061 | * <li>{@link #MEDIA_ERROR_UNKNOWN} |
| 3062 | * <li>{@link #MEDIA_ERROR_SERVER_DIED} |
| 3063 | * </ul> |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3064 | * @param extra an extra code, specific to the error. Typically |
James Dong | b10ec1f | 2012-07-30 15:43:14 -0700 | [diff] [blame] | 3065 | * implementation dependent. |
James Dong | 2adac49 | 2012-08-26 13:00:30 -0700 | [diff] [blame] | 3066 | * <ul> |
| 3067 | * <li>{@link #MEDIA_ERROR_IO} |
| 3068 | * <li>{@link #MEDIA_ERROR_MALFORMED} |
| 3069 | * <li>{@link #MEDIA_ERROR_UNSUPPORTED} |
| 3070 | * <li>{@link #MEDIA_ERROR_TIMED_OUT} |
| 3071 | * </ul> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3072 | * @return True if the method handled the error, false if it didn't. |
| 3073 | * Returning false, or not having an OnErrorListener at all, will |
| 3074 | * cause the OnCompletionListener to be called. |
| 3075 | */ |
| 3076 | boolean onError(MediaPlayer mp, int what, int extra); |
| 3077 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 3078 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3079 | /** |
| 3080 | * Register a callback to be invoked when an error has happened |
| 3081 | * during an asynchronous operation. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 3082 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3083 | * @param listener the callback that will be run |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3084 | */ |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3085 | public void setOnErrorListener(OnErrorListener listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3086 | { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3087 | mOnErrorListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | private OnErrorListener mOnErrorListener; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3091 | |
| 3092 | |
| 3093 | /* Do not change these values without updating their counterparts |
| 3094 | * in include/media/mediaplayer.h! |
| 3095 | */ |
| 3096 | /** Unspecified media player info. |
| 3097 | * @see android.media.MediaPlayer.OnInfoListener |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3098 | */ |
| 3099 | public static final int MEDIA_INFO_UNKNOWN = 1; |
| 3100 | |
Marco Nelissen | 84b8320 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 3101 | /** The player was started because it was used as the next player for another |
| 3102 | * player, which just completed playback. |
| 3103 | * @see android.media.MediaPlayer.OnInfoListener |
| 3104 | * @hide |
| 3105 | */ |
| 3106 | public static final int MEDIA_INFO_STARTED_AS_NEXT = 2; |
| 3107 | |
James Dong | 15a89e6 | 2012-07-23 15:00:37 -0700 | [diff] [blame] | 3108 | /** The player just pushed the very first video frame for rendering. |
| 3109 | * @see android.media.MediaPlayer.OnInfoListener |
| 3110 | */ |
| 3111 | public static final int MEDIA_INFO_VIDEO_RENDERING_START = 3; |
| 3112 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3113 | /** The video is too complex for the decoder: it can't decode frames fast |
| 3114 | * enough. Possibly only the audio plays fine at this stage. |
| 3115 | * @see android.media.MediaPlayer.OnInfoListener |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3116 | */ |
| 3117 | public static final int MEDIA_INFO_VIDEO_TRACK_LAGGING = 700; |
| 3118 | |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 3119 | /** MediaPlayer is temporarily pausing playback internally in order to |
| 3120 | * buffer more data. |
James Dong | c36b3c6 | 2011-03-29 12:16:59 -0700 | [diff] [blame] | 3121 | * @see android.media.MediaPlayer.OnInfoListener |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 3122 | */ |
| 3123 | public static final int MEDIA_INFO_BUFFERING_START = 701; |
| 3124 | |
| 3125 | /** MediaPlayer is resuming playback after filling buffers. |
James Dong | c36b3c6 | 2011-03-29 12:16:59 -0700 | [diff] [blame] | 3126 | * @see android.media.MediaPlayer.OnInfoListener |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 3127 | */ |
| 3128 | public static final int MEDIA_INFO_BUFFERING_END = 702; |
| 3129 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3130 | /** Bad interleaving means that a media has been improperly interleaved or |
| 3131 | * not interleaved at all, e.g has all the video samples first then all the |
| 3132 | * audio ones. Video is playing but a lot of disk seeks may be happening. |
| 3133 | * @see android.media.MediaPlayer.OnInfoListener |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3134 | */ |
| 3135 | public static final int MEDIA_INFO_BAD_INTERLEAVING = 800; |
| 3136 | |
| 3137 | /** The media cannot be seeked (e.g live stream) |
| 3138 | * @see android.media.MediaPlayer.OnInfoListener |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3139 | */ |
| 3140 | public static final int MEDIA_INFO_NOT_SEEKABLE = 801; |
| 3141 | |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 3142 | /** A new set of metadata is available. |
| 3143 | * @see android.media.MediaPlayer.OnInfoListener |
| 3144 | */ |
| 3145 | public static final int MEDIA_INFO_METADATA_UPDATE = 802; |
| 3146 | |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 3147 | /** A new set of external-only metadata is available. Used by |
| 3148 | * JAVA framework to avoid triggering track scanning. |
| 3149 | * @hide |
| 3150 | */ |
| 3151 | public static final int MEDIA_INFO_EXTERNAL_METADATA_UPDATE = 803; |
| 3152 | |
Insun Kang | 41f3f71 | 2012-02-16 20:28:27 +0900 | [diff] [blame] | 3153 | /** Failed to handle timed text track properly. |
| 3154 | * @see android.media.MediaPlayer.OnInfoListener |
| 3155 | * |
| 3156 | * {@hide} |
| 3157 | */ |
| 3158 | public static final int MEDIA_INFO_TIMED_TEXT_ERROR = 900; |
| 3159 | |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 3160 | /** Subtitle track was not supported by the media framework. |
| 3161 | * @see android.media.MediaPlayer.OnInfoListener |
| 3162 | */ |
| 3163 | public static final int MEDIA_INFO_UNSUPPORTED_SUBTITLE = 901; |
| 3164 | |
| 3165 | /** Reading the subtitle track takes too long. |
| 3166 | * @see android.media.MediaPlayer.OnInfoListener |
| 3167 | */ |
| 3168 | public static final int MEDIA_INFO_SUBTITLE_TIMED_OUT = 902; |
| 3169 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3170 | /** |
| 3171 | * Interface definition of a callback to be invoked to communicate some |
| 3172 | * info and/or warning about the media or its playback. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3173 | */ |
| 3174 | public interface OnInfoListener |
| 3175 | { |
| 3176 | /** |
| 3177 | * Called to indicate an info or a warning. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 3178 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3179 | * @param mp the MediaPlayer the info pertains to. |
| 3180 | * @param what the type of info or warning. |
| 3181 | * <ul> |
| 3182 | * <li>{@link #MEDIA_INFO_UNKNOWN} |
| 3183 | * <li>{@link #MEDIA_INFO_VIDEO_TRACK_LAGGING} |
James Dong | 15a89e6 | 2012-07-23 15:00:37 -0700 | [diff] [blame] | 3184 | * <li>{@link #MEDIA_INFO_VIDEO_RENDERING_START} |
James Dong | c36b3c6 | 2011-03-29 12:16:59 -0700 | [diff] [blame] | 3185 | * <li>{@link #MEDIA_INFO_BUFFERING_START} |
| 3186 | * <li>{@link #MEDIA_INFO_BUFFERING_END} |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3187 | * <li>{@link #MEDIA_INFO_BAD_INTERLEAVING} |
| 3188 | * <li>{@link #MEDIA_INFO_NOT_SEEKABLE} |
Nicolas Catania | 9193e08 | 2009-07-06 12:43:36 -0700 | [diff] [blame] | 3189 | * <li>{@link #MEDIA_INFO_METADATA_UPDATE} |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 3190 | * <li>{@link #MEDIA_INFO_UNSUPPORTED_SUBTITLE} |
| 3191 | * <li>{@link #MEDIA_INFO_SUBTITLE_TIMED_OUT} |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3192 | * </ul> |
| 3193 | * @param extra an extra code, specific to the info. Typically |
James Dong | b10ec1f | 2012-07-30 15:43:14 -0700 | [diff] [blame] | 3194 | * implementation dependent. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3195 | * @return True if the method handled the info, false if it didn't. |
| 3196 | * Returning false, or not having an OnErrorListener at all, will |
| 3197 | * cause the info to be discarded. |
| 3198 | */ |
| 3199 | boolean onInfo(MediaPlayer mp, int what, int extra); |
| 3200 | } |
| 3201 | |
| 3202 | /** |
| 3203 | * Register a callback to be invoked when an info/warning is available. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 3204 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3205 | * @param listener the callback that will be run |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 3206 | */ |
| 3207 | public void setOnInfoListener(OnInfoListener listener) |
| 3208 | { |
| 3209 | mOnInfoListener = listener; |
| 3210 | } |
| 3211 | |
| 3212 | private OnInfoListener mOnInfoListener; |
Marco Nelissen | c39d2e3 | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 3213 | |
James Dong | 454014e | 2012-04-28 16:03:55 -0700 | [diff] [blame] | 3214 | /* |
| 3215 | * Test whether a given video scaling mode is supported. |
| 3216 | */ |
| 3217 | private boolean isVideoScalingModeSupported(int mode) { |
| 3218 | return (mode == VIDEO_SCALING_MODE_SCALE_TO_FIT || |
| 3219 | mode == VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); |
| 3220 | } |
Andreas Huber | d5f9fa5 | 2013-05-28 14:39:39 -0700 | [diff] [blame] | 3221 | |
Wei Jia | d93fcf4 | 2015-02-09 16:05:53 -0800 | [diff] [blame] | 3222 | /* |
| 3223 | * Test whether a given audio playback mode is supported. |
| 3224 | * TODO query supported AudioPlaybackMode from player. |
| 3225 | */ |
| 3226 | private boolean isAudioPlaybackModeSupported(int mode) { |
| 3227 | return (mode == PLAYBACK_RATE_AUDIO_MODE_RESAMPLE); |
| 3228 | } |
| 3229 | |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3230 | /** @hide */ |
| 3231 | static class TimeProvider implements MediaPlayer.OnSeekCompleteListener, |
| 3232 | MediaTimeProvider { |
| 3233 | private static final String TAG = "MTP"; |
| 3234 | private static final long MAX_NS_WITHOUT_POSITION_CHECK = 5000000000L; |
| 3235 | private static final long MAX_EARLY_CALLBACK_US = 1000; |
| 3236 | private static final long TIME_ADJUSTMENT_RATE = 2; /* meaning 1/2 */ |
| 3237 | private long mLastTimeUs = 0; |
| 3238 | private MediaPlayer mPlayer; |
| 3239 | private boolean mPaused = true; |
| 3240 | private boolean mStopped = true; |
| 3241 | private long mLastReportedTime; |
| 3242 | private long mTimeAdjustment; |
| 3243 | // since we are expecting only a handful listeners per stream, there is |
| 3244 | // no need for log(N) search performance |
| 3245 | private MediaTimeProvider.OnMediaTimeListener mListeners[]; |
| 3246 | private long mTimes[]; |
| 3247 | private long mLastNanoTime; |
| 3248 | private Handler mEventHandler; |
| 3249 | private boolean mRefresh = false; |
| 3250 | private boolean mPausing = false; |
Lajos Molnar | d58b122 | 2013-09-24 18:04:23 -0700 | [diff] [blame] | 3251 | private boolean mSeeking = false; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3252 | private static final int NOTIFY = 1; |
| 3253 | private static final int NOTIFY_TIME = 0; |
| 3254 | private static final int REFRESH_AND_NOTIFY_TIME = 1; |
| 3255 | private static final int NOTIFY_STOP = 2; |
| 3256 | private static final int NOTIFY_SEEK = 3; |
Lajos Molnar | a67a443 | 2013-09-06 06:30:35 -0700 | [diff] [blame] | 3257 | private HandlerThread mHandlerThread; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3258 | |
| 3259 | /** @hide */ |
| 3260 | public boolean DEBUG = false; |
| 3261 | |
| 3262 | public TimeProvider(MediaPlayer mp) { |
| 3263 | mPlayer = mp; |
| 3264 | try { |
| 3265 | getCurrentTimeUs(true, false); |
| 3266 | } catch (IllegalStateException e) { |
| 3267 | // we assume starting position |
| 3268 | mRefresh = true; |
| 3269 | } |
Lajos Molnar | a67a443 | 2013-09-06 06:30:35 -0700 | [diff] [blame] | 3270 | |
| 3271 | Looper looper; |
| 3272 | if ((looper = Looper.myLooper()) == null && |
| 3273 | (looper = Looper.getMainLooper()) == null) { |
| 3274 | // Create our own looper here in case MP was created without one |
| 3275 | mHandlerThread = new HandlerThread("MediaPlayerMTPEventThread", |
| 3276 | Process.THREAD_PRIORITY_FOREGROUND); |
| 3277 | mHandlerThread.start(); |
| 3278 | looper = mHandlerThread.getLooper(); |
| 3279 | } |
| 3280 | mEventHandler = new EventHandler(looper); |
| 3281 | |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3282 | mListeners = new MediaTimeProvider.OnMediaTimeListener[0]; |
| 3283 | mTimes = new long[0]; |
| 3284 | mLastTimeUs = 0; |
| 3285 | mTimeAdjustment = 0; |
| 3286 | } |
| 3287 | |
| 3288 | private void scheduleNotification(int type, long delayUs) { |
Lajos Molnar | d58b122 | 2013-09-24 18:04:23 -0700 | [diff] [blame] | 3289 | // ignore time notifications until seek is handled |
| 3290 | if (mSeeking && |
| 3291 | (type == NOTIFY_TIME || type == REFRESH_AND_NOTIFY_TIME)) { |
| 3292 | return; |
| 3293 | } |
| 3294 | |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3295 | if (DEBUG) Log.v(TAG, "scheduleNotification " + type + " in " + delayUs); |
| 3296 | mEventHandler.removeMessages(NOTIFY); |
| 3297 | Message msg = mEventHandler.obtainMessage(NOTIFY, type, 0); |
| 3298 | mEventHandler.sendMessageDelayed(msg, (int) (delayUs / 1000)); |
| 3299 | } |
| 3300 | |
| 3301 | /** @hide */ |
| 3302 | public void close() { |
| 3303 | mEventHandler.removeMessages(NOTIFY); |
Lajos Molnar | a67a443 | 2013-09-06 06:30:35 -0700 | [diff] [blame] | 3304 | if (mHandlerThread != null) { |
| 3305 | mHandlerThread.quitSafely(); |
| 3306 | mHandlerThread = null; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | /** @hide */ |
| 3311 | protected void finalize() { |
| 3312 | if (mHandlerThread != null) { |
| 3313 | mHandlerThread.quitSafely(); |
| 3314 | } |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | /** @hide */ |
| 3318 | public void onPaused(boolean paused) { |
| 3319 | synchronized(this) { |
| 3320 | if (DEBUG) Log.d(TAG, "onPaused: " + paused); |
| 3321 | if (mStopped) { // handle as seek if we were stopped |
Lajos Molnar | 4de8e7c | 2013-10-28 07:16:41 -0700 | [diff] [blame] | 3322 | mStopped = false; |
| 3323 | mSeeking = true; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3324 | scheduleNotification(NOTIFY_SEEK, 0 /* delay */); |
| 3325 | } else { |
| 3326 | mPausing = paused; // special handling if player disappeared |
Lajos Molnar | 4de8e7c | 2013-10-28 07:16:41 -0700 | [diff] [blame] | 3327 | mSeeking = false; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3328 | scheduleNotification(REFRESH_AND_NOTIFY_TIME, 0 /* delay */); |
| 3329 | } |
| 3330 | } |
| 3331 | } |
| 3332 | |
| 3333 | /** @hide */ |
| 3334 | public void onStopped() { |
| 3335 | synchronized(this) { |
| 3336 | if (DEBUG) Log.d(TAG, "onStopped"); |
| 3337 | mPaused = true; |
Lajos Molnar | 4de8e7c | 2013-10-28 07:16:41 -0700 | [diff] [blame] | 3338 | mStopped = true; |
| 3339 | mSeeking = false; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3340 | scheduleNotification(NOTIFY_STOP, 0 /* delay */); |
| 3341 | } |
| 3342 | } |
| 3343 | |
| 3344 | /** @hide */ |
| 3345 | @Override |
| 3346 | public void onSeekComplete(MediaPlayer mp) { |
| 3347 | synchronized(this) { |
Lajos Molnar | 4de8e7c | 2013-10-28 07:16:41 -0700 | [diff] [blame] | 3348 | mStopped = false; |
| 3349 | mSeeking = true; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3350 | scheduleNotification(NOTIFY_SEEK, 0 /* delay */); |
| 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | /** @hide */ |
| 3355 | public void onNewPlayer() { |
| 3356 | if (mRefresh) { |
| 3357 | synchronized(this) { |
Lajos Molnar | 4de8e7c | 2013-10-28 07:16:41 -0700 | [diff] [blame] | 3358 | mStopped = false; |
| 3359 | mSeeking = true; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3360 | scheduleNotification(NOTIFY_SEEK, 0 /* delay */); |
| 3361 | } |
| 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | private synchronized void notifySeek() { |
Lajos Molnar | d58b122 | 2013-09-24 18:04:23 -0700 | [diff] [blame] | 3366 | mSeeking = false; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3367 | try { |
| 3368 | long timeUs = getCurrentTimeUs(true, false); |
| 3369 | if (DEBUG) Log.d(TAG, "onSeekComplete at " + timeUs); |
| 3370 | |
| 3371 | for (MediaTimeProvider.OnMediaTimeListener listener: mListeners) { |
| 3372 | if (listener == null) { |
| 3373 | break; |
| 3374 | } |
| 3375 | listener.onSeek(timeUs); |
| 3376 | } |
| 3377 | } catch (IllegalStateException e) { |
| 3378 | // we should not be there, but at least signal pause |
| 3379 | if (DEBUG) Log.d(TAG, "onSeekComplete but no player"); |
| 3380 | mPausing = true; // special handling if player disappeared |
| 3381 | notifyTimedEvent(false /* refreshTime */); |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | private synchronized void notifyStop() { |
| 3386 | for (MediaTimeProvider.OnMediaTimeListener listener: mListeners) { |
| 3387 | if (listener == null) { |
| 3388 | break; |
| 3389 | } |
| 3390 | listener.onStop(); |
| 3391 | } |
| 3392 | } |
| 3393 | |
| 3394 | private int registerListener(MediaTimeProvider.OnMediaTimeListener listener) { |
| 3395 | int i = 0; |
| 3396 | for (; i < mListeners.length; i++) { |
| 3397 | if (mListeners[i] == listener || mListeners[i] == null) { |
| 3398 | break; |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | // new listener |
| 3403 | if (i >= mListeners.length) { |
| 3404 | MediaTimeProvider.OnMediaTimeListener[] newListeners = |
| 3405 | new MediaTimeProvider.OnMediaTimeListener[i + 1]; |
| 3406 | long[] newTimes = new long[i + 1]; |
| 3407 | System.arraycopy(mListeners, 0, newListeners, 0, mListeners.length); |
| 3408 | System.arraycopy(mTimes, 0, newTimes, 0, mTimes.length); |
| 3409 | mListeners = newListeners; |
| 3410 | mTimes = newTimes; |
| 3411 | } |
| 3412 | |
| 3413 | if (mListeners[i] == null) { |
| 3414 | mListeners[i] = listener; |
| 3415 | mTimes[i] = MediaTimeProvider.NO_TIME; |
| 3416 | } |
| 3417 | return i; |
| 3418 | } |
| 3419 | |
| 3420 | public void notifyAt( |
| 3421 | long timeUs, MediaTimeProvider.OnMediaTimeListener listener) { |
| 3422 | synchronized(this) { |
| 3423 | if (DEBUG) Log.d(TAG, "notifyAt " + timeUs); |
| 3424 | mTimes[registerListener(listener)] = timeUs; |
| 3425 | scheduleNotification(NOTIFY_TIME, 0 /* delay */); |
| 3426 | } |
| 3427 | } |
| 3428 | |
| 3429 | public void scheduleUpdate(MediaTimeProvider.OnMediaTimeListener listener) { |
| 3430 | synchronized(this) { |
| 3431 | if (DEBUG) Log.d(TAG, "scheduleUpdate"); |
| 3432 | int i = registerListener(listener); |
| 3433 | |
Robert Shih | 8c4d53c | 2014-09-05 13:33:31 -0700 | [diff] [blame] | 3434 | if (!mStopped) { |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3435 | mTimes[i] = 0; |
| 3436 | scheduleNotification(NOTIFY_TIME, 0 /* delay */); |
| 3437 | } |
| 3438 | } |
| 3439 | } |
| 3440 | |
| 3441 | public void cancelNotifications( |
| 3442 | MediaTimeProvider.OnMediaTimeListener listener) { |
| 3443 | synchronized(this) { |
| 3444 | int i = 0; |
| 3445 | for (; i < mListeners.length; i++) { |
| 3446 | if (mListeners[i] == listener) { |
| 3447 | System.arraycopy(mListeners, i + 1, |
| 3448 | mListeners, i, mListeners.length - i - 1); |
| 3449 | System.arraycopy(mTimes, i + 1, |
| 3450 | mTimes, i, mTimes.length - i - 1); |
| 3451 | mListeners[mListeners.length - 1] = null; |
| 3452 | mTimes[mTimes.length - 1] = NO_TIME; |
| 3453 | break; |
| 3454 | } else if (mListeners[i] == null) { |
| 3455 | break; |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | scheduleNotification(NOTIFY_TIME, 0 /* delay */); |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | private synchronized void notifyTimedEvent(boolean refreshTime) { |
| 3464 | // figure out next callback |
| 3465 | long nowUs; |
| 3466 | try { |
| 3467 | nowUs = getCurrentTimeUs(refreshTime, true); |
| 3468 | } catch (IllegalStateException e) { |
| 3469 | // assume we paused until new player arrives |
| 3470 | mRefresh = true; |
| 3471 | mPausing = true; // this ensures that call succeeds |
| 3472 | nowUs = getCurrentTimeUs(refreshTime, true); |
| 3473 | } |
| 3474 | long nextTimeUs = nowUs; |
| 3475 | |
Lajos Molnar | d58b122 | 2013-09-24 18:04:23 -0700 | [diff] [blame] | 3476 | if (mSeeking) { |
| 3477 | // skip timed-event notifications until seek is complete |
| 3478 | return; |
| 3479 | } |
| 3480 | |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3481 | if (DEBUG) { |
| 3482 | StringBuilder sb = new StringBuilder(); |
| 3483 | sb.append("notifyTimedEvent(").append(mLastTimeUs).append(" -> ") |
| 3484 | .append(nowUs).append(") from {"); |
| 3485 | boolean first = true; |
| 3486 | for (long time: mTimes) { |
| 3487 | if (time == NO_TIME) { |
| 3488 | continue; |
| 3489 | } |
| 3490 | if (!first) sb.append(", "); |
| 3491 | sb.append(time); |
| 3492 | first = false; |
| 3493 | } |
| 3494 | sb.append("}"); |
| 3495 | Log.d(TAG, sb.toString()); |
| 3496 | } |
| 3497 | |
| 3498 | Vector<MediaTimeProvider.OnMediaTimeListener> activatedListeners = |
| 3499 | new Vector<MediaTimeProvider.OnMediaTimeListener>(); |
| 3500 | for (int ix = 0; ix < mTimes.length; ix++) { |
| 3501 | if (mListeners[ix] == null) { |
| 3502 | break; |
| 3503 | } |
| 3504 | if (mTimes[ix] <= NO_TIME) { |
| 3505 | // ignore, unless we were stopped |
| 3506 | } else if (mTimes[ix] <= nowUs + MAX_EARLY_CALLBACK_US) { |
| 3507 | activatedListeners.add(mListeners[ix]); |
| 3508 | if (DEBUG) Log.d(TAG, "removed"); |
| 3509 | mTimes[ix] = NO_TIME; |
| 3510 | } else if (nextTimeUs == nowUs || mTimes[ix] < nextTimeUs) { |
| 3511 | nextTimeUs = mTimes[ix]; |
| 3512 | } |
| 3513 | } |
| 3514 | |
| 3515 | if (nextTimeUs > nowUs && !mPaused) { |
| 3516 | // schedule callback at nextTimeUs |
| 3517 | if (DEBUG) Log.d(TAG, "scheduling for " + nextTimeUs + " and " + nowUs); |
| 3518 | scheduleNotification(NOTIFY_TIME, nextTimeUs - nowUs); |
| 3519 | } else { |
| 3520 | mEventHandler.removeMessages(NOTIFY); |
| 3521 | // no more callbacks |
| 3522 | } |
| 3523 | |
| 3524 | for (MediaTimeProvider.OnMediaTimeListener listener: activatedListeners) { |
| 3525 | listener.onTimedEvent(nowUs); |
| 3526 | } |
| 3527 | } |
| 3528 | |
| 3529 | private long getEstimatedTime(long nanoTime, boolean monotonic) { |
| 3530 | if (mPaused) { |
| 3531 | mLastReportedTime = mLastTimeUs + mTimeAdjustment; |
| 3532 | } else { |
| 3533 | long timeSinceRead = (nanoTime - mLastNanoTime) / 1000; |
| 3534 | mLastReportedTime = mLastTimeUs + timeSinceRead; |
| 3535 | if (mTimeAdjustment > 0) { |
| 3536 | long adjustment = |
| 3537 | mTimeAdjustment - timeSinceRead / TIME_ADJUSTMENT_RATE; |
| 3538 | if (adjustment <= 0) { |
| 3539 | mTimeAdjustment = 0; |
| 3540 | } else { |
| 3541 | mLastReportedTime += adjustment; |
| 3542 | } |
| 3543 | } |
| 3544 | } |
| 3545 | return mLastReportedTime; |
| 3546 | } |
| 3547 | |
| 3548 | public long getCurrentTimeUs(boolean refreshTime, boolean monotonic) |
| 3549 | throws IllegalStateException { |
| 3550 | synchronized (this) { |
| 3551 | // we always refresh the time when the paused-state changes, because |
| 3552 | // we expect to have received the pause-change event delayed. |
| 3553 | if (mPaused && !refreshTime) { |
| 3554 | return mLastReportedTime; |
| 3555 | } |
| 3556 | |
| 3557 | long nanoTime = System.nanoTime(); |
| 3558 | if (refreshTime || |
| 3559 | nanoTime >= mLastNanoTime + MAX_NS_WITHOUT_POSITION_CHECK) { |
| 3560 | try { |
Lajos Molnar | 7246fa5 | 2014-03-10 17:21:49 -0700 | [diff] [blame] | 3561 | mLastTimeUs = mPlayer.getCurrentPosition() * 1000L; |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3562 | mPaused = !mPlayer.isPlaying(); |
| 3563 | if (DEBUG) Log.v(TAG, (mPaused ? "paused" : "playing") + " at " + mLastTimeUs); |
| 3564 | } catch (IllegalStateException e) { |
| 3565 | if (mPausing) { |
| 3566 | // if we were pausing, get last estimated timestamp |
| 3567 | mPausing = false; |
| 3568 | getEstimatedTime(nanoTime, monotonic); |
| 3569 | mPaused = true; |
| 3570 | if (DEBUG) Log.d(TAG, "illegal state, but pausing: estimating at " + mLastReportedTime); |
| 3571 | return mLastReportedTime; |
| 3572 | } |
| 3573 | // TODO get time when prepared |
| 3574 | throw e; |
| 3575 | } |
| 3576 | mLastNanoTime = nanoTime; |
| 3577 | if (monotonic && mLastTimeUs < mLastReportedTime) { |
| 3578 | /* have to adjust time */ |
| 3579 | mTimeAdjustment = mLastReportedTime - mLastTimeUs; |
Lajos Molnar | d58b122 | 2013-09-24 18:04:23 -0700 | [diff] [blame] | 3580 | if (mTimeAdjustment > 1000000) { |
| 3581 | // schedule seeked event if time jumped significantly |
| 3582 | // TODO: do this properly by introducing an exception |
Lajos Molnar | 4de8e7c | 2013-10-28 07:16:41 -0700 | [diff] [blame] | 3583 | mStopped = false; |
| 3584 | mSeeking = true; |
Lajos Molnar | d58b122 | 2013-09-24 18:04:23 -0700 | [diff] [blame] | 3585 | scheduleNotification(NOTIFY_SEEK, 0 /* delay */); |
| 3586 | } |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3587 | } else { |
| 3588 | mTimeAdjustment = 0; |
| 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | return getEstimatedTime(nanoTime, monotonic); |
| 3593 | } |
| 3594 | } |
| 3595 | |
| 3596 | private class EventHandler extends Handler { |
Lajos Molnar | a67a443 | 2013-09-06 06:30:35 -0700 | [diff] [blame] | 3597 | public EventHandler(Looper looper) { |
| 3598 | super(looper); |
| 3599 | } |
| 3600 | |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3601 | @Override |
| 3602 | public void handleMessage(Message msg) { |
| 3603 | if (msg.what == NOTIFY) { |
| 3604 | switch (msg.arg1) { |
| 3605 | case NOTIFY_TIME: |
| 3606 | notifyTimedEvent(false /* refreshTime */); |
| 3607 | break; |
| 3608 | case REFRESH_AND_NOTIFY_TIME: |
| 3609 | notifyTimedEvent(true /* refreshTime */); |
| 3610 | break; |
| 3611 | case NOTIFY_STOP: |
| 3612 | notifyStop(); |
| 3613 | break; |
| 3614 | case NOTIFY_SEEK: |
| 3615 | notifySeek(); |
| 3616 | break; |
| 3617 | } |
| 3618 | } |
| 3619 | } |
| 3620 | } |
Lajos Molnar | 3d99856 | 2013-08-15 17:05:05 -0700 | [diff] [blame] | 3621 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3622 | } |