belongs to Maven artifact androidx.media:media-widget:1.0.0-alpha1

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 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(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 : By default, VideoView2 requests audio focus with AudioManager.AUDIOFOCUS_GAIN. Use setAudioFocusRequest(int) to change this behavior. The default AudioAttributes used during playback have a usage of AudioAttributes.USAGE_MEDIA and a content type of AudioAttributes.CONTENT_TYPE_MOVIE, use setAudioAttributes(AudioAttributes) 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

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.

float getSpeed()

Returns playback speed.

int getViewType()

Returns view type.

boolean isSubtitleEnabled()

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

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

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

void setAudioFocusRequest(int focusGain)

Sets which type of audio focus will be requested during the playback, or configures playback to not request audio focus.

void setMediaControlView2(MediaControlView2 mediaControlView, long intervalMs)

Sets MediaControlView2 instance.

void setSpeed(float speed)

Sets playback speed.

void setSubtitleEnabled(boolean enable)

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

void setVideoUri(Uri uri, Map<String, String> headers)

Sets video URI using specific headers.

void setViewType(int viewType)

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

boolean shouldDelayChildPressedState()

Protected methods

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

Inherited methods

Constants

VIEW_TYPE_SURFACEVIEW

int VIEW_TYPE_SURFACEVIEW

Indicates video is rendering on SurfaceView.

See also:

Constant Value: 0 (0x00000000)

VIEW_TYPE_TEXTUREVIEW

int VIEW_TYPE_TEXTUREVIEW

Indicates video is rendering on TextureView.

See also:

Constant Value: 1 (0x00000001)

Public constructors

VideoView2

VideoView2 (Context context)

Parameters
context Context

VideoView2

VideoView2 (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

VideoView2

VideoView2 (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

checkLayoutParams

boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

dispatchTouchEvent

boolean dispatchTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

generateDefaultLayoutParams

ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns
ViewGroup.LayoutParams

generateLayoutParams

ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams lp)

Parameters
lp ViewGroup.LayoutParams

Returns
ViewGroup.LayoutParams

generateLayoutParams

ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
ViewGroup.LayoutParams

getAccessibilityClassName

CharSequence getAccessibilityClassName ()

Returns
CharSequence

getMediaControlView2

MediaControlView2 getMediaControlView2 ()

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

Returns
MediaControlView2

getSpeed

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

int getViewType ()

Returns view type.

Returns
int view type. See .

isSubtitleEnabled

boolean isSubtitleEnabled ()

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(boolean).

Returns
boolean

onAttachedToWindow

void onAttachedToWindow ()

onDetachedFromWindow

void onDetachedFromWindow ()

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

onTrackballEvent

boolean onTrackballEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

setAudioAttributes

void setAudioAttributes (AudioAttributes attributes)

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

Parameters
attributes AudioAttributes: non-null AudioAttributes.

setAudioFocusRequest

void setAudioFocusRequest (int focusGain)

Sets which type of audio focus will be requested during the playback, or configures playback to not request audio focus. Valid values for focus requests are AudioManager.AUDIOFOCUS_GAIN, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, and AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE. Or use AudioManager.AUDIOFOCUS_NONE to express that audio focus should not be requested when playback starts. You can for instance use this when playing a silent animation through this class, and you don't want to affect other audio applications playing in the background.

Parameters
focusGain int: the type of audio focus gain that will be requested, or AudioManager.AUDIOFOCUS_NONE to disable the use audio focus during playback.

setMediaControlView2

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.

setSpeed

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.

setSubtitleEnabled

void setSubtitleEnabled (boolean enable)

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.

setVideoUri

void setVideoUri (Uri uri, 
                Map<String, String> headers)

Sets video URI using specific headers.

Parameters
uri Uri: the URI of the video.

headers Map: 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.

setViewType

void setViewType (int viewType)

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

Parameters
viewType int: the view type to render video

shouldDelayChildPressedState

boolean shouldDelayChildPressedState ()

Returns
boolean

Protected methods

onLayout

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

Parameters
changed boolean

left int

top int

right int

bottom int