Android应用开发之视频播放器


资源:

ImageButton所用图片4张

Strings:
  1. <string name="app_name">MyVideoPlayer</string>  
  2.     <string name="video_text">视频文件</string>  
  3.     <string name="notfoundsdcard_error">找不到Sd卡</string>  
  4.     <string name="notfoundfile_error">找不到视频文件</string>  

布局

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#FFFFCC"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/video_text" />  
  12.   
  13.     <EditText  
  14.         android:id="@+id/videoFileEt"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="cat.3gp" />  
  18.   
  19.     <TableLayout  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:gravity="center"  
  23.         android:stretchColumns="*" >  
  24.   
  25.         <TableRow >  
  26.   
  27.             <ImageButton  
  28.                 android:id="@+id/playBtn"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:src="@drawable/play" />  
  32.   
  33.             <ImageButton  
  34.                 android:id="@+id/pauseBtn"  
  35.                 android:layout_width="wrap_content"  
  36.                 android:layout_height="wrap_content"  
  37.                 android:src="@drawable/pause" />  
  38.   
  39.             <ImageButton  
  40.                 android:id="@+id/resetBtn"  
  41.                 android:layout_width="wrap_content"  
  42.                 android:layout_height="wrap_content"  
  43.                 android:src="@drawable/reset" />  
  44.   
  45.             <ImageButton  
  46.                 android:id="@+id/stopBtn"  
  47.                 android:layout_width="wrap_content"  
  48.                 android:layout_height="wrap_content"  
  49.                 android:src="@drawable/stop" />  
  50.         </TableRow>  
  51.     </TableLayout>  
  52.   
  53.     <SurfaceView  
  54.         android:id="@+id/surfaceView"  
  55.         android:layout_width="fill_parent"  
  56.         android:layout_height="240dip" />  
  57.   
  58. </LinearLayout>  

Activity

  1. package cn.class3g.videoplayer;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import android.app.Activity;  
  7. import android.media.AudioManager;  
  8. import android.media.MediaPlayer;  
  9. import android.os.Bundle;  
  10. import android.os.Environment;  
  11. import android.util.Log;  
  12. import android.view.SurfaceHolder;  
  13. import android.view.SurfaceHolder.Callback;  
  14. import android.view.SurfaceView;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.EditText;  
  18. import android.widget.ImageButton;  
  19. import android.widget.Toast;  
  20.   
  21. public class VideoPlayerActivity extends Activity implements OnClickListener {  
  22.   
  23.     private static final String TAG = "VideoPlayerActivity";  
  24.     private ImageButton playBtn, pauseBtn, resetBtn, stopBtn;  
  25.     private EditText fileEt;  
  26.     private SurfaceView videoSv;  
  27.   
  28.     private MediaPlayer mediaPlayer;  
  29.   
  30.     private SurfaceHolder holder;  
  31.     private int position = 0;  
  32.     File videoFile = null;  
  33.       
  34.   
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.main);  
  38.   
  39.         findViews();  
  40.   
  41.         mediaPlayer = new MediaPlayer();  
  42.     }  
  43.   
  44.     private void findViews() {  
  45.         fileEt = (EditText) this.findViewById(R.id.videoFileEt);  
  46.         playBtn = (ImageButton) this.findViewById(R.id.playBtn);  
  47.         pauseBtn = (ImageButton) this.findViewById(R.id.pauseBtn);  
  48.         resetBtn = (ImageButton) this.findViewById(R.id.resetBtn);  
  49.         stopBtn = (ImageButton) this.findViewById(R.id.stopBtn);  
  50.   
  51.         playBtn.setOnClickListener(this);  
  52.         pauseBtn.setOnClickListener(this);  
  53.         resetBtn.setOnClickListener(this);  
  54.         stopBtn.setOnClickListener(this);  
  55.   
  56.         videoSv = (SurfaceView) this.findViewById(R.id.surfaceView);  
  57.         holder = videoSv.getHolder();  
  58.   
  59.         holder.setFixedSize(176144); // 设置分辨率   
  60.         /* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */  
  61.         holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  62.         //holder.addCallback(new SurfaceCallback());   
  63.     }  
  64.   
  65.     public void onClick(View v) {  
  66.   
  67.         if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) {  
  68.             String fileName = fileEt.getText().toString().trim();  
  69.   
  70.             videoFile = new File(  
  71.                     Environment.getExternalStorageDirectory(), fileName);  
  72.             if (videoFile.exists()) {  
  73.                 try {  
  74.                     switch (v.getId()) {  
  75.                     case R.id.playBtn:  
  76.                         playVideo(videoFile);  
  77.                         break;  
  78.                     case R.id.pauseBtn:  
  79. //                      if(mediaPlayer.isPlaying()){   
  80. //                          mediaPlayer.pause();   
  81. //                      }else{   
  82. //                          mediaPlayer.start();   
  83. //                      }   
  84.                         if(mediaPlayer.isPlaying()){  
  85.                             position = mediaPlayer.getCurrentPosition();  
  86.                             mediaPlayer.pause();  
  87.                         }  
  88.                           
  89.                         break;  
  90.                     case R.id.resetBtn:  
  91.                         if(mediaPlayer.isPlaying()){  
  92.                             mediaPlayer.seekTo(0);  
  93.                         }else{  
  94.                             playVideo(videoFile);  
  95.                         }  
  96.                         break;  
  97.                     case R.id.stopBtn:  
  98.                         if(mediaPlayer.isPlaying()){  
  99.                             mediaPlayer.stop();  
  100.                         }  
  101.                         break;  
  102.                     }  
  103.                 } catch (Exception e) {  
  104.                     Log.e(TAG, e.toString());  
  105.                 }  
  106.   
  107.             } else {  
  108.                 showToast(R.string.notfoundfile_error);  
  109.             }  
  110.         } else {  
  111.             showToast(R.string.notfoundsdcard_error);  
  112.         }  
  113.     }  
  114.   
  115.     private void playVideo(File videoFile) throws IOException {  
  116.         mediaPlayer.reset();  
  117.         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
  118.         mediaPlayer.setDataSource(videoFile.getAbsolutePath());  
  119.         mediaPlayer.setDisplay(holder);  
  120.         mediaPlayer.prepare();  
  121.         mediaPlayer.start();  
  122.     }  
  123.   
  124.     private void showToast(int resId) {  
  125.         Toast.makeText(this, resId, 1).show();  
  126.     }  
  127. }  
  • 1
  • 2
  • 下一页

相关内容