VideoView2

public class VideoView2
extends ViewGroup

java.lang.Object
   ↳ 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 functionality. 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 custom MediaControlView2, assign the custom media control widget using setMediaControlView2(MediaControlView2, long).
  • 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 Activity.onSaveInstanceState(Bundle) and Activity.onRestoreInstanceState(Bundle).

Summary

XML attributes

VideoView2_enableControlView  
VideoView2_viewType  

Constants

int VIEW_TYPE_SURFACEVIEW

Indicates video is rendering on SurfaceView.

int VIEW_TYPE_TEXTUREVIEW

Indicates video is rendering on TextureView.

Inherited constants

Inherited fields

Public constructors

VideoView2(Context context)
VideoView2(Context context, AttributeSet attrs)
VideoView2(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
boolean dispatchTouchEvent(MotionEvent ev)
ViewGroup.LayoutParams generateDefaultLayoutParams()
ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp)
ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
CharSequence getAccessibilityClassName()
MediaControlView2 getMediaControlView2()

Returns MediaControlView2 instance which is currently attached to VideoView2 by default or by setMediaControlView2(MediaControlView2, long) method.

SessionToken2 getMediaSessionToken2()

Returns SessionToken2 so that developers create their own MediaController2 instance.

float getSpeed()

Returns playback speed.

int getViewType()

Returns view type.

void onAttachedToWindow()
void onDetachedFromWindow()
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
boolean onTouchEvent(MotionEvent ev)
boolean onTrackballEvent(MotionEvent ev)
void setAudioAttributes(AudioAttributesCompat attributes)

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

void setMediaControlView2(MediaControlView2 mediaControlView, long intervalMs)

Sets MediaControlView2 instance.

void setMediaItem2(MediaItem2 mediaItem)

Sets MediaItem2 object to render using VideoView2.

void setSpeed(float speed)

Sets playback speed.

void setViewType(int viewType)

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

boolean shouldDelayChildPressedState()

Protected methods

void onLayout(boolean changed, int left, int top, int right, int bottom)

Inherited methods

XML attributes

VideoView2_enableControlView

VideoView2_viewType

Constants

VIEW_TYPE_SURFACEVIEW

public static final int VIEW_TYPE_SURFACEVIEW

Indicates video is rendering on SurfaceView.

See also:

Constant Value: 0 (0x00000000)

VIEW_TYPE_TEXTUREVIEW

public static final int VIEW_TYPE_TEXTUREVIEW

Indicates video is rendering on TextureView.

See also:

Constant Value: 1 (0x00000001)

Public constructors

VideoView2

public VideoView2 (Context context)

Parameters
context Context

VideoView2

public VideoView2 (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

VideoView2

public VideoView2 (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

checkLayoutParams

public boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

dispatchTouchEvent

public boolean dispatchTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

generateDefaultLayoutParams

public ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns
ViewGroup.LayoutParams

generateLayoutParams

public ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams lp)

Parameters
lp ViewGroup.LayoutParams

Returns
ViewGroup.LayoutParams

generateLayoutParams

public ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
ViewGroup.LayoutParams

getAccessibilityClassName

public CharSequence getAccessibilityClassName ()

Returns
CharSequence

getMediaControlView2

public MediaControlView2 getMediaControlView2 ()

Returns MediaControlView2 instance which is currently attached to VideoView2 by default or by setMediaControlView2(MediaControlView2, long) method.

Returns
MediaControlView2

getMediaSessionToken2

public SessionToken2 getMediaSessionToken2 ()

Returns SessionToken2 so that developers create their own MediaController2 instance. This method should be called when VideoView2 is attached to window, or it throws IllegalStateException.

Returns
SessionToken2

Throws
IllegalStateException if internal MediaSession is not created yet.

getSpeed

public float getSpeed ()

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

Returns
float

getViewType

public int getViewType ()

Returns view type.

Returns
int view type. See .

onAttachedToWindow

public void onAttachedToWindow ()

onDetachedFromWindow

public void onDetachedFromWindow ()

onMeasure

public void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onTouchEvent

public boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

onTrackballEvent

public boolean onTrackballEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

setAudioAttributes

public void setAudioAttributes (AudioAttributesCompat attributes)

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

Parameters
attributes AudioAttributesCompat: non-null AudioAttributesCompat.

setMediaControlView2

public void setMediaControlView2 (MediaControlView2 mediaControlView, 
                long intervalMs)

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

Parameters
mediaControlView MediaControlView2: a media control view2 instance.

intervalMs long: a time interval in milliseconds until VideoView2 hides MediaControlView2.

setMediaItem2

public void setMediaItem2 (MediaItem2 mediaItem)

Sets MediaItem2 object to render using VideoView2.

Parameters
mediaItem MediaItem2: the MediaItem2 to play

setSpeed

public void setSpeed (float speed)

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.

setViewType

public void setViewType (int viewType)

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

Parameters
viewType int: the view type to render video

shouldDelayChildPressedState

public boolean shouldDelayChildPressedState ()

Returns
boolean

Protected methods

onLayout

protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int