MediaPlayer and SurfaceView for Android Development

Source: Internet
Author: User

MediaPlayer and SurfaceView for Android Development

You can use MediaPlayer to play audio and video files. However, when you use MediaPlayer to play a video, the image output interface is not provided. You can use the SurfaceView component to display video images. Using MediaPlayer and SurfaceView to play a video can be roughly divided into the following four steps:

1) define the SurfaceView component. It is best to implement it in the layout file.

2) create a MediaPlayer object and load the video to be played.

3) output the video to SurfaceView. You can use the setDisplay method of the MediaPlayer object to output the video image to SurfaceView.

4) Call the corresponding method of the MediaPlayer object to control video playback. Play, pause, pause, stop.

An example is as follows:


Import java. io. IOException; import android. app. activity; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android. OS. bundle; import android. view. surfaceView; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private MediaPlayer mp; // declare MediaPla Yer object private SurfaceView sv; // declare SurfaceView object @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mp = new MediaPlayer (); // instantiate the MediaPlayer object sv = (SurfaceView) findViewById (R. id. surfaceView1); // obtain the SurfaceView component Button play = (Button) findViewById (R. id. play); // obtain the "play" Button final Button pause = (Button) findViewById (R. id. pause); // get" Pause/continue Button stop = (Button) findViewById (R. id. stop); // get the "stop" button // Add and click the event listener play for the "play" button. setOnClickListener (new OnClickListener () {public void onClick (View v) {mp. reset (); // reset the MediaPlayer object try {mp. setDataSource ("/sdcard/ccc.mp4"); // sets the video mp to be played. setDisplay (sv. getHolder (); // sets to output the video image to SurfaceViewmp. prepare (); // pre-load the video mp. start (); // start playing sv. setBackgroundResource (R. drawable. bg_playing); // change the background image paus of SurfaceView. E. setText ("pause"); pause. setEnabled (true); // set the "pause" button available} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (SecurityException e) {e. printStackTrace ();} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}}); // click the stop button to add and click the event listener stop. setOnClickListener (new OnClickListener () {public void onClick (View v) {if (mp. isPlaying () {mp. stop (); // stop playing sv. s EtBackgroundResource (R. drawable. bg_finish); // change the pause of the SurfaceView background image. setEnabled (false); // set the pause button to unavailable }}); // Add and click the event listener pause for the pause button. setOnClickListener (new OnClickListener () {public void onClick (View v) {// TODO Auto-generated method stubif (mp. isPlaying () {mp. pause (); // pause the video (Button) v ). setText ("continue");} else {mp. start (); // resume video playback (Button) v ). setText ("pause") ;}}); // adds the event listener for the MediaPlayer object. setOnComplet IonListener (new OnCompletionListener () {public void onCompletion (MediaPlayer mp) {// TODO Auto-generated method stubsv. setBackgroundResource (R. drawable. bg_finish); // change the background image Toast of SurfaceView. makeText (MainActivity. this, "video playback is complete! ", Toast. LENGTH_SHORT ). show () ;}}) ;}@ Overrideprotected void onDestroy () {if (mp. isPlaying () {mp. stop (); // stop playing the video} mp. release (); // release the resource super. onDestroy ();}}

Okay, it's over.



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.