1. Enter the mksdcard 512 M c: \ sdcard. img command in the command line to create the sdcard image file.
2. Click the Eclipse menu command "Window/Preferences" to open the Preferences Window, and select "Android/Launch" on the left ", enter "-sdcard c: \ sdcard in" Default emulator options "on the right. img ".
3. Start the simulator, switch to DDMS, find the "File Explorer" window, select "mnt/sdcard", and execute "Push a file onto the device" in the upper right corner ", add the video file "qtfy01.3gp" to the sdcard ".
4. compile the project layout file main. xml. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<SurfaceView
Android: id = "@ + id/surfaceView"
Android: layout_width = "fill_parent"
Android: layout_height = "pixel PX"
/>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
<Button
Android: id = "@ + id/play_btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "play"
/>
<Button
Android: id = "@ + id/pause_btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "paused"
/>
</LinearLayout>
</LinearLayout>
5. Compile MainActivity. java code.
Package game. test;
Import android. app. Activity;
Import android. graphics. PixelFormat;
Import android. media. AudioManager;
Import android. media. MediaPlayer;
Import android. OS. Bundle;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class MainActivity extends Activity implements SurfaceHolder. Callback {
String path = "/sdcard/qtfy01.3gp ";
Button play_btn, pause_btn;
Boolean isPause = false;
SurfaceHolder surfaceHolder;
MediaPlayer mediaPlayer;
SurfaceView surfaceView;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Play_btn = (Button) findViewById (R. id. play_btn );
Play_btn.setOnClickListener (video_listener );
Pause_btn = (Button) findViewById (R. id. pause_btn );
Pause_btn.setOnClickListener (video_listener );
GetWindow (). setFormat (PixelFormat. UNKNOWN );
SurfaceView = (SurfaceView) findViewById (R. id. surfaceView );
SurfaceHolder = surfaceView. getHolder ();
SurfaceHolder. addCallback (this );
SurfaceHolder. setFixedSize (176,144 );
SurfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
MediaPlayer = new MediaPlayer ();
}
Private OnClickListener video_listener = new OnClickListener (){
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. play_btn:
IsPause = false;
PlayVideo (path );
Break;
Case R. id. pause_btn:
If (isPause = false ){
MediaPlayer. pause ();
IsPause = true;
} Else {
MediaPlayer. start ();
IsPause = false;
}
Break;
}
}
Private void playVideo (String path ){
// TODO Auto-generated method stub
If (mediaPlayer. isPlaying () = true ){
MediaPlayer. reset ();
}
MediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
MediaPlayer. setDisplay (surfaceHolder );
Try {
MediaPlayer. setDataSource (path );
MediaPlayer. prepare ();
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}
MediaPlayer. start ();
}
};
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
// TODO Auto-generated method stub
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
// TODO Auto-generated method stub
}
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub
}
}
Author's "Android Learning Experience"