A high level view for media playback that can be integrated with either a
MediaController. Developers can easily implement a video rendering application
using this class. By default, a
MediaControlView is attached so the playback
control buttons are displayed on top of VideoView.
- Using VideoView with SessionPlayer or MediaController
- Using VideoView with MediaControlView
- Choosing a view type
- Comparison with android.widget.VideoView
- Displaying Metadata
Using VideoView with SessionPlayer or MediaController
- For simple use cases that do not require communication with a
MediaSession, apps need to create a player instance that extends
MediaPlayer) and link it to this view by calling
- For more advanced use cases that require a
MediaSession(e.g. handling media key events, integrating with other MediaSession apps as Assistant), apps need to create a
MediaControllerthat's attached to the
MediaSessionand link it to this view by calling
Using VideoView with MediaControlView
VideoViewis working with
MediaControlViewand a MediaControlView instance is attached to VideoView by default.
Choosing a view typeVideoView can render videos on a TextureView or SurfaceView. The default is SurfaceView which can be changed by using the
setViewType(int)method or by setting the
R.attr.viewTypeattribute in the layout file.
SurfaceView is recommended in most cases for saving battery life. TextureView might be preferred for supporting various UIs such as animation and translucency.
Comparison with android.widget.VideoViewThese are the main differences between the media2 VideoView widget and the older android widget:
MediaPlayerinstance internally and wraps playback APIs around it.
androidx.media2.widget.VideoViewdoes not create a player instance internally. Instead, either a
MediaControllerinstance should be created externally and link to
android.widget.VideoViewinherits from the SurfaceView class.
androidx.media2.widget.VideoViewinherits from ViewGroup and can render videos using SurfaceView or TextureView, depending on your choice.
VideoViewcan respond to media key events if you call
setMediaController(MediaController)to link it to a
MediaControllerthat's connected to an active
Displaying MetadataWhen you play music only (sound with no video), VideoView can display album art and other metadata by calling
MediaItem.setMetadata(MediaMetadata). The following table shows the metadata displayed by the VideoView, and the default values assigned if the keys are not set:
Note: VideoView does not retain its full state when going into the background. In particular, it
does not save, and does not restore the current play state, play position, selected tracks.
Applications should save and restore these on their own in
Example of attributes for a VideoView with TextureView and no attached control view:
<androidx.media2.widget.VideoView android:id="@+id/video_view" widget:enableControlView="false" widget:viewType="textureView" />
Interface definition of a callback to be invoked when the view type has been changed.
Indicates video is rendering on SurfaceView.
Indicates video is rendering on TextureView.