VideoView

open class VideoView : SelectiveLayout
androidx.media2.widget.VideoView

A high level view for media playbacks that can be integrated with either a SessionPlayer or a MediaController. Developers can easily implement a video rendering application using this class.

For simple use cases not requiring communication with MediaSession, apps need to create a SessionPlayer (e.g. androidx.media2.player.MediaPlayer) and set it to this view by calling setPlayer. For more advanced use cases that require MediaSession (e.g. handling media key events, integrating with other MediaSession apps as Assistant), apps need to create a MediaController attached to the MediaSession and set it to this view by calling setMediaController.

View type can be selected : VideoView can render videos on top of TextureView as well as SurfaceView selectively. The default is SurfaceView and it can be changed using setViewType(int) method. Using SurfaceView is recommended in most cases for saving battery. TextureView might be preferred for supporting various UIs such as animation and translucency.

Differences between android.widget.VideoView class : VideoView covers and inherits the most of android.widget.VideoView's functionality. The main differences are

Displaying metadata : VideoView supports displaying metadata for music by calling MediaItem#setMetadata(MediaMetadata). Currently supported metadata are MediaMetadata#METADATA_KEY_TITLE, MediaMetadata#METADATA_KEY_ARTIST, and MediaMetadata#METADATA_KEY_ALBUM_ART. If values for these keys are not set, the following default values will be shown, respectively: androidx.media2.widget.R.string#mcv2_music_title_unknown_text androidx.media2.widget.R.string#mcv2_music_artist_unknown_text androidx.media2.widget.R.drawable#ic_default_album_image

Note: VideoView does not retain its full state when going into the background. In particular, it 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. androidx.media2.widget.R.attr#enableControlView androidx.media2.widget.R.attr#viewType

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)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

Public methods

open CharSequence!

open MediaControlView?

Returns MediaControlView instance which is currently attached to VideoView by default or by setMediaControlView method.

open Int

Returns view type.

open Unit
setMediaControlView(@NonNull mediaControlView: MediaControlView, intervalMs: Long)

Sets MediaControlView instance.

open Unit
setMediaController(@NonNull controller: MediaController)

Sets MediaController to display media content.

open Unit

Sets a listener to be called when a view type change is done.

open Unit
setPlayer(@NonNull player: SessionPlayer)

Sets SessionPlayer to display media content.

open Unit
setViewType(viewType: Int)

Selects which view will be used to render video between SurfaceView and TextureView.

Protected methods

open Unit

open Unit

Constants

VIEW_TYPE_SURFACEVIEW

static val VIEW_TYPE_SURFACEVIEW: Int

Indicates video is rendering on SurfaceView.

Value: 0

See Also

VIEW_TYPE_TEXTUREVIEW

static val VIEW_TYPE_TEXTUREVIEW: Int

Indicates video is rendering on TextureView.

Value: 1

See Also

Public constructors

<init>

VideoView(@NonNull context: Context)

<init>

VideoView(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>

VideoView(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

Public methods

getAccessibilityClassName

open fun getAccessibilityClassName(): CharSequence!

getMediaControlView

@Nullable open fun getMediaControlView(): MediaControlView?

Returns MediaControlView instance which is currently attached to VideoView by default or by setMediaControlView method.

getViewType

open fun getViewType(): Int

Returns view type.

Return
Int: view type. See {@see setViewType}.

setMediaControlView

open fun setMediaControlView(@NonNull mediaControlView: MediaControlView, intervalMs: Long): Unit

Sets MediaControlView instance. It will replace the previously assigned MediaControlView instance if any.

If a MediaController or a SessionPlayer instance has been set to VideoView, the same instance will be set to MediaControlView.

Parameters
mediaControlView MediaControlView: a MediaControlView instance.
intervalMs MediaControlView: a time interval in milliseconds until VideoView hides MediaControlView.

setMediaController

open fun setMediaController(@NonNull controller: MediaController): Unit

Sets MediaController to display media content. Setting a MediaController will unset any MediaController or SessionPlayer that was previously set.

If VideoView has a MediaControlView instance, this controller will also be set to it.

Parameters
controller MediaController: the controller

See Also

setOnViewTypeChangedListener

open fun setOnViewTypeChangedListener(@Nullable listener: VideoView.OnViewTypeChangedListener?): Unit

Sets a listener to be called when a view type change is done.

Parameters
listener VideoView.OnViewTypeChangedListener?: The listener to be called. A value of null removes any existing listener.

setPlayer

open fun setPlayer(@NonNull player: SessionPlayer): Unit

Sets SessionPlayer to display media content. Setting a SessionPlayer will unset any MediaController or SessionPlayer that was previously set.

If VideoView has a MediaControlView instance, this player will also be set to it.

Parameters
player SessionPlayer: the player

setViewType

open fun setViewType(viewType: Int): Unit

Selects which view will be used to render video between SurfaceView and TextureView.

Note: There are two known issues on API level 28+ devices.

  • When changing view type to SurfaceView from TextureView in "paused" playback state, a blank screen can be shown.
  • When changing view type to TextureView from SurfaceView repeatedly in "paused" playback state, the lastly rendered frame on TextureView can be shown.
Parameters
viewType Int: the view type to render video

Protected methods

onAttachedToWindow

protected open fun onAttachedToWindow(): Unit

onDetachedFromWindow

protected open fun onDetachedFromWindow(): Unit