1. MainActivity. java source code
[Html]
Import android. app. Activity;
Import android. graphics. PixelFormat;
Import android. media. AudioManager;
Import android. media. MediaPlayer;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. View;
Import android. widget. ImageButton;
Import android. widget. TextView;
Import android. widget. Toast;
Public class EX07_14 extends Activity
Implements SurfaceHolder. Callback
{
Private TextView mTextView01;
Private static final String TAG = "HIPPO_MediaPlayer"; // the flag for printing logs.
Private MediaPlayer mMediaPlayer01;
Private SurfaceView mSurfaceView01;
Private SurfaceHolder mSurfaceHolder01;
Private ImageButton mPlay;
Private ImageButton mPause;
Private ImageButton mReset;
Private ImageButton mStop;
Private boolean bIsPaused = false;
Private boolean bIsReleased = false;
Private String strVideoPath = "";
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
If (! CheckSDCard () // if no SD card exists
{
MMakeTextToast
(
GetResources (). getText (R. string. str_err_nosd). toString (),
True
);
}
MTextView01 = (TextView) findViewById (R. id. myTextView1 );
GetWindow (). setFormat (PixelFormat. UNKNOWN );
MSurfaceView01 = (SurfaceView) findViewById (R. id. mSurfaceView1); // display the animation container
MSurfaceHolder01 = mSurfaceView01.getHolder ();
MSurfaceHolder01.addCallback (this );
MSurfaceHolder01.setFixedSize (176,144 );
MSurfaceHolder01.setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
MPlay = (ImageButton) findViewById (R. id. play );
MPause = (ImageButton) findViewById (R. id. pause );
MReset = (ImageButton) findViewById (R. id. reset );
MStop = (ImageButton) findViewById (R. id. stop );
StrVideoPath = "/sdcard/a.3gp ";
MPlay. setOnClickListener (new ImageButton. OnClickListener ()
{
Public void onClick (View view)
{
If (checkSDCard ())
{
PlayVideo (strVideoPath );
}
}
});
MPause. setOnClickListener (new ImageButton. OnClickListener ()
{
Public void onClick (View view)
{
If (checkSDCard ())
{
If (mMediaPlayer01! = Null)
{
If (bIsReleased = false)
{
If (bIsPaused = false)
{
MMediaPlayer01.pause ();
BIsPaused = true;
MTextView01.setText (R. string. str_pause );
}
Else if (bIsPaused = true)
{
MMediaPlayer01.start ();
BIsPaused = false;
MTextView01.setText (R. string. str_play );
}
}
}
}
}
});
MReset. setOnClickListener (new ImageButton. OnClickListener ()
{
Public void onClick (View view)
{
If (checkSDCard ())
{
If (bIsReleased = false)
{
If (mMediaPlayer01! = Null)
{
MMediaPlayer01.seekTo (0 );
}
}
}
}
});
MStop. setOnClickListener (new ImageButton. OnClickListener ()
{
Public void onClick (View view)
{
If (checkSDCard ())
{
If (mMediaPlayer01! = Null)
{
If (bIsReleased = false)
{
MMediaPlayer01.stop ();
MMediaPlayer01.release ();
BIsReleased = true;
MTextView01.setText (R. string. str_stop );
}
}
}
}
});
}
Private void playVideo (String strPath)
{
MMediaPlayer01 = new MediaPlayer ();
MMediaPlayer01.setAudioStreamType (AudioManager. STREAM_MUSIC );
MMediaPlayer01.setDisplay (mSurfaceHolder01 );
Try
{
MMediaPlayer01.setDataSource (strPath );
}
Catch (Exception e)
{
// TODO Auto-generated catch block
MTextView01.setText ("setDataSource Exceeption:" + e. toString ());
}
Try
{
MMediaPlayer01.prepare ();
}
Catch (Exception e)
{
// TODO Auto-generated catch block
MTextView01.setText ("prepare Exceeption:" + e. toString ());
}
MMediaPlayer01.start ();
BIsReleased = false;
MTextView01.setText (R. string. str_play );
MMediaPlayer01.setOnCompletionListener
(New MediaPlayer. OnCompletionListener ()
{
@ Override
Public void onCompletion (MediaPlayer arg0)
{
// TODO Auto-generated method stub
MTextView01.setText (R. string. str_stop );
}
});
}
Private boolean checkSDCard ()
{
If (android. OS. Environment. getExternalStorageState (). equals (android. OS. Environment. MEDIA_MOUNTED ))
{
Return true;
}
Else
{
Return false;
}
}
Public void mMakeTextToast (String str, boolean isLong)
{
If (isLong = true)
{
Toast. makeText (EX07_14.this, str, Toast. LENGTH_LONG). show ();
}
Else
{
Toast. makeText (EX07_14.this, str, Toast. LENGTH_SHORT). show ();
}
}
@ Override
Public void surfaceChanged
(SurfaceHolder surfaceholder, int format, int w, int h)
{
// TODO Auto-generated method stub
Log. I (TAG, "Surface Changed ");
}
@ Override
Public void surfaceCreated (SurfaceHolder surfaceholder)
{
// TODO Auto-generated method stub
Log. I (TAG, "Surface Changed ");
}
@ Override
Public void surfaceDestroyed (SurfaceHolder surfaceholder)
{
// TODO Auto-generated method stub
Log. I (TAG, "Surface Destroyed ");
}
}
Ii. main. xml source code
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: background = "@ drawable/white"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: id = "@ + id/myTextView1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textColor = "@ drawable/blue"
Android: text = "@ string/hello"
/>
<SurfaceView
Android: id = "@ + id/mSurfaceView1"
Android: layout_width = "100px"
Android: layout_height = "100px">
</SurfaceView>
<SeekBar android: id = "@ + id/seekBar" android: layout_height = "wrap_content" android: layout_width = "fill_parent"/>
<LinearLayout
Android: orientation = "horizontal"
Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent"
Android: padding = "10dip"
>
<ImageButton android: id = "@ + id/play"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: src = "@ drawable/play"
/>
<ImageButton android: id = "@ + id/pause"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: src = "@ drawable/pause"
/>
<ImageButton android: id = "@ + id/reset"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: src = "@ drawable/reset"
/>
<ImageButton android: id = "@ + id/stop"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: src = "@ drawable/stop"
/>
</LinearLayout>
</LinearLayout>
Third, the remaining images are required in the source code. You can bring your own images. You can play the video after testing. You must input a playback file in the sdcard.
From the column of ODA