1/* 2 * Copyright 2018 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 17package androidx.media.widget; 18 19import android.content.Context; 20import android.media.AudioManager; 21import android.net.Uri; 22import android.support.v4.media.session.MediaControllerCompat; 23import android.support.v4.media.session.PlaybackStateCompat; 24import android.util.AttributeSet; 25import android.view.MotionEvent; 26import android.view.View; 27 28import androidx.annotation.NonNull; 29import androidx.annotation.Nullable; 30import androidx.media.AudioAttributesCompat; 31import androidx.media.DataSourceDesc; 32import androidx.media.MediaItem2; 33import androidx.media.MediaMetadata2; 34import androidx.media.SessionToken2; 35 36import java.util.List; 37import java.util.Map; 38import java.util.concurrent.Executor; 39 40/** 41 * Interface for impl classes. 42 */ 43interface VideoView2Impl { 44 void initialize( 45 VideoView2 instance, Context context, 46 @Nullable AttributeSet attrs, int defStyleAttr); 47 48 /** 49 * Sets MediaControlView2 instance. It will replace the previously assigned MediaControlView2 50 * instance if any. 51 * 52 * @param mediaControlView a media control view2 instance. 53 * @param intervalMs a time interval in milliseconds until VideoView2 hides MediaControlView2. 54 */ 55 void setMediaControlView2(MediaControlView2 mediaControlView, long intervalMs); 56 57 /** 58 * Returns MediaControlView2 instance which is currently attached to VideoView2 by default or by 59 * {@link #setMediaControlView2} method. 60 */ 61 MediaControlView2 getMediaControlView2(); 62 63 /** 64 * Sets MediaMetadata2 instance. It will replace the previously assigned MediaMetadata2 instance 65 * if any. 66 * 67 * @param metadata a MediaMetadata2 instance. 68 */ 69 void setMediaMetadata(MediaMetadata2 metadata); 70 71 /** 72 * Returns MediaMetadata2 instance which is retrieved from MediaPlayer inside VideoView2 by 73 * default or by {@link #setMediaMetadata} method. 74 */ 75 MediaMetadata2 getMediaMetadata(); 76 77 /** 78 * Returns MediaController instance which is connected with MediaSession that VideoView2 is 79 * using. This method should be called when VideoView2 is attached to window, or it throws 80 * IllegalStateException, since internal MediaSession instance is not available until 81 * this view is attached to window. Please check {@link View#isAttachedToWindow} 82 * before calling this method. 83 * 84 * @throws IllegalStateException if interal MediaSession is not created yet. 85 */ 86 MediaControllerCompat getMediaController(); 87 88 /** 89 * Returns {@link SessionToken2} so that developers create their own 90 * {@link androidx.media.MediaController2} instance. This method should be called when 91 * VideoView2 is attached to window, or it throws IllegalStateException. 92 * 93 * @throws IllegalStateException if interal MediaSession is not created yet. 94 */ 95 SessionToken2 getMediaSessionToken(); 96 97 /** 98 * Shows or hides closed caption or subtitles if there is any. 99 * The first subtitle track will be chosen if there multiple subtitle tracks exist. 100 * Default behavior of VideoView2 is not showing subtitle. 101 * @param enable shows closed caption or subtitles if this value is true, or hides. 102 */ 103 void setSubtitleEnabled(boolean enable); 104 105 /** 106 * Returns true if showing subtitle feature is enabled or returns false. 107 * Although there is no subtitle track or closed caption, it can return true, if the feature 108 * has been enabled by {@link #setSubtitleEnabled}. 109 */ 110 boolean isSubtitleEnabled(); 111 112 /** 113 * Sets playback speed. 114 * 115 * It is expressed as a multiplicative factor, where normal speed is 1.0f. If it is less than 116 * or equal to zero, it will be just ignored and nothing will be changed. If it exceeds the 117 * maximum speed that internal engine supports, system will determine best handling or it will 118 * be reset to the normal speed 1.0f. 119 * @param speed the playback speed. It should be positive. 120 */ 121 void setSpeed(float speed); 122 123 /** 124 * Returns playback speed. 125 * 126 * It returns the same value that has been set by {@link #setSpeed}, if it was available value. 127 * If {@link #setSpeed} has not been called before, then the normal speed 1.0f will be returned. 128 */ 129 float getSpeed(); 130 131 /** 132 * Sets which type of audio focus will be requested during the playback, or configures playback 133 * to not request audio focus. Valid values for focus requests are 134 * {@link AudioManager#AUDIOFOCUS_GAIN}, {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT}, 135 * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK}, and 136 * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}. Or use 137 * {@link AudioManager#AUDIOFOCUS_NONE} to express that audio focus should not be 138 * requested when playback starts. You can for instance use this when playing a silent animation 139 * through this class, and you don't want to affect other audio applications playing in the 140 * background. 141 * 142 * @param focusGain the type of audio focus gain that will be requested, or 143 * {@link AudioManager#AUDIOFOCUS_NONE} to disable the use audio focus during 144 * playback. 145 */ 146 void setAudioFocusRequest(int focusGain); 147 148 /** 149 * Sets the {@link AudioAttributesCompat} to be used during the playback of the video. 150 * 151 * @param attributes non-null <code>AudioAttributesCompat</code>. 152 */ 153 void setAudioAttributes(@NonNull AudioAttributesCompat attributes); 154 155 /** 156 * Sets video path. 157 * 158 * @param path the path of the video. 159 */ 160 void setVideoPath(String path); 161 162 /** 163 * Sets video URI. 164 * 165 * @param uri the URI of the video. 166 */ 167 void setVideoUri(Uri uri); 168 169 /** 170 * Sets video URI using specific headers. 171 * 172 * @param uri the URI of the video. 173 * @param headers the headers for the URI request. 174 * Note that the cross domain redirection is allowed by default, but that can be 175 * changed with key/value pairs through the headers parameter with 176 * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value 177 * to disallow or allow cross domain redirection. 178 */ 179 void setVideoUri(Uri uri, @Nullable Map<String, String> headers); 180 181 /** 182 * Sets {@link MediaItem2} object to render using VideoView2. Alternative way to set media 183 * object to VideoView2 is {@link #setDataSource}. 184 * @param mediaItem the MediaItem2 to play 185 * @see #setDataSource 186 */ 187 void setMediaItem(@NonNull MediaItem2 mediaItem); 188 189 /** 190 * Sets {@link DataSourceDesc} object to render using VideoView2. 191 * @param dataSource the {@link DataSourceDesc} object to play. 192 * @see #setMediaItem 193 */ 194 void setDataSource(@NonNull DataSourceDesc dataSource); 195 196 /** 197 * Selects which view will be used to render video between SurfacView and TextureView. 198 * 199 * @param viewType the view type to render video 200 * <ul> 201 * <li>{@link #VideoView2.VIEW_TYPE_SURFACEVIEW} 202 * <li>{@link #VideoView2.VIEW_TYPE_TEXTUREVIEW} 203 * </ul> 204 */ 205 void setViewType(@VideoView2.ViewType int viewType); 206 207 /** 208 * Returns view type. 209 * 210 * @return view type. See {@see setViewType}. 211 */ 212 @VideoView2.ViewType 213 int getViewType(); 214 215 /** 216 * Sets custom actions which will be shown as custom buttons in {@link MediaControlView2}. 217 * 218 * @param actionList A list of {@link PlaybackStateCompat.CustomAction}. The return value of 219 * {@link PlaybackStateCompat.CustomAction#getIcon()} will be used to draw 220 * buttons in {@link MediaControlView2}. 221 * @param executor executor to run callbacks on. 222 * @param listener A listener to be called when a custom button is clicked. 223 */ 224 void setCustomActions(List<PlaybackStateCompat.CustomAction> actionList, 225 Executor executor, VideoView2.OnCustomActionListener listener); 226 227 /** 228 * Registers a callback to be invoked when a view type change is done. 229 * {@see #setViewType(int)} 230 * @param l The callback that will be run 231 */ 232 void setOnViewTypeChangedListener(VideoView2.OnViewTypeChangedListener l); 233 234 void onAttachedToWindowImpl(); 235 236 void onDetachedFromWindowImpl(); 237 238 void onTouchEventImpl(MotionEvent ev); 239 240 void onTrackballEventImpl(MotionEvent ev); 241 242 void onMeasureImpl(int widthMeasureSpec, int heightMeasureSpec); 243} 244