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();

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment