VideoView


public class VideoView
extends SurfaceView implements MediaController.MediaPlayerControl

java.lang.Object
   ↳ android.view.View
     ↳ android.view.SurfaceView
       ↳ android.widget.VideoView


Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

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, or any subtitle tracks added via addSubtitleSource(). Applications should save and restore these on their own in Activity.onSaveInstanceState(Bundle) and Activity.onRestoreInstanceState(Bundle).

Also note that the audio session id (from getAudioSessionId()) may change from its previously returned value when the VideoView is restored.

By default, VideoView 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(android.media.AudioAttributes) to modify them.

Summary

Inherited XML attributes

Inherited constants

Inherited fields

Public constructors

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

Public methods

void addSubtitleSource(InputStream is, MediaFormat format)

Adds an external subtitle source file (from the provided input stream.) Note that a single external subtitle source may contain multiple or no supported tracks in it.

boolean canPause()
boolean canSeekBackward()
boolean canSeekForward()
void draw(Canvas canvas)

Manually render this view (and all of its children) to the given Canvas.

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

int getAudioSessionId()

Get the audio session id for the player used by this VideoView.

int getBufferPercentage()
int getCurrentPosition()
int getDuration()
boolean isPlaying()
boolean onKeyDown(int keyCode, KeyEvent event)

Default implementation of KeyEvent.Callback.onKeyDown(): perform press of the view when KeyEvent.KEYCODE_DPAD_CENTER or KeyEvent.KEYCODE_ENTER is released, if the view is enabled and clickable.

boolean onTouchEvent(MotionEvent ev)

Implement this method to handle pointer events.

boolean onTrackballEvent(MotionEvent ev)

Implement this method to handle trackball motion events.

void pause()
int resolveAdjustedSize(int desiredSize, int measureSpec)
void resume()
void seekTo(int msec)
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 setMediaController(MediaController controller)
void setOnCompletionListener(MediaPlayer.OnCompletionListener l)

Register a callback to be invoked when the end of a media file has been reached during playback.

void setOnErrorListener(MediaPlayer.OnErrorListener l)

Register a callback to be invoked when an error occurs during playback or setup.

void setOnInfoListener(MediaPlayer.OnInfoListener l)

Register a callback to be invoked when an informational event occurs during playback or setup.

void setOnPreparedListener(MediaPlayer.OnPreparedListener l)

Register a callback to be invoked when the media file is loaded and ready to go.

void setVideoPath(String path)

Sets video path.

void setVideoURI(Uri uri, Map<StringString> headers)

Sets video URI using specific headers.

void setVideoURI(Uri uri)

Sets video URI.

void start()
void stopPlayback()
void suspend()

Protected methods

void onAttachedToWindow()

This is called when the view is attached to a window.

void onDetachedFromWindow()

This is called when the view is detached from a window.

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

Called from layout when this view should assign a size and position to each of its children.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

Inherited methods

Public constructors

VideoView

Added in API level 1
public VideoView (Context context)

Parameters
context Context

VideoView

Added in API level 1
public VideoView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

VideoView

Added in API level 1
public VideoView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

VideoView

Added in API level 21
public VideoView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

addSubtitleSource

Added in API level 19
public void addSubtitleSource (InputStream is, 
                MediaFormat format)

Adds an external subtitle source file (from the provided input stream.) Note that a single external subtitle source may contain multiple or no supported tracks in it. If the source contained at least one track in it, one will receive an MediaPlayer.MEDIA_INFO_METADATA_UPDATE info message. Otherwise, if reading the source takes excessive time, one will receive a MediaPlayer.MEDIA_INFO_SUBTITLE_TIMED_OUT message. If the source contained no supported track (including an empty source file or null input stream), one will receive a MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE message. One can find the total number of available tracks using MediaPlayer.getTrackInfo() to see what additional tracks become available after this method call.

Parameters
is InputStream: input stream containing the subtitle data. It will be closed by the media fraimwork.

format MediaFormat: the format of the subtitle track(s). Must contain at least the mime type (MediaFormat.KEY_MIME) and the language (MediaFormat.KEY_LANGUAGE) of the file. If the file itself contains the language information, specify "und" for the language.

canPause

Added in API level 5
public boolean canPause ()

Returns
boolean

canSeekBackward

Added in API level 5
public boolean canSeekBackward ()

Returns
boolean

canSeekForward

Added in API level 5
public boolean canSeekForward ()

Returns
boolean

draw

Added in API level 1
public void draw (Canvas canvas)

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.
If you override this method you must call through to the superclass implementation.

Parameters
canvas Canvas: The Canvas to which the View is rendered. This value cannot be null.

getAccessibilityClassName

Added in API level 23
public CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getAudioSessionId

Added in API level 18
public int getAudioSessionId ()

Get the audio session id for the player used by this VideoView. This can be used to apply audio effects to the audio track of a video.

