Android应用开发之视频播放器

package cn.class3g.videoplayer;      import java.io.File;   import java.io.IOException;      import android.app.Activity;   import android.media.AudioManager;   import android.media.MediaPlayer;   import android.os.Bundle;   import android.os.Environment;   import android.util.Log;   import android.view.SurfaceHolder;   import android.view.SurfaceHolder.Callback;   import android.view.SurfaceView;   import android.view.View;   import android.view.View.OnClickListener;   import android.widget.EditText;   import android.widget.ImageButton;   import android.widget.Toast;      public class VideoPlayerActivity extends Activity implements OnClickListener {          private static final String TAG = "VideoPlayerActivity";       private ImageButton playBtn, pauseBtn, resetBtn, stopBtn;       private EditText fileEt;       private SurfaceView videoSv;          private MediaPlayer mediaPlayer;          private SurfaceHolder holder;       private int position = 0;       File videoFile = null;                 public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);              findViews();              mediaPlayer = new MediaPlayer();       }          private void findViews() {           fileEt = (EditText) this.findViewById(R.id.videoFileEt);           playBtn = (ImageButton) this.findViewById(R.id.playBtn);           pauseBtn = (ImageButton) this.findViewById(R.id.pauseBtn);           resetBtn = (ImageButton) this.findViewById(R.id.resetBtn);           stopBtn = (ImageButton) this.findViewById(R.id.stopBtn);              playBtn.setOnClickListener(this);           pauseBtn.setOnClickListener(this);           resetBtn.setOnClickListener(this);           stopBtn.setOnClickListener(this);              videoSv = (SurfaceView) this.findViewById(R.id.surfaceView);           holder = videoSv.getHolder();              holder.setFixedSize(176144); // 设置分辨率            /* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */           holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);           //holder.addCallback(new SurfaceCallback());        }          public void onClick(View v) {              if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) {               String fileName = fileEt.getText().toString().trim();                  videoFile = new File(                       Environment.getExternalStorageDirectory(), fileName);               if (videoFile.exists()) {                   try {                       switch (v.getId()) {                       case R.id.playBtn:                           playVideo(videoFile);                           break;                       case R.id.pauseBtn:   //                      if(mediaPlayer.isPlaying()){    //                          mediaPlayer.pause();    //                      }else{    //                          mediaPlayer.start();    //                      }                            if(mediaPlayer.isPlaying()){                               position = mediaPlayer.getCurrentPosition();                               mediaPlayer.pause();                           }                                                      break;                       case R.id.resetBtn:                           if(mediaPlayer.isPlaying()){                               mediaPlayer.seekTo(0);                           }else{                               playVideo(videoFile);                           }                           break;                       case R.id.stopBtn:                           if(mediaPlayer.isPlaying()){                               mediaPlayer.stop();                           }                           break;                       }                   } catch (Exception e) {                       Log.e(TAG, e.toString());                   }                  } else {                   showToast(R.string.notfoundfile_error);               }           } else {               showToast(R.string.notfoundsdcard_error);           }       }          private void playVideo(File videoFile) throws IOException {           mediaPlayer.reset();           mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);           mediaPlayer.setDataSource(videoFile.getAbsolutePath());           mediaPlayer.setDisplay(holder);           mediaPlayer.prepare();           mediaPlayer.start();       }          private void showToast(int resId) {           Toast.makeText(this, resId, 1).show();       }   }  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wywfgw.html