Android video playback

Source: Internet
Author: User

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 pathvv.setVideoURI(Uri.fromFile(newFile(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(newMediaController(this));

◆ Call the start method to start video playback.

12 // Start playing the videovv.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 = newMediaPlayer();

4) Implementation of the start Operation

123456789101112131415161718 publicvoidstart() {// ResetmediaPlayer.reset();try{// Set video resourcesmediaPlayer.setDataSource(this, Uri.fromFile(newFile(Environment.getExternalStorageDirectory().getAbsoluteFile()+ "/Video path and file name suffix")));// Set the video display position, which is on surfaceViewmediaPlayer.setDisplay(sv.getHolder());// Prepare ResourcesmediaPlayer.prepare();// Start the videomediaPlayer.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 playedif(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.