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.widget; |
| 18 | |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.app.AlertDialog; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.res.Resources; |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 25 | import android.graphics.Canvas; |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 26 | import android.media.AudioAttributes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.media.AudioManager; |
Jaesung Chung | 978bf5e | 2015-12-07 21:46:40 +0900 | [diff] [blame] | 28 | import android.media.Cea708CaptionRenderer; |
Chong Zhang | bdfd910 | 2014-06-11 15:10:23 -0700 | [diff] [blame] | 29 | import android.media.ClosedCaptionRenderer; |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 30 | import android.media.MediaFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.media.MediaPlayer; |
| 32 | import android.media.MediaPlayer.OnCompletionListener; |
| 33 | import android.media.MediaPlayer.OnErrorListener; |
James Dong | a0ba794 | 2012-07-27 17:30:38 -0700 | [diff] [blame] | 34 | import android.media.MediaPlayer.OnInfoListener; |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 35 | import android.media.Metadata; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 36 | import android.media.SubtitleController; |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 37 | import android.media.SubtitleTrack.RenderingWidget; |
Sungsoo Lim | ba3699b | 2014-03-17 10:24:32 +0900 | [diff] [blame] | 38 | import android.media.TtmlRenderer; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 39 | import android.media.WebVttRenderer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.net.Uri; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 41 | import android.os.Build; |
Lajos Molnar | 29f5183 | 2013-09-20 08:45:31 -0700 | [diff] [blame] | 42 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.util.AttributeSet; |
| 44 | import android.util.Log; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 45 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.view.KeyEvent; |
| 47 | import android.view.MotionEvent; |
| 48 | import android.view.SurfaceHolder; |
| 49 | import android.view.SurfaceView; |
| 50 | import android.view.View; |
Owen Lin | a8381df | 2010-11-09 19:32:30 +0800 | [diff] [blame] | 51 | import android.widget.MediaController.MediaPlayerControl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | import java.io.IOException; |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 54 | import java.io.InputStream; |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 55 | import java.util.Map; |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 56 | import java.util.Vector; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Displays a video file. The VideoView class |
| 60 | * can load images from various sources (such as resources or content |
| 61 | * providers), takes care of computing its measurement from the video so that |
| 62 | * it can be used in any layout manager, and provides various display options |
Lajos Molnar | 7e11b16 | 2013-10-30 14:04:10 -0700 | [diff] [blame] | 63 | * such as scaling and tinting.<p> |
| 64 | * |
| 65 | * <em>Note: VideoView does not retain its full state when going into the |
| 66 | * background.</em> In particular, it does not restore the current play state, |
| 67 | * play position, selected tracks, or any subtitle tracks added via |
| 68 | * {@link #addSubtitleSource addSubtitleSource()}. Applications should |
| 69 | * save and restore these on their own in |
| 70 | * {@link android.app.Activity#onSaveInstanceState} and |
| 71 | * {@link android.app.Activity#onRestoreInstanceState}.<p> |
| 72 | * Also note that the audio session id (from {@link #getAudioSessionId}) may |
| 73 | * change from its previously returned value when the VideoView is restored. |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 74 | * <p> |
| 75 | * By default, VideoView requests audio focus with {@link AudioManager#AUDIOFOCUS_GAIN}. Use |
| 76 | * {@link #setAudioFocusRequest(int)} to change this behavior. |
| 77 | * <p> |
| 78 | * The default {@link AudioAttributes} used during playback have a usage of |
| 79 | * {@link AudioAttributes#USAGE_MEDIA} and a content type of |
| 80 | * {@link AudioAttributes#CONTENT_TYPE_MOVIE}, use {@link #setAudioAttributes(AudioAttributes)} to |
| 81 | * modify them. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | */ |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 83 | public class VideoView extends SurfaceView |
| 84 | implements MediaPlayerControl, SubtitleController.Anchor { |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 85 | private static final String TAG = "VideoView"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 87 | // all possible internal states |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 88 | private static final int STATE_ERROR = -1; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 89 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 90 | private static final int STATE_IDLE = 0; |
| 91 | private static final int STATE_PREPARING = 1; |
| 92 | private static final int STATE_PREPARED = 2; |
| 93 | private static final int STATE_PLAYING = 3; |
| 94 | private static final int STATE_PAUSED = 4; |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 95 | private static final int STATE_PLAYBACK_COMPLETED = 5; |
| 96 | |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 97 | private final Vector<Pair<InputStream, MediaFormat>> mPendingSubtitleTracks = new Vector<>(); |
| 98 | |
| 99 | // settable by the client |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 100 | @UnsupportedAppUsage |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 101 | private Uri mUri; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 102 | @UnsupportedAppUsage |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 103 | private Map<String, String> mHeaders; |
| 104 | |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 105 | // mCurrentState is a VideoView object's current state. |
| 106 | // mTargetState is the state that a method caller intends to reach. |
| 107 | // For instance, regardless the VideoView object's current state, |
| 108 | // calling pause() intends to bring the object to a target state |
| 109 | // of STATE_PAUSED. |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 110 | @UnsupportedAppUsage |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 111 | private int mCurrentState = STATE_IDLE; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 112 | @UnsupportedAppUsage |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 113 | private int mTargetState = STATE_IDLE; |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | // All the stuff we need for playing and showing a video |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 116 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | private SurfaceHolder mSurfaceHolder = null; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 118 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | private MediaPlayer mMediaPlayer = null; |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 120 | private int mAudioSession; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 121 | @UnsupportedAppUsage |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 122 | private int mVideoWidth; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 123 | @UnsupportedAppUsage |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 124 | private int mVideoHeight; |
| 125 | private int mSurfaceWidth; |
| 126 | private int mSurfaceHeight; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 127 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | private MediaController mMediaController; |
| 129 | private OnCompletionListener mOnCompletionListener; |
| 130 | private MediaPlayer.OnPreparedListener mOnPreparedListener; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 131 | @UnsupportedAppUsage |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 132 | private int mCurrentBufferPercentage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | private OnErrorListener mOnErrorListener; |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 134 | private OnInfoListener mOnInfoListener; |
| 135 | private int mSeekWhenPrepared; // recording the seek position while preparing |
| 136 | private boolean mCanPause; |
| 137 | private boolean mCanSeekBack; |
| 138 | private boolean mCanSeekForward; |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 139 | private AudioManager mAudioManager; |
| 140 | private int mAudioFocusType = AudioManager.AUDIOFOCUS_GAIN; // legacy focus gain |
| 141 | private AudioAttributes mAudioAttributes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 143 | /** Subtitle rendering widget overlaid on top of the video. */ |
| 144 | private RenderingWidget mSubtitleWidget; |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 145 | |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 146 | /** Listener for changes to subtitle data, used to redraw when needed. */ |
| 147 | private RenderingWidget.OnChangedListener mSubtitlesChangedListener; |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | public VideoView(Context context) { |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 150 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | public VideoView(Context context, AttributeSet attrs) { |
| 154 | this(context, attrs, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 156 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 157 | public VideoView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 158 | this(context, attrs, defStyleAttr, 0); |
| 159 | } |
| 160 | |
| 161 | public VideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 162 | super(context, attrs, defStyleAttr, defStyleRes); |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 163 | |
| 164 | mVideoWidth = 0; |
| 165 | mVideoHeight = 0; |
| 166 | |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 167 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| 168 | mAudioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA) |
| 169 | .setContentType(AudioAttributes.CONTENT_TYPE_MOVIE).build(); |
| 170 | |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 171 | getHolder().addCallback(mSHCallback); |
| 172 | getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
| 173 | |
| 174 | setFocusable(true); |
| 175 | setFocusableInTouchMode(true); |
| 176 | requestFocus(); |
| 177 | |
| 178 | mCurrentState = STATE_IDLE; |
| 179 | mTargetState = STATE_IDLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 184 | //Log.i("@@@@", "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", " |
| 185 | // + MeasureSpec.toString(heightMeasureSpec) + ")"); |
| 186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | int width = getDefaultSize(mVideoWidth, widthMeasureSpec); |
| 188 | int height = getDefaultSize(mVideoHeight, heightMeasureSpec); |
| 189 | if (mVideoWidth > 0 && mVideoHeight > 0) { |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 190 | |
| 191 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 192 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 193 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 194 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 195 | |
| 196 | if (widthSpecMode == MeasureSpec.EXACTLY && heightSpecMode == MeasureSpec.EXACTLY) { |
| 197 | // the size is fixed |
| 198 | width = widthSpecSize; |
| 199 | height = heightSpecSize; |
| 200 | |
| 201 | // for compatibility, we adjust size based on aspect ratio |
| 202 | if ( mVideoWidth * height < width * mVideoHeight ) { |
| 203 | //Log.i("@@@", "image too wide, correcting"); |
| 204 | width = height * mVideoWidth / mVideoHeight; |
| 205 | } else if ( mVideoWidth * height > width * mVideoHeight ) { |
| 206 | //Log.i("@@@", "image too tall, correcting"); |
| 207 | height = width * mVideoHeight / mVideoWidth; |
| 208 | } |
| 209 | } else if (widthSpecMode == MeasureSpec.EXACTLY) { |
| 210 | // only the width is fixed, adjust the height to match aspect ratio if possible |
| 211 | width = widthSpecSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | height = width * mVideoHeight / mVideoWidth; |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 213 | if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) { |
| 214 | // couldn't match aspect ratio within the constraints |
| 215 | height = heightSpecSize; |
| 216 | } |
| 217 | } else if (heightSpecMode == MeasureSpec.EXACTLY) { |
| 218 | // only the height is fixed, adjust the width to match aspect ratio if possible |
| 219 | height = heightSpecSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | width = height * mVideoWidth / mVideoHeight; |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 221 | if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) { |
| 222 | // couldn't match aspect ratio within the constraints |
| 223 | width = widthSpecSize; |
| 224 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | } else { |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 226 | // neither the width nor the height are fixed, try to use actual video size |
| 227 | width = mVideoWidth; |
| 228 | height = mVideoHeight; |
| 229 | if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) { |
| 230 | // too tall, decrease both width and height |
| 231 | height = heightSpecSize; |
| 232 | width = height * mVideoWidth / mVideoHeight; |
| 233 | } |
| 234 | if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) { |
| 235 | // too wide, decrease both width and height |
| 236 | width = widthSpecSize; |
| 237 | height = width * mVideoHeight / mVideoWidth; |
| 238 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 240 | } else { |
| 241 | // no size yet, just adopt the given spec sizes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | setMeasuredDimension(width, height); |
| 244 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 245 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 246 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 247 | public CharSequence getAccessibilityClassName() { |
| 248 | return VideoView.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Marco Nelissen | c1374e8 | 2012-11-13 10:51:50 -0800 | [diff] [blame] | 251 | public int resolveAdjustedSize(int desiredSize, int measureSpec) { |
| 252 | return getDefaultSize(desiredSize, measureSpec); |
| 253 | } |
| 254 | |
Ronghua Wu | 0e9e3b2 | 2014-08-20 16:24:52 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Sets video path. |
| 257 | * |
| 258 | * @param path the path of the video. |
| 259 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | public void setVideoPath(String path) { |
| 261 | setVideoURI(Uri.parse(path)); |
| 262 | } |
| 263 | |
Ronghua Wu | 0e9e3b2 | 2014-08-20 16:24:52 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Sets video URI. |
| 266 | * |
| 267 | * @param uri the URI of the video. |
| 268 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | public void setVideoURI(Uri uri) { |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 270 | setVideoURI(uri, null); |
| 271 | } |
| 272 | |
| 273 | /** |
Ronghua Wu | 0e9e3b2 | 2014-08-20 16:24:52 -0700 | [diff] [blame] | 274 | * Sets video URI using specific headers. |
| 275 | * |
| 276 | * @param uri the URI of the video. |
| 277 | * @param headers the headers for the URI request. |
Ronghua Wu | 48db783 | 2014-08-25 14:47:58 -0700 | [diff] [blame] | 278 | * Note that the cross domain redirection is allowed by default, but that can be |
| 279 | * changed with key/value pairs through the headers parameter with |
| 280 | * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value |
| 281 | * to disallow or allow cross domain redirection. |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 282 | */ |
| 283 | public void setVideoURI(Uri uri, Map<String, String> headers) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | mUri = uri; |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 285 | mHeaders = headers; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | mSeekWhenPrepared = 0; |
| 287 | openVideo(); |
| 288 | requestLayout(); |
| 289 | invalidate(); |
| 290 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 291 | |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 292 | /** |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 293 | * Sets which type of audio focus will be requested during the playback, or configures playback |
| 294 | * to not request audio focus. Valid values for focus requests are |
| 295 | * {@link AudioManager#AUDIOFOCUS_GAIN}, {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT}, |
| 296 | * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK}, and |
| 297 | * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}. Or use |
| 298 | * {@link AudioManager#AUDIOFOCUS_NONE} to express that audio focus should not be |
| 299 | * requested when playback starts. You can for instance use this when playing a silent animation |
| 300 | * through this class, and you don't want to affect other audio applications playing in the |
| 301 | * background. |
| 302 | * @param focusGain the type of audio focus gain that will be requested, or |
| 303 | * {@link AudioManager#AUDIOFOCUS_NONE} to disable the use audio focus during playback. |
| 304 | */ |
| 305 | public void setAudioFocusRequest(int focusGain) { |
| 306 | if (focusGain != AudioManager.AUDIOFOCUS_NONE |
| 307 | && focusGain != AudioManager.AUDIOFOCUS_GAIN |
| 308 | && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT |
| 309 | && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK |
| 310 | && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) { |
| 311 | throw new IllegalArgumentException("Illegal audio focus type " + focusGain); |
| 312 | } |
| 313 | mAudioFocusType = focusGain; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Sets the {@link AudioAttributes} to be used during the playback of the video. |
| 318 | * @param attributes non-null <code>AudioAttributes</code>. |
| 319 | */ |
| 320 | public void setAudioAttributes(@NonNull AudioAttributes attributes) { |
| 321 | if (attributes == null) { |
| 322 | throw new IllegalArgumentException("Illegal null AudioAttributes"); |
| 323 | } |
| 324 | mAudioAttributes = attributes; |
| 325 | } |
| 326 | |
| 327 | /** |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 328 | * Adds an external subtitle source file (from the provided input stream.) |
| 329 | * |
| 330 | * Note that a single external subtitle source may contain multiple or no |
| 331 | * supported tracks in it. If the source contained at least one track in |
| 332 | * it, one will receive an {@link MediaPlayer#MEDIA_INFO_METADATA_UPDATE} |
| 333 | * info message. Otherwise, if reading the source takes excessive time, |
| 334 | * one will receive a {@link MediaPlayer#MEDIA_INFO_SUBTITLE_TIMED_OUT} |
| 335 | * message. If the source contained no supported track (including an empty |
| 336 | * source file or null input stream), one will receive a {@link |
| 337 | * MediaPlayer#MEDIA_INFO_UNSUPPORTED_SUBTITLE} message. One can find the |
| 338 | * total number of available tracks using {@link MediaPlayer#getTrackInfo()} |
| 339 | * to see what additional tracks become available after this method call. |
| 340 | * |
| 341 | * @param is input stream containing the subtitle data. It will be |
| 342 | * closed by the media framework. |
| 343 | * @param format the format of the subtitle track(s). Must contain at least |
| 344 | * the mime type ({@link MediaFormat#KEY_MIME}) and the |
| 345 | * language ({@link MediaFormat#KEY_LANGUAGE}) of the file. |
| 346 | * If the file itself contains the language information, |
| 347 | * specify "und" for the language. |
| 348 | */ |
| 349 | public void addSubtitleSource(InputStream is, MediaFormat format) { |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 350 | if (mMediaPlayer == null) { |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 351 | mPendingSubtitleTracks.add(Pair.create(is, format)); |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 352 | } else { |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 353 | try { |
| 354 | mMediaPlayer.addSubtitleSource(is, format); |
| 355 | } catch (IllegalStateException e) { |
| 356 | mInfoListener.onInfo( |
| 357 | mMediaPlayer, MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE, 0); |
| 358 | } |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | public void stopPlayback() { |
| 363 | if (mMediaPlayer != null) { |
| 364 | mMediaPlayer.stop(); |
| 365 | mMediaPlayer.release(); |
| 366 | mMediaPlayer = null; |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 367 | mCurrentState = STATE_IDLE; |
| 368 | mTargetState = STATE_IDLE; |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 369 | mAudioManager.abandonAudioFocus(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | private void openVideo() { |
| 374 | if (mUri == null || mSurfaceHolder == null) { |
| 375 | // not ready for playback just yet, will try again later |
| 376 | return; |
| 377 | } |
Marco Nelissen | dddeee6 | 2009-07-10 14:14:52 -0700 | [diff] [blame] | 378 | // we shouldn't clear the target state, because somebody might have |
| 379 | // called start() previously |
| 380 | release(false); |
Marco Nelissen | 926ebb8 | 2015-03-11 09:59:49 -0700 | [diff] [blame] | 381 | |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 382 | if (mAudioFocusType != AudioManager.AUDIOFOCUS_NONE) { |
| 383 | // TODO this should have a focus listener |
| 384 | mAudioManager.requestAudioFocus(null, mAudioAttributes, mAudioFocusType, 0 /*flags*/); |
| 385 | } |
Marco Nelissen | 926ebb8 | 2015-03-11 09:59:49 -0700 | [diff] [blame] | 386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | try { |
| 388 | mMediaPlayer = new MediaPlayer(); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 389 | // TODO: create SubtitleController in MediaPlayer, but we need |
| 390 | // a context for the subtitle renderers |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 391 | final Context context = getContext(); |
| 392 | final SubtitleController controller = new SubtitleController( |
| 393 | context, mMediaPlayer.getMediaTimeProvider(), mMediaPlayer); |
| 394 | controller.registerRenderer(new WebVttRenderer(context)); |
Sungsoo Lim | ba3699b | 2014-03-17 10:24:32 +0900 | [diff] [blame] | 395 | controller.registerRenderer(new TtmlRenderer(context)); |
Jaesung Chung | 978bf5e | 2015-12-07 21:46:40 +0900 | [diff] [blame] | 396 | controller.registerRenderer(new Cea708CaptionRenderer(context)); |
Chong Zhang | bdfd910 | 2014-06-11 15:10:23 -0700 | [diff] [blame] | 397 | controller.registerRenderer(new ClosedCaptionRenderer(context)); |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 398 | mMediaPlayer.setSubtitleAnchor(controller, this); |
| 399 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 400 | if (mAudioSession != 0) { |
| 401 | mMediaPlayer.setAudioSessionId(mAudioSession); |
| 402 | } else { |
| 403 | mAudioSession = mMediaPlayer.getAudioSessionId(); |
| 404 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | mMediaPlayer.setOnPreparedListener(mPreparedListener); |
| 406 | mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | mMediaPlayer.setOnCompletionListener(mCompletionListener); |
| 408 | mMediaPlayer.setOnErrorListener(mErrorListener); |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 409 | mMediaPlayer.setOnInfoListener(mInfoListener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); |
| 411 | mCurrentBufferPercentage = 0; |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 412 | mMediaPlayer.setDataSource(mContext, mUri, mHeaders); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | mMediaPlayer.setDisplay(mSurfaceHolder); |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 414 | mMediaPlayer.setAudioAttributes(mAudioAttributes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | mMediaPlayer.setScreenOnWhilePlaying(true); |
| 416 | mMediaPlayer.prepareAsync(); |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 417 | |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 418 | for (Pair<InputStream, MediaFormat> pending: mPendingSubtitleTracks) { |
| 419 | try { |
| 420 | mMediaPlayer.addSubtitleSource(pending.first, pending.second); |
| 421 | } catch (IllegalStateException e) { |
| 422 | mInfoListener.onInfo( |
| 423 | mMediaPlayer, MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE, 0); |
| 424 | } |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Marco Nelissen | dddeee6 | 2009-07-10 14:14:52 -0700 | [diff] [blame] | 427 | // we don't set the target state here either, but preserve the |
| 428 | // target state that was there before. |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 429 | mCurrentState = STATE_PREPARING; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | attachMediaController(); |
| 431 | } catch (IOException ex) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 432 | Log.w(TAG, "Unable to open content: " + mUri, ex); |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 433 | mCurrentState = STATE_ERROR; |
| 434 | mTargetState = STATE_ERROR; |
Andrei Popescu | 020d2e3 | 2009-09-30 14:54:55 +0100 | [diff] [blame] | 435 | mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | return; |
| 437 | } catch (IllegalArgumentException ex) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 438 | Log.w(TAG, "Unable to open content: " + mUri, ex); |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 439 | mCurrentState = STATE_ERROR; |
| 440 | mTargetState = STATE_ERROR; |
Andrei Popescu | 020d2e3 | 2009-09-30 14:54:55 +0100 | [diff] [blame] | 441 | mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | return; |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 443 | } finally { |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 444 | mPendingSubtitleTracks.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 447 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | public void setMediaController(MediaController controller) { |
| 449 | if (mMediaController != null) { |
| 450 | mMediaController.hide(); |
| 451 | } |
| 452 | mMediaController = controller; |
| 453 | attachMediaController(); |
| 454 | } |
| 455 | |
| 456 | private void attachMediaController() { |
| 457 | if (mMediaPlayer != null && mMediaController != null) { |
| 458 | mMediaController.setMediaPlayer(this); |
| 459 | View anchorView = this.getParent() instanceof View ? |
| 460 | (View)this.getParent() : this; |
| 461 | mMediaController.setAnchorView(anchorView); |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 462 | mMediaController.setEnabled(isInPlaybackState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | } |
| 464 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener = |
| 467 | new MediaPlayer.OnVideoSizeChangedListener() { |
| 468 | public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { |
| 469 | mVideoWidth = mp.getVideoWidth(); |
| 470 | mVideoHeight = mp.getVideoHeight(); |
| 471 | if (mVideoWidth != 0 && mVideoHeight != 0) { |
| 472 | getHolder().setFixedSize(mVideoWidth, mVideoHeight); |
Teng-Hui Zhu | f5d102e | 2012-08-23 15:24:29 -0700 | [diff] [blame] | 473 | requestLayout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | }; |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 477 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 478 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() { |
| 480 | public void onPrepared(MediaPlayer mp) { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 481 | mCurrentState = STATE_PREPARED; |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 482 | |
| 483 | // Get the capabilities of the player for this stream |
| 484 | Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL, |
| 485 | MediaPlayer.BYPASS_METADATA_FILTER); |
Andreas Huber | d44d33b | 2009-08-20 11:12:27 -0700 | [diff] [blame] | 486 | |
| 487 | if (data != null) { |
| 488 | mCanPause = !data.has(Metadata.PAUSE_AVAILABLE) |
| 489 | || data.getBoolean(Metadata.PAUSE_AVAILABLE); |
| 490 | mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE) |
| 491 | || data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE); |
| 492 | mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE) |
| 493 | || data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE); |
| 494 | } else { |
Andreas Huber | 2869e95 | 2010-03-09 09:18:01 -0800 | [diff] [blame] | 495 | mCanPause = mCanSeekBack = mCanSeekForward = true; |
Andreas Huber | d44d33b | 2009-08-20 11:12:27 -0700 | [diff] [blame] | 496 | } |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | if (mOnPreparedListener != null) { |
| 499 | mOnPreparedListener.onPrepared(mMediaPlayer); |
| 500 | } |
| 501 | if (mMediaController != null) { |
| 502 | mMediaController.setEnabled(true); |
| 503 | } |
| 504 | mVideoWidth = mp.getVideoWidth(); |
| 505 | mVideoHeight = mp.getVideoHeight(); |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 506 | |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 507 | int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be changed after seekTo() call |
| 508 | if (seekToPosition != 0) { |
| 509 | seekTo(seekToPosition); |
| 510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | if (mVideoWidth != 0 && mVideoHeight != 0) { |
| 512 | //Log.i("@@@@", "video size: " + mVideoWidth +"/"+ mVideoHeight); |
| 513 | getHolder().setFixedSize(mVideoWidth, mVideoHeight); |
| 514 | if (mSurfaceWidth == mVideoWidth && mSurfaceHeight == mVideoHeight) { |
| 515 | // We didn't actually change the size (it was already at the size |
| 516 | // we need), so we won't get a "surface changed" callback, so |
| 517 | // start the video here instead of in the callback. |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 518 | if (mTargetState == STATE_PLAYING) { |
James Dong | aa00e39 | 2009-07-02 16:26:39 -0700 | [diff] [blame] | 519 | start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | if (mMediaController != null) { |
| 521 | mMediaController.show(); |
| 522 | } |
| 523 | } else if (!isPlaying() && |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 524 | (seekToPosition != 0 || getCurrentPosition() > 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | if (mMediaController != null) { |
| 526 | // Show the media controls when we're paused into a video and make 'em stick. |
| 527 | mMediaController.show(0); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } else { |
| 532 | // We don't know the video size yet, but should start anyway. |
| 533 | // The video size might be reported to us later. |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 534 | if (mTargetState == STATE_PLAYING) { |
James Dong | aa00e39 | 2009-07-02 16:26:39 -0700 | [diff] [blame] | 535 | start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | } |
| 539 | }; |
| 540 | |
| 541 | private MediaPlayer.OnCompletionListener mCompletionListener = |
| 542 | new MediaPlayer.OnCompletionListener() { |
| 543 | public void onCompletion(MediaPlayer mp) { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 544 | mCurrentState = STATE_PLAYBACK_COMPLETED; |
| 545 | mTargetState = STATE_PLAYBACK_COMPLETED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | if (mMediaController != null) { |
| 547 | mMediaController.hide(); |
| 548 | } |
| 549 | if (mOnCompletionListener != null) { |
| 550 | mOnCompletionListener.onCompletion(mMediaPlayer); |
| 551 | } |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 552 | if (mAudioFocusType != AudioManager.AUDIOFOCUS_NONE) { |
| 553 | mAudioManager.abandonAudioFocus(null); |
| 554 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | } |
| 556 | }; |
| 557 | |
Lajos Molnar | af309824 | 2013-08-15 20:56:53 -0700 | [diff] [blame] | 558 | private MediaPlayer.OnInfoListener mInfoListener = |
| 559 | new MediaPlayer.OnInfoListener() { |
| 560 | public boolean onInfo(MediaPlayer mp, int arg1, int arg2) { |
| 561 | if (mOnInfoListener != null) { |
| 562 | mOnInfoListener.onInfo(mp, arg1, arg2); |
| 563 | } |
| 564 | return true; |
| 565 | } |
| 566 | }; |
| 567 | |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 568 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | private MediaPlayer.OnErrorListener mErrorListener = |
| 570 | new MediaPlayer.OnErrorListener() { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 571 | public boolean onError(MediaPlayer mp, int framework_err, int impl_err) { |
| 572 | Log.d(TAG, "Error: " + framework_err + "," + impl_err); |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 573 | mCurrentState = STATE_ERROR; |
| 574 | mTargetState = STATE_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | if (mMediaController != null) { |
| 576 | mMediaController.hide(); |
| 577 | } |
| 578 | |
| 579 | /* If an error handler has been supplied, use it and finish. */ |
| 580 | if (mOnErrorListener != null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 581 | if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | return true; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* Otherwise, pop up an error dialog so the user knows that |
| 587 | * something bad has happened. Only try and pop up the dialog |
| 588 | * if we're attached to a window. When we're going away and no |
| 589 | * longer have a window, don't bother showing the user an error. |
| 590 | */ |
| 591 | if (getWindowToken() != null) { |
| 592 | Resources r = mContext.getResources(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 593 | int messageId; |
| 594 | |
| 595 | if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) { |
| 596 | messageId = com.android.internal.R.string.VideoView_error_text_invalid_progressive_playback; |
| 597 | } else { |
| 598 | messageId = com.android.internal.R.string.VideoView_error_text_unknown; |
| 599 | } |
| 600 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | new AlertDialog.Builder(mContext) |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 602 | .setMessage(messageId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | .setPositiveButton(com.android.internal.R.string.VideoView_error_button, |
| 604 | new DialogInterface.OnClickListener() { |
| 605 | public void onClick(DialogInterface dialog, int whichButton) { |
| 606 | /* If we get here, there is no onError listener, so |
| 607 | * at least inform them that the video is over. |
| 608 | */ |
| 609 | if (mOnCompletionListener != null) { |
| 610 | mOnCompletionListener.onCompletion(mMediaPlayer); |
| 611 | } |
| 612 | } |
| 613 | }) |
| 614 | .setCancelable(false) |
| 615 | .show(); |
| 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | }; |
| 620 | |
| 621 | private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener = |
| 622 | new MediaPlayer.OnBufferingUpdateListener() { |
| 623 | public void onBufferingUpdate(MediaPlayer mp, int percent) { |
| 624 | mCurrentBufferPercentage = percent; |
| 625 | } |
| 626 | }; |
| 627 | |
| 628 | /** |
| 629 | * Register a callback to be invoked when the media file |
| 630 | * is loaded and ready to go. |
| 631 | * |
| 632 | * @param l The callback that will be run |
| 633 | */ |
| 634 | public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) |
| 635 | { |
| 636 | mOnPreparedListener = l; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Register a callback to be invoked when the end of a media file |
| 641 | * has been reached during playback. |
| 642 | * |
| 643 | * @param l The callback that will be run |
| 644 | */ |
| 645 | public void setOnCompletionListener(OnCompletionListener l) |
| 646 | { |
| 647 | mOnCompletionListener = l; |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Register a callback to be invoked when an error occurs |
| 652 | * during playback or setup. If no listener is specified, |
| 653 | * or if the listener returned false, VideoView will inform |
| 654 | * the user of any errors. |
| 655 | * |
| 656 | * @param l The callback that will be run |
| 657 | */ |
| 658 | public void setOnErrorListener(OnErrorListener l) |
| 659 | { |
| 660 | mOnErrorListener = l; |
| 661 | } |
| 662 | |
James Dong | a0ba794 | 2012-07-27 17:30:38 -0700 | [diff] [blame] | 663 | /** |
| 664 | * Register a callback to be invoked when an informational event |
| 665 | * occurs during playback or setup. |
| 666 | * |
| 667 | * @param l The callback that will be run |
| 668 | */ |
| 669 | public void setOnInfoListener(OnInfoListener l) { |
| 670 | mOnInfoListener = l; |
| 671 | } |
| 672 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 673 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() |
| 675 | { |
| 676 | public void surfaceChanged(SurfaceHolder holder, int format, |
| 677 | int w, int h) |
| 678 | { |
| 679 | mSurfaceWidth = w; |
| 680 | mSurfaceHeight = h; |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 681 | boolean isValidState = (mTargetState == STATE_PLAYING); |
| 682 | boolean hasValidSize = (mVideoWidth == w && mVideoHeight == h); |
| 683 | if (mMediaPlayer != null && isValidState && hasValidSize) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | if (mSeekWhenPrepared != 0) { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 685 | seekTo(mSeekWhenPrepared); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | } |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 687 | start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | public void surfaceCreated(SurfaceHolder holder) |
| 692 | { |
| 693 | mSurfaceHolder = holder; |
Andreas Huber | 69b8d69 | 2010-10-29 12:00:20 -0700 | [diff] [blame] | 694 | openVideo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | public void surfaceDestroyed(SurfaceHolder holder) |
| 698 | { |
| 699 | // after we return from this we can't use the surface any more |
| 700 | mSurfaceHolder = null; |
| 701 | if (mMediaController != null) mMediaController.hide(); |
Andreas Huber | 69b8d69 | 2010-10-29 12:00:20 -0700 | [diff] [blame] | 702 | release(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | } |
| 704 | }; |
| 705 | |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 706 | /* |
| 707 | * release the media player in any state |
| 708 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 709 | @UnsupportedAppUsage |
Marco Nelissen | dddeee6 | 2009-07-10 14:14:52 -0700 | [diff] [blame] | 710 | private void release(boolean cleartargetstate) { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 711 | if (mMediaPlayer != null) { |
| 712 | mMediaPlayer.reset(); |
| 713 | mMediaPlayer.release(); |
| 714 | mMediaPlayer = null; |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 715 | mPendingSubtitleTracks.clear(); |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 716 | mCurrentState = STATE_IDLE; |
Marco Nelissen | dddeee6 | 2009-07-10 14:14:52 -0700 | [diff] [blame] | 717 | if (cleartargetstate) { |
| 718 | mTargetState = STATE_IDLE; |
| 719 | } |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 720 | if (mAudioFocusType != AudioManager.AUDIOFOCUS_NONE) { |
| 721 | mAudioManager.abandonAudioFocus(null); |
| 722 | } |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | @Override |
| 727 | public boolean onTouchEvent(MotionEvent ev) { |
Vladislav Kaznacheev | fd48ed6 | 2017-05-02 09:39:06 -0700 | [diff] [blame] | 728 | if (ev.getAction() == MotionEvent.ACTION_DOWN |
| 729 | && isInPlaybackState() && mMediaController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | toggleMediaControlsVisiblity(); |
| 731 | } |
Vladislav Kaznacheev | fd48ed6 | 2017-05-02 09:39:06 -0700 | [diff] [blame] | 732 | return super.onTouchEvent(ev); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 734 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | @Override |
| 736 | public boolean onTrackballEvent(MotionEvent ev) { |
Vladislav Kaznacheev | fd48ed6 | 2017-05-02 09:39:06 -0700 | [diff] [blame] | 737 | if (ev.getAction() == MotionEvent.ACTION_DOWN |
| 738 | && isInPlaybackState() && mMediaController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | toggleMediaControlsVisiblity(); |
| 740 | } |
Vladislav Kaznacheev | fd48ed6 | 2017-05-02 09:39:06 -0700 | [diff] [blame] | 741 | return super.onTrackballEvent(ev); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 743 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | @Override |
| 745 | public boolean onKeyDown(int keyCode, KeyEvent event) |
| 746 | { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 747 | boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK && |
| 748 | keyCode != KeyEvent.KEYCODE_VOLUME_UP && |
| 749 | keyCode != KeyEvent.KEYCODE_VOLUME_DOWN && |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 750 | keyCode != KeyEvent.KEYCODE_VOLUME_MUTE && |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 751 | keyCode != KeyEvent.KEYCODE_MENU && |
| 752 | keyCode != KeyEvent.KEYCODE_CALL && |
| 753 | keyCode != KeyEvent.KEYCODE_ENDCALL; |
| 754 | if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK || |
Andy Stadler | f8a7cea | 2009-04-10 16:24:47 -0700 | [diff] [blame] | 756 | keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | if (mMediaPlayer.isPlaying()) { |
| 758 | pause(); |
| 759 | mMediaController.show(); |
| 760 | } else { |
| 761 | start(); |
| 762 | mMediaController.hide(); |
| 763 | } |
| 764 | return true; |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 765 | } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) { |
Chih-Chung Chang | d7db701 | 2011-02-10 18:11:27 +0800 | [diff] [blame] | 766 | if (!mMediaPlayer.isPlaying()) { |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 767 | start(); |
| 768 | mMediaController.hide(); |
| 769 | } |
| 770 | return true; |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 771 | } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 772 | || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) { |
Chih-Chung Chang | d7db701 | 2011-02-10 18:11:27 +0800 | [diff] [blame] | 773 | if (mMediaPlayer.isPlaying()) { |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 774 | pause(); |
| 775 | mMediaController.show(); |
| 776 | } |
| 777 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | } else { |
| 779 | toggleMediaControlsVisiblity(); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | return super.onKeyDown(keyCode, event); |
| 784 | } |
| 785 | |
| 786 | private void toggleMediaControlsVisiblity() { |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 787 | if (mMediaController.isShowing()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | mMediaController.hide(); |
| 789 | } else { |
| 790 | mMediaController.show(); |
| 791 | } |
| 792 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 793 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 794 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | public void start() { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 796 | if (isInPlaybackState()) { |
| 797 | mMediaPlayer.start(); |
| 798 | mCurrentState = STATE_PLAYING; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | } |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 800 | mTargetState = STATE_PLAYING; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 802 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 803 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | public void pause() { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 805 | if (isInPlaybackState()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | if (mMediaPlayer.isPlaying()) { |
| 807 | mMediaPlayer.pause(); |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 808 | mCurrentState = STATE_PAUSED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | } |
| 810 | } |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 811 | mTargetState = STATE_PAUSED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 813 | |
Gloria Wang | 2e1818a | 2010-02-22 14:49:13 -0800 | [diff] [blame] | 814 | public void suspend() { |
Andreas Huber | 69b8d69 | 2010-10-29 12:00:20 -0700 | [diff] [blame] | 815 | release(false); |
Gloria Wang | 2e1818a | 2010-02-22 14:49:13 -0800 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | public void resume() { |
Andreas Huber | 69b8d69 | 2010-10-29 12:00:20 -0700 | [diff] [blame] | 819 | openVideo(); |
Gloria Wang | 2e1818a | 2010-02-22 14:49:13 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 822 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | public int getDuration() { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 824 | if (isInPlaybackState()) { |
Andreas Huber | 585c07e | 2012-11-27 15:06:56 -0800 | [diff] [blame] | 825 | return mMediaPlayer.getDuration(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | } |
Andreas Huber | 585c07e | 2012-11-27 15:06:56 -0800 | [diff] [blame] | 827 | |
| 828 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 830 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 831 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | public int getCurrentPosition() { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 833 | if (isInPlaybackState()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | return mMediaPlayer.getCurrentPosition(); |
| 835 | } |
| 836 | return 0; |
| 837 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 838 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 839 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | public void seekTo(int msec) { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 841 | if (isInPlaybackState()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | mMediaPlayer.seekTo(msec); |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 843 | mSeekWhenPrepared = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | } else { |
| 845 | mSeekWhenPrepared = msec; |
| 846 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 849 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | public boolean isPlaying() { |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 851 | return isInPlaybackState() && mMediaPlayer.isPlaying(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 853 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 854 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | public int getBufferPercentage() { |
| 856 | if (mMediaPlayer != null) { |
| 857 | return mCurrentBufferPercentage; |
| 858 | } |
| 859 | return 0; |
| 860 | } |
James Dong | fdf3ac6 | 2009-07-06 12:37:45 -0700 | [diff] [blame] | 861 | |
| 862 | private boolean isInPlaybackState() { |
| 863 | return (mMediaPlayer != null && |
| 864 | mCurrentState != STATE_ERROR && |
| 865 | mCurrentState != STATE_IDLE && |
| 866 | mCurrentState != STATE_PREPARING); |
| 867 | } |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 868 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 869 | @Override |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 870 | public boolean canPause() { |
| 871 | return mCanPause; |
| 872 | } |
| 873 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 874 | @Override |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 875 | public boolean canSeekBackward() { |
| 876 | return mCanSeekBack; |
| 877 | } |
| 878 | |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 879 | @Override |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 880 | public boolean canSeekForward() { |
| 881 | return mCanSeekForward; |
| 882 | } |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 883 | |
| 884 | @Override |
| 885 | public int getAudioSessionId() { |
| 886 | if (mAudioSession == 0) { |
| 887 | MediaPlayer foo = new MediaPlayer(); |
| 888 | mAudioSession = foo.getAudioSessionId(); |
| 889 | foo.release(); |
| 890 | } |
| 891 | return mAudioSession; |
| 892 | } |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 893 | |
| 894 | @Override |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 895 | protected void onAttachedToWindow() { |
| 896 | super.onAttachedToWindow(); |
| 897 | |
| 898 | if (mSubtitleWidget != null) { |
| 899 | mSubtitleWidget.onAttachedToWindow(); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | @Override |
| 904 | protected void onDetachedFromWindow() { |
| 905 | super.onDetachedFromWindow(); |
| 906 | |
| 907 | if (mSubtitleWidget != null) { |
| 908 | mSubtitleWidget.onDetachedFromWindow(); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | @Override |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 913 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 914 | super.onLayout(changed, left, top, right, bottom); |
| 915 | |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 916 | if (mSubtitleWidget != null) { |
| 917 | measureAndLayoutSubtitleWidget(); |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | @Override |
| 922 | public void draw(Canvas canvas) { |
| 923 | super.draw(canvas); |
| 924 | |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 925 | if (mSubtitleWidget != null) { |
| 926 | final int saveCount = canvas.save(); |
| 927 | canvas.translate(getPaddingLeft(), getPaddingTop()); |
| 928 | mSubtitleWidget.draw(canvas); |
| 929 | canvas.restoreToCount(saveCount); |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
| 933 | /** |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 934 | * Forces a measurement and layout pass for all overlaid views. |
| 935 | * |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 936 | * @see #setSubtitleWidget(RenderingWidget) |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 937 | */ |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 938 | private void measureAndLayoutSubtitleWidget() { |
| 939 | final int width = getWidth() - getPaddingLeft() - getPaddingRight(); |
| 940 | final int height = getHeight() - getPaddingTop() - getPaddingBottom(); |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 941 | |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 942 | mSubtitleWidget.setSize(width, height); |
Alan Viverette | de213f7 | 2013-09-03 16:17:56 -0700 | [diff] [blame] | 943 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 944 | |
| 945 | /** @hide */ |
| 946 | @Override |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 947 | public void setSubtitleWidget(RenderingWidget subtitleWidget) { |
| 948 | if (mSubtitleWidget == subtitleWidget) { |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 949 | return; |
| 950 | } |
| 951 | |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 952 | final boolean attachedToWindow = isAttachedToWindow(); |
| 953 | if (mSubtitleWidget != null) { |
| 954 | if (attachedToWindow) { |
| 955 | mSubtitleWidget.onDetachedFromWindow(); |
| 956 | } |
Lajos Molnar | 484ff7a9 | 2013-08-15 11:37:47 -0700 | [diff] [blame] | 957 | |
Alan Viverette | d43daf3 | 2013-09-05 16:34:30 -0700 | [diff] [blame] | 958 | mSubtitleWidget.setOnChangedListener(null); |
| 959 | } |
| 960 | |
| 961 | mSubtitleWidget = subtitleWidget; |
| 962 | |
| 963 | if (subtitleWidget != null) { |
| 964 | if (mSubtitlesChangedListener == null) { |
| 965 | mSubtitlesChangedListener = new RenderingWidget.OnChangedListener() { |
| 966 | @Override |
| 967 | public void onChanged(RenderingWidget renderingWidget) { |
| 968 | invalidate(); |
| 969 | } |
| 970 | }; |
| 971 | } |
| 972 | |
| 973 | setWillNotDraw(false); |
| 974 | subtitleWidget.setOnChangedListener(mSubtitlesChangedListener); |
| 975 | |
| 976 | if (attachedToWindow) { |
| 977 | subtitleWidget.onAttachedToWindow(); |
| 978 | requestLayout(); |
| 979 | } |
| 980 | } else { |
| 981 | setWillNotDraw(true); |
| 982 | } |
| 983 | |
| 984 | invalidate(); |
| 985 | } |
Lajos Molnar | 29f5183 | 2013-09-20 08:45:31 -0700 | [diff] [blame] | 986 | |
| 987 | /** @hide */ |
| 988 | @Override |
| 989 | public Looper getSubtitleLooper() { |
| 990 | return Looper.getMainLooper(); |
| 991 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | } |