Android Multimedia Development Notes [1] -- using SurfaceView + MediaPlayer to develop the simplest Video Player

Source: Internet
Author: User

Use the interfaces provided by the android SDK to develop the simplest video playback demo.
Next, paste the source code of main. xml:
[Html]
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
 
<SurfaceView
Android: id = "@ + id/surfaceView1"
Android: layout_width = "wrap_content"
Android: layout_height = "360dp"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentRight = "true"
Android: layout_alignParentTop = "true"/>
 
</RelativeLayout>

Next, paste the program source code:
[Java]
Package com. example. surfacemediaplayer;
 
Import android. media. MediaPlayer;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. app. Activity;
Import android. view. Menu;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. SurfaceHolder. Callback;
 
Public class SurfaceMediaPlayer extends Activity {

SurfaceView surfaceView;
MediaPlayer mediaPlayer;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_surface_media_player );

MediaPlayer = new MediaPlayer ();
SurfaceView = (SurfaceView) this. findViewById (R. id. surfaceView1 );

SurfaceView. getHolder (). addCallback (new Callback (){
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){

}
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
// Start playing
Play ();
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
 
}
});
}
 
Private void play (){
Try {
String path = Environment. getExternalStorageDirectory (). getPath () + "/Movies/run_to_you.3gp ";
MediaPlayer. setDataSource (path );
// Output the video image to SurfaceView
MediaPlayer. setDisplay (surfaceView. getHolder ());
MediaPlayer. prepare ();
// Start playing
MediaPlayer. start ();
} Catch (Exception e ){
// TODO: handle exception
}
}
 
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_surface_media_player, menu );
Return true;
}
}

Program running:

 

Related Article

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.