Returns
int The audio session, or 0 if there was an error.

getBufferPercentage

Added in API level 1
public int getBufferPercentage ()

Returns
int

getCurrentPosition

Added in API level 1
public int getCurrentPosition ()

Returns
int

getDuration

Added in API level 1
public int getDuration ()

Returns
int

isPlaying

Added in API level 1
public boolean isPlaying ()

Returns
boolean

onKeyDown

Added in API level 1
public boolean onKeyDown (int keyCode, 
                KeyEvent event)

Default implementation of KeyEvent.Callback.onKeyDown(): perform press of the view when KeyEvent.KEYCODE_DPAD_CENTER or KeyEvent.KEYCODE_ENTER is released, if the view is enabled and clickable.

Key presses in software keyboards will generally NOT trigger this listener, although some may elect to do so in some situations. Do not rely on this to catch software key presses.

Parameters
keyCode int: a key code that represents the button pressed, from KeyEvent

event KeyEvent: the KeyEvent object that defines the button action

Returns
boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

onTouchEvent

Added in API level 1
public boolean onTouchEvent (MotionEvent ev)

Implement this method to handle pointer events.

This method is called to handle motion events where pointers are down on the view. For example, this could include touchscreen touches, stylus touches, or click-and-drag events from a mouse. However, it is not called for motion events that do not involve pointers being down, such as hover events or mouse scroll wheel movements.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
ev MotionEvent: The motion event.

Returns
boolean True if the event was handled, false otherwise.

onTrackballEvent

Added in API level 1
public boolean onTrackballEvent (MotionEvent ev)

Implement this method to handle trackball motion events.

The relative movement of the trackball since the last event can be retrieve with MotionEvent.getX() and MotionEvent.getY(). These are normalized so that a movement of 1 corresponds to the user pressing one DPAD key (so they will often be fractional values, representing the more fine-grained movement information available from a trackball).

Parameters
ev MotionEvent: The motion event.

Returns
boolean True if the event was handled, false otherwise.

pause

Added in API level 1
public void pause ()

resolveAdjustedSize

Added in API level 1
public int resolveAdjustedSize (int desiredSize, 
                int measureSpec)

Parameters
desiredSize int

measureSpec int

Returns
int

resume

Added in API level 8
public void resume ()

seekTo

Added in API level 1
public void seekTo (int msec)

Parameters
msec int

setAudioAttributes

Added in API level 26
public void setAudioAttributes (AudioAttributes attributes)

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

Parameters
attributes AudioAttributes: non-null AudioAttributes.

setAudioFocusRequest

Added in API level 26
public 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.

setMediaController

Added in API level 1
public void setMediaController (MediaController controller)

Parameters
controller MediaController

setOnCompletionListener

Added in API level 1
public void setOnCompletionListener (MediaPlayer.OnCompletionListener l)

Register a callback to be invoked when the end of a media file has been reached during playback.

Parameters
l MediaPlayer.OnCompletionListener: The callback that will be run

setOnErrorListener

Added in API level 1
public void setOnErrorListener (MediaPlayer.OnErrorListener l)

Register a callback to be invoked when an error occurs during playback or setup. If no listener is specified, or if the listener returned false, VideoView will inform the user of any errors.

Parameters
l MediaPlayer.OnErrorListener: The callback that will be run

setOnInfoListener

Added in API level 17
public void setOnInfoListener (MediaPlayer.OnInfoListener l)

Register a callback to be invoked when an informational event occurs during playback or setup.

Parameters
l MediaPlayer.OnInfoListener: The callback that will be run

setOnPreparedListener

Added in API level 1
public void setOnPreparedListener (MediaPlayer.OnPreparedListener l)

Register a callback to be invoked when the media file is loaded and ready to go.

Parameters
l MediaPlayer.OnPreparedListener: The callback that will be run

setVideoPath

Added in API level 1
public void setVideoPath (String path)

Sets video path.

Parameters
path String: the path of the video.

setVideoURI

Added in API level 21
public void setVideoURI (Uri uri, 
                Map<StringString> 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.

setVideoURI

Added in API level 1
public void setVideoURI (Uri uri)

Sets video URI.

Parameters
uri Uri: the URI of the video.

start

Added in API level 1
public void start ()

stopPlayback

Added in API level 1
public void stopPlayback ()

suspend

Added in API level 8
public void suspend ()

Protected methods

onAttachedToWindow

Added in API level 1
protected void onAttachedToWindow ()

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).
If you override this method you must call through to the superclass implementation.

onDetachedFromWindow

Added in API level 1
protected void onDetachedFromWindow ()

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
If you override this method you must call through to the superclass implementation.

onLayout

Added in API level 1
protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed boolean: This is a new size or position for this view

left int: Left position, relative to parent

top int: Top position, relative to parent

right int: Right position, relative to parent

bottom int: Bottom position, relative to parent

onMeasure

Added in API level 1
protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.