VideoView
open class VideoView : SelectiveLayout
androidx.media2.widget.VideoView |
A high level view for media playback that can be integrated with either a SessionPlayer
or a MediaController
. Developers can easily implement a video rendering application using this class. By default, a is attached so the playback control buttons are displayed on top of VideoView.
Contents:
- 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 extendsSessionPlayer
(e.g.androidx.media2.player.MediaPlayer
) and link it to this view by callingsetPlayer
. - 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 aMediaController
that's attached to theMediaSession
and link it to this view by callingsetMediaController
.
Using VideoView with MediaControlView
VideoView
is working with MediaControlView
and a MediaControlView instance is attached to VideoView by default.
If you want to attach a custom MediaControlView
, assign the custom media control widget using setMediaControlView
.
If you don't want to use MediaControlView
, set the VideoView attribute androidx.media2.widget.R.attr#enableControlView
to false.
Choosing a view type
VideoView can render videos on a TextureView or SurfaceView. The default is SurfaceView which can be changed by using thesetViewType(int)
method or by setting the androidx.media2.widget.R.attr#viewType
attribute 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.VideoView
These are the main differences between the media2 VideoView widget and the older android widget:-
android.widget.VideoView
creates aandroid.media.MediaPlayer
instance internally and wraps playback APIs around it.androidx.media2.widget.VideoView
does not create a player instance internally. Instead, either aSessionPlayer
or aMediaController
instance should be created externally and link toVideoView
usingsetPlayer(SessionPlayer)
orsetMediaController(MediaController)
, respectively. -
android.widget.VideoView
inherits from the SurfaceView class.androidx.media2.widget.VideoView
inherits from ViewGroup and can render videos using SurfaceView or TextureView, depending on your choice. - A
VideoView
can respond to media key events if you callsetMediaController
to link it to aMediaController
that's connected to an activeMediaSession
.
Displaying Metadata
When you play music only (sound with no video), VideoView can display album art and other metadata by callingMediaItem#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 android.app.Activity#onSaveInstanceState and android.app.Activity#onRestoreInstanceState.
Attributes :
Example of attributes for a VideoView with TextureView and no attached control view:
<code><androidx.media2.widget.VideoView android:id="@+id/video_view" widget:enableControlView="false" widget:viewType="textureView" /></code>
Summary
Nested classes | |
---|---|
abstract |
Interface definition of a callback to be invoked when the view type has been changed. |
Constants | |
---|---|
static Int |
Indicates video is rendering on SurfaceView. |
static Int |
Indicates video is rendering on TextureView. |
Public constructors | |
---|---|
<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?) |
|
<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int) |
Public methods | |
---|---|
open CharSequence! | |
open MediaControlView? |
Returns |
open Int |
Returns view type. |
open Unit |
setMediaControlView(@NonNull mediaControlView: MediaControlView, intervalMs: Long) Sets |
open Unit |
setMediaController(@NonNull controller: MediaController) Sets |
open Unit |
setOnViewTypeChangedListener(@Nullable listener: VideoView.OnViewTypeChangedListener?) Sets a listener to be called when a view type change is done. |
open Unit |
setPlayer(@NonNull player: SessionPlayer) Sets |
open Unit |
setViewType(viewType: Int) Selects which view will be used to render video between SurfaceView and TextureView. |
Protected methods | |
---|---|
open Unit | |
open Unit |