Most of the Code is the same as that of a music player.
Put the video file in the sdcard and enter the file name in the input box.
Use android2.0 to run the simulator. It is said that the simulator of other versions is used.
Video Playback is not supported. If any error occurs, please note
The java code is as follows:
Package cn. mrzhu;
Import java. io. File;
Import android. app. Activity;
Import android. media. MediaPlayer;
Import android. media. MediaPlayer. OnPreparedListener;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. view. SurfaceHolder;
Import android. view. SurfaceHolder. Callback;
Import android. view. SurfaceView;
Import android. view. View;
Import android. widget. EditText;
Import android. widget. Toast;
Public class VideoPlayDemoActivity extends Activity {
Private EditText nameText;
Private String path; // file path
Private MediaPlayer mediaPlayer; // media object
Private SurfaceView surfaceView;
Private boolean pause; // pause the flag
Private int position; // playback progress
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
NameText = (EditText) findViewById (R. id. filename );
SurfaceView = (SurfaceView) findViewById (R. id. surfaceview );
// Directly display the video image sent to surfaceview to the screen. do not maintain its own buffer zone.
SurfaceView. getHolder (). setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
SurfaceView. getHolder (). setFixedSize (176,144); // you can specify the video playback window size.
SurfaceView. getHolder (). setKeepScreenOn (true); // keep the screen highlighted
SurfaceView. getHolder (). addCallback (new SurfaceCallback ());
MediaPlayer = new MediaPlayer (); // instantiate a Player Object
}
Private final class SurfaceCallback implements Callback {
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
}
/*
* When surfaceView is created
*/
Public void surfaceCreated (SurfaceHolder holder ){
If (position> 0 & path! = Null ){
Play (position );
Position = 0;
}
}
/*
* When surfaceView is destroyed
*/
Public void surfaceDestroyed (SurfaceHolder holder ){
MediaPlayer. stop ();
}
}
/*
* Button event. In main. xml, click_on = mediaplay for each button
*/
Public void mediaplay (View v ){
Switch (v. getId ()){
Case R. id. playbutton: // play button
String filename = nameText. getText (). toString (); // get the file name
File file = new File (Environment. getExternalStorageDirectory (), filename );
If (file. exists ()){
Path = file. getAbsolutePath ();
Play (0 );
} Else {
// If the path does not exist, the Toast prompt is displayed.
Toast. makeText (getApplicationContext (), R. string. filenoexsit, Toast. LENGTH_SHORT). show ();
}
Break;
Case R. id. pausebutton: // pause button
If (mediaPlayer. isPlaying ()){
MediaPlayer. pause ();
Pause = true;
} Else {
MediaPlayer. start ();
Pause = false;
}
Break;
Case R. id. resetbutton: // replay button
If (mediaPlayer. isPlaying ()){
MediaPlayer. seekTo (0 );
} Else {
If (path! = Null ){
Play (0); // set the playback progress to 0 to restore the initial state.
}
}
Break;
Case R. id. stopbutton: // stop button
If (mediaPlayer. isPlaying ()){
MediaPlayer. stop ();
}
Break;
}
}
Private void play (int position ){
Try {
MediaPlayer. reset ();
MediaPlayer. setDataSource (path );
MediaPlayer. setDisplay (surfaceView. getHolder ());
MediaPlayer. prepare ();
MediaPlayer. setOnPreparedListener (new PrepareListener (position ));
} Catch (Exception e ){
}
}
Private final class PrepareListener implements OnPreparedListener {
Private int position;
Public PrepareListener (int position ){
Super ();
This. position = position;
}
Public void onPrepared (MediaPlayer mp ){
MediaPlayer. start (); // play the video
If (position> 0) mediaPlayer. seekTo (position );
}
}
@ Override
Protected void onDestroy (){
MediaPlayer. release ();
MediaPlayer = null;
Super. onDestroy ();
}
}
The main. xml Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
Android: background = "# FFFFFF">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/filename"/>
<EditText
Android: id = "@ + id/filename"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/file"/>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/playbutton"
Android: onClick = "mediaplay"
Android: text = "@ string/playbutton"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/pausebutton"
Android: onClick = "mediaplay"
Android: text = "@ string/pausebutton"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/resetbutton"
Android: onClick = "mediaplay"
Android: text = "@ string/resetbutton"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/stopbutton"
Android: onClick = "mediaplay"
Android: text = "@ string/stopbutton"
/>
</LinearLayout>
<SurfaceView
Android: layout_width = "fill_parent"
Android: layout_height = "240dp"
Android: id = "@ + id/surfaceview"
/>
</LinearLayout>
The string. xml Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "filenoexsit"> the video file does not exist </string>
<String name = "app_name"> Video Player </string>
<String name = "filename"> video file name </string>
<String name = "playbutton"> playback </string>
<String name = "pausebutton"> pause </string>
<String name = "resetbutton"> replay </string>
<String name = "stopbutton"> stop </string>
<String name = "file"> OPPO.mp4 </string>
</Resources>