Android APIs
public class

VideoView

extends SurfaceView
implements MediaController.MediaPlayerControl
java.lang.Object
   ↳ android.view.View
     ↳ android.view.SurfaceView
       ↳ android.widget.VideoView

Class Overview

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

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

Summary

[Expand]
Inherited XML Attributes
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
VideoView(Context context)
VideoView(Context context, AttributeSet attrs)
VideoView(Context context, AttributeSet attrs, int defStyle)
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.
int getAudioSessionId()
Get the audio session id for the player used by this VideoView.
int getBufferPercentage()
int getCurrentPosition()
int getDuration()
boolean isPlaying()
void onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
boolean onKeyDown(int keyCode, KeyEvent event)
Default implementation of KeyEvent.Callback.onKeyDown(): perform press of the view when KEYCODE_DPAD_CENTER or KEYCODE_ENTER is released, if the view is enabled and clickable.
boolean onTouchEvent(MotionEvent ev)
Implement this method to handle touch screen motion 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 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)
void setVideoURI(Uri 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.

[Expand]
Inherited Methods
From class android.view.SurfaceView
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource
From interface android.widget.MediaController.MediaPlayerControl

Public Constructors

public VideoView (Context context)

Added in API level 1

public VideoView (Context context, AttributeSet attrs)

Added in API level 1

public VideoView (Context context, AttributeSet attrs, int defStyle)

Added in API level 1

Public Methods

public void addSubtitleSource (InputStream is, MediaFormat format)

Added in API level 19

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 MEDIA_INFO_METADATA_UPDATE info message. Otherwise, if reading the source takes excessive time, one will receive a 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 MEDIA_INFO_UNSUPPORTED_SUBTITLE message. One can find the total number of available tracks using getTrackInfo() to see what additional tracks become available after this method call.

Parameters
is input stream containing the subtitle data. It will be closed by the media framework.
format the format of the subtitle track(s). Must contain at least the mime type (KEY_MIME) and the language (KEY_LANGUAGE) of the file. If the file itself contains the language information, specify "und" for the language.

public boolean canPause ()

Added in API level 5

public boolean canSeekBackward ()

Added in API level 5

public boolean canSeekForward ()

Added in API level 5

public void draw (Canvas canvas)

Added in API level 1

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.

Parameters
canvas The Canvas to which the View is rendered.

public int getAudioSessionId ()

Added in API level 18

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
  • The audio session, or 0 if there was an error.

public int getBufferPercentage ()

Added in API level 1

public int getCurrentPosition ()

Added in API level 1

public int getDuration ()

Added in API level 1

public boolean isPlaying ()

Added in API level 1

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The event to initialize.

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Added in API level 14

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info The instance to initialize.

public boolean onKeyDown (int keyCode, KeyEvent event)

Added in API level 1

Default implementation of KeyEvent.Callback.onKeyDown(): perform press of the view when KEYCODE_DPAD_CENTER or 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 A key code that represents the button pressed, from KeyEvent.
event The KeyEvent object that defines the button action.
Returns
  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public boolean onTouchEvent (MotionEvent ev)

Added in API level 1

Implement this method to handle touch screen motion events.

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 The motion event.
Returns
  • True if the event was handled, false otherwise.

public boolean onTrackballEvent (MotionEvent ev)

Added in API level 1

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 The motion event.
Returns
  • True if the event was handled, false otherwise.

public void pause ()

Added in API level 1

public int resolveAdjustedSize (int desiredSize, int measureSpec)

Added in API level 1

public void resume ()

Added in API level 8

public void seekTo (int msec)

Added in API level 1

public void setMediaController (MediaController controller)

Added in API level 1

public void setOnCompletionListener (MediaPlayer.OnCompletionListener l)

Added in API level 1

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

Parameters
l The callback that will be run

public void setOnErrorListener (MediaPlayer.OnErrorListener l)

Added in API level 1

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 The callback that will be run

public void setOnInfoListener (MediaPlayer.OnInfoListener l)

Added in API level 17

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

Parameters
l The callback that will be run

public void setOnPreparedListener (MediaPlayer.OnPreparedListener l)

Added in API level 1

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

Parameters
l The callback that will be run

public void setVideoPath (String path)

Added in API level 1

public void setVideoURI (Uri uri)

Added in API level 1

public void start ()

Added in API level 1

public void stopPlayback ()

Added in API level 1

public void suspend ()

Added in API level 8

Protected Methods

protected void onAttachedToWindow ()

Added in API level 1

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

protected void onDetachedFromWindow ()

Added in API level 1

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.

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

Added in API level 1

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 This is a new size or position for this view
left Left position, relative to parent
top Top position, relative to parent
right Right position, relative to parent
bottom Bottom position, relative to parent

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Added in API level 1

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 overriden 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 horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.