VideoView2

open class VideoView2 : ViewGroup
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ androidx.media.widget.VideoView2

Displays a video file. VideoView2 class is a ViewGroup class which is wrapping MediaPlayer so that developers can easily implement a video rendering application.

Data sources that VideoView2 supports : VideoView2 can play video files and audio-only files as well. It can load from various sources such as resources or content providers. The supported media file formats are the same as MediaPlayer.

View type can be selected : VideoView2 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 VideoView class : VideoView2 covers and inherits the most of VideoView's functionalities. The main differences are

  • VideoView2 inherits ViewGroup and renders videos using SurfaceView and TextureView selectively while VideoView inherits SurfaceView class.
  • VideoView2 is integrated with MediaControlView2 and a default MediaControlView2 instance is attached to VideoView2 by default.
  • If a developer wants to attach a customed MediaControlView2, assign the customed media control widget using #setMediaControlView2.
  • VideoView2 is integrated with MediaSession and so it responses with media key events. A VideoView2 keeps a MediaSession instance internally and connects it to a corresponding MediaControlView2 instance.

Audio focus and audio attributes : VideoView2 requests audio focus with AudioManager#AUDIOFOCUS_GAIN internally, when playing a media content. The default AudioAttributesCompat used during playback have a usage of AudioAttributesCompat#USAGE_MEDIA and a content type of AudioAttributesCompat#CONTENT_TYPE_MOVIE, use #setAudioAttributes(AudioAttributesCompat) to modify them.

Note: VideoView2 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.

Summary

Constants
static Int

Indicates video is rendering on SurfaceView.

static Int

Indicates video is rendering on TextureView.

Public constructors
<init>(context: Context)

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

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

Public methods
open Unit

open Float

Returns playback speed.

open Boolean

open Int

Returns view type.

open Unit
setMediaControlView2(mediaControlView: MediaControlView2!, intervalMs: Long)

Sets MediaControlView2 instance.

open Boolean

open Unit
setSpeed(speed: Float)

Sets playback speed.

open CharSequence!

open Boolean

open Unit

open MediaControlView2!

Returns MediaControlView2 instance which is currently attached to VideoView2 by default or by #setMediaControlView2 method.

open LayoutParams!

open LayoutParams!

open Unit

Shows or hides closed caption or subtitles if there is any.

open Boolean

open Unit
setAudioAttributes(attributes: AudioAttributesCompat)

Sets the AudioAttributesCompat to be used during the playback of the video.

open Boolean

Returns true if showing subtitle feature is enabled or returns false.

open Unit
setViewType(viewType: Int)

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

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

open Boolean

open LayoutParams!

open Unit
setVideoUri(uri: Uri!, headers: MutableMap<String!, String!>?)

Sets video URI using specific headers.

Protected methods
open Unit
onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int)

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>

VideoView2(context: Context)

<init>

VideoView2(context: Context, attrs: AttributeSet?)

<init>

VideoView2(context: Context, attrs: AttributeSet?, defStyleAttr: Int)

Public methods

onAttachedToWindow

open fun onAttachedToWindow(): Unit

getSpeed

open fun getSpeed(): Float

Returns playback speed. It returns the same value that has been set by #setSpeed, if it was available value. If #setSpeed has not been called before, then the normal speed 1.0f will be returned.

onTouchEvent

open fun onTouchEvent(ev: MotionEvent!): Boolean

getViewType

open fun getViewType(): Int

Returns view type.

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

setMediaControlView2

open fun setMediaControlView2(mediaControlView: MediaControlView2!, intervalMs: Long): Unit

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

Parameters
mediaControlView MediaControlView2!: a media control view2 instance.
intervalMs MediaControlView2!: a time interval in milliseconds until VideoView2 hides MediaControlView2.

checkLayoutParams

open fun checkLayoutParams(p: LayoutParams!): Boolean

setSpeed

open fun setSpeed(speed: Float): Unit

Sets playback speed. It is expressed as a multiplicative factor, where normal speed is 1.0f. If it is less than or equal to zero, it will be just ignored and nothing will be changed. If it exceeds the maximum speed that internal engine supports, system will determine best handling or it will be reset to the normal speed 1.0f.

Parameters
speed Float: the playback speed. It should be positive.

getAccessibilityClassName

open fun getAccessibilityClassName(): CharSequence!

onTrackballEvent

open fun onTrackballEvent(ev: MotionEvent!): Boolean

onDetachedFromWindow

open fun onDetachedFromWindow(): Unit

getMediaControlView2

open fun getMediaControlView2(): MediaControlView2!

Returns MediaControlView2 instance which is currently attached to VideoView2 by default or by #setMediaControlView2 method.

generateLayoutParams

open fun generateLayoutParams(attrs: AttributeSet!): LayoutParams!

generateLayoutParams

open fun generateLayoutParams(lp: LayoutParams!): LayoutParams!

setSubtitleEnabled

open fun setSubtitleEnabled(enable: Boolean): Unit

Shows or hides closed caption or subtitles if there is any. The first subtitle track will be chosen if there multiple subtitle tracks exist. Default behavior of VideoView2 is not showing subtitle.

Parameters
enable Boolean: shows closed caption or subtitles if this value is true, or hides.

shouldDelayChildPressedState

open fun shouldDelayChildPressedState(): Boolean

setAudioAttributes

open fun setAudioAttributes(attributes: AudioAttributesCompat): Unit

Sets the AudioAttributesCompat to be used during the playback of the video.

Parameters
attributes AudioAttributesCompat: non-null AudioAttributesCompat.

isSubtitleEnabled

open fun isSubtitleEnabled(): Boolean

Returns true if showing subtitle feature is enabled or returns false. Although there is no subtitle track or closed caption, it can return true, if the feature has been enabled by #setSubtitleEnabled.

setViewType

open fun setViewType(viewType: Int): Unit

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

Parameters
viewType Int: the view type to render video

onMeasure

open fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit

dispatchTouchEvent

open fun dispatchTouchEvent(ev: MotionEvent!): Boolean

generateDefaultLayoutParams

open fun generateDefaultLayoutParams(): LayoutParams!

setVideoUri

open fun setVideoUri(uri: Uri!, headers: MutableMap<String!, String!>?): Unit

Sets video URI using specific headers.

Parameters
uri Uri!: the URI of the video.
headers Uri!: the headers for the URI request. Note that the cross domain redirection is allowed by default, but that can be changed with key/value pairs through the headers parameter with "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value to disallow or allow cross domain redirection.

Protected methods

onLayout

protected open fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int): Unit