Playing Video Using the Video View

The simplest way to play back video is to use the VideoView control. The Video View includes a Surface on which the video is displayed and encapsulates and manages a Media Player to manage the video playback.

The Video View supports the playback of local or streaming video as supported by the Media Player component.

Video Views conveniently encapsulate the initialization of the Media Player. To assign a video to play, simply call setVideoPath or setVideoUri to specify the path to a local file, or the URI of a Content Provider or remote video stream:

streamingVideoView.setVideoUri("http://www.mysite.com/videos/myvideo.3gp"); localVideoView.setVideoPath("/sdcard/test2.3gp");

Once initialized, you can control playback using the start, stopPlayback, pause, and seekTo methods. The Video View also includes the setKeepScreenOn method to apply a screen Wake Lock that will prevent the screen from being dimmed while playback is in progress.

Listing 11-3 shows the simple skeleton code used to assign a video to a Video View and control playback.

LISTING 11-3: Video playback using a Video View

VideoView videoView = (VideoView)findViewById(R.id.surface); videoView.setKeepScreenOn(true); videoView.setVideoPath("/sdcard/test2.3gp"); if (videoView.canSeekForward())

videoView.seekTo(videoView.getDuration()/2); videoView.start(); [ ... do something ... ] videoView.stopPlayback();

0 0

Post a comment

  • Receive news updates via email from this site