O Google tem o compromisso de promover a igualdade racial para as comunidades negras. Saiba como.

VideoView

public class VideoView
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ 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 MediaControlView is attached so the playback control buttons are displayed on top of VideoView.

Contents:

  1. Using VideoView with SessionPlayer or MediaController
  2. Using VideoView with MediaControlView
  3. Choosing a view type
  4. Comparison with android.widget.VideoView
  5. Displaying Metadata

Using VideoView with SessionPlayer or MediaController

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(MediaControlView, long).

If you don't want to use MediaControlView, set the VideoView attribute 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 the setViewType(int) method or by setting the 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:

Displaying Metadata

When you play music only (sound with no video), VideoView can display album art and other metadata by calling MediaItem.setMetadata(MediaMetadata). The following table shows the metadata displayed by the VideoView, and the default values assigned if the keys are not set:
KeyDefault
MediaMetadata.METADATA_KEY_TITLE R.string.mcv2_music_title_unknown_text
MediaMetadata.METADATA_KEY_ARTIST R.string.mcv2_music_artist_unknown_text
MediaMetadata.METADATA_KEY_ALBUM_ART R.drawable.media2_widget_ic_default_album_image

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 Activity.onSaveInstanceState(Bundle) and Activity.onRestoreInstanceState(Bundle).

Attributes :

Example of attributes for a VideoView with TextureView and no attached control view:

 <androidx.media2.widget.VideoView
      android:id="@+id/video_view"
      widget:enableControlView="false"
      widget:viewType="textureView"
  />

Summary

Nested classes

interface VideoView.OnViewTypeChangedListener

Interface definition of a callback to be invoked when the view type has been changed. 

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

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

Public methods

ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
CharSequence getAccessibilityClassName()
MediaControlView getMediaControlView()

Returns MediaControlView instance which is currently attached to VideoView by default or by setMediaControlView(MediaControlView, long) method.

int getViewType()

Returns view type.

void onVisibilityAggregated(boolean isVisible)
void setMediaControlView(MediaControlView mediaControlView, long intervalMs)

Sets MediaControlView instance.

void setMediaController(MediaController controller)

Sets MediaController to display media content.

void setOnViewTypeChangedListener(VideoView.OnViewTypeChangedListener listener)

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

void setPlayer(SessionPlayer player)

Sets SessionPlayer to display media content.

void setViewType(int viewType)

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

boolean shouldDelayChildPressedState()

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
ViewGroup.LayoutParams generateDefaultLayoutParams()
ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp)
void onAttachedToWindow()
void onDetachedFromWindow()
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onVisibilityChanged(View view, int visibility)
void onWindowVisibilityChanged(int visibility)

Inherited methods

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

VideoView

public VideoView (Context context)

Parameters
context Context

VideoView

public VideoView (Context context, 
                AttributeSet attrs)

Parameters
context<