In Android, The MediaPlayer class is used for audio playback, but this class does not provide the playback position of the video image. Therefore, the video playing here is slightly different. There are two Playback modes: you can directly call the system's VideoView to play a video or use Mediaplayer and SurfaceView to play a video.
I. video playback using the system's VideoView
1) drag a VideoView control on the main interface.
2) complete the following operations in the main Activity:
◆ Declare the VideoView control and find the ID
1 |
vv = (VideoView) findViewById(R.id.videoView1); |
|
◆ Set the resource path. Here we place the video on the sdCard. Note that you can obtain almost all the sdCard paths of mobile phones.
12 |
// Set the video path
vv.setVideoURI(Uri.fromFile( new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+ "/Video playback path and file name" ))); |
|
◆ Add a System Controller
12 |
// Add the system controller to display the fast forward and return control bars.
vv.setMediaController( new MediaController( this )); |
|
◆ Call the start method to start video playback.
12 |
// Start playing the video vv.start(); |
|
3) result: the system's VideoView is called to play the video.
2. video playback using MediaPlayer and SurfaceView
1: SurfaceView
In general, we place operations such as drawing images in the main UI, so that the main UI not only needs to process drawing operations, but also other user-defined click operations, this seems too bloated. Therefore, SurfaceView is to put such time-consuming operations into another separate thread to complete the process.
He provides a Surface and can control the Surface operations through the SurfaceHolder controller. The getHolder method can be used to obtain the Controller object. The methods to be rewritten are as follows:
1) surfaceCreated method: This method is used when Surfaceview is created for the first time. It mainly completes initialization. Generally, do not complete the painting operation here.
2) surfaceChanged method: This method is triggered when the Surface state changes.
3) surfaceDestroyed method: the State triggered before Surface destruction, used to clear resources.
2: Video Playback
1) drag a surfaceview on the main interface and place the four buttons below to control the playback of the video.
2) obtain the Controller object and set the corresponding attributes.
12 |
// SurfaceView does not manage the buffer, so that the frequency Screen Renderer engine automatically manages the video to the user.
sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
|
3) instantiate a MediaPlayer object
1 |
mediaPlayer = new MediaPlayer(); |
|
4) Implementation of the start Operation
123456789101112131415161718 |
public void start() {
// Reset
mediaPlayer.reset();
try {
// Set video resources
mediaPlayer.setDataSource( this , Uri.fromFile( new File(Environment
.getExternalStorageDirectory().getAbsoluteFile()
+ "/Video path and file name suffix" )));
// Set the video display position, which is on surfaceView
mediaPlayer.setDisplay(sv.getHolder());
// Prepare Resources
mediaPlayer.prepare();
// Start the video
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
} |
|
5) implementation of the stop operation
1 |
mediaPlayer.stop(); // Stop the video |
|
6) pause operation implementation
123456 |
// Whether the video is being played
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause(); // Stop if yes
} else {
mediaPlayer.start(); // Start if it is not
} |
|
7) setPosition operation implementation
1 |
mediaPlayer.seekTo( 5000 ); // Jump to the location of 5000 milliseconds, no matter where you are currently |
|
8) result: the video is played.
SurfaceView is far more useful than this. It can also be used for graphic image processing. We will continue tomorrow .. Remove 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/095642GO-0.gif "style =" padding: 0px; margin: 0px; vertical-align: top; border: none; "/>
This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1294850