android基础--视频播放器

 

public class MainActivity extends Activity {

       private EditText eFileName;

       private ImageButton pauseButton;

       private ImageButton playButton;

       private ImageButton resetButton;

       private ImageButton stopButton;

       private MediaPlayer mediaPlayer;

       private SurfaceView surfaceView;

       private int position ;

      

       private File videoFile;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        mediaPlayer = new MediaPlayer();

       

        surfaceView = (SurfaceView) this.findViewById(R.id.surfaceview);

        eFileName = (EditText) this.findViewById(R.id.filename);

        pauseButton = (ImageButton) this.findViewById(R.id.pausebutton);

        playButton = (ImageButton)this.findViewById(R.id.playbutton);

        resetButton = (ImageButton)this.findViewById(R.id.resetbutton);

        stopButton = (ImageButton)this.findViewById(R.id.stopbutton);

       

        ButtonClickListener listener = new ButtonClickListener();

        playButton.setOnClickListener(listener);

        pauseButton.setOnClickListener(listener);

        resetButton.setOnClickListener(listener);

        stopButton.setOnClickListener(listener);

       

        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率

        //SurfaceView Activity

        //当Activity离开前台,SurfaceView会被销毁,当Activity重新回到前台,SurfaceView又会被重新创建,

        //创建发生在onResume()方法之后

        surfaceView.getHolder().addCallback(new SurfaceCallback());

    }

    private final class SurfaceCallback implements Callback{

              public void surfaceChanged(SurfaceHolder holder, int format, int width,

                            int height) {

                    

              }

 

              public void surfaceCreated(SurfaceHolder holder) {

                     if(videoFile != null&& position > 0){

                            try {

                                   play();

                                   mediaPlayer.seekTo(position);

                                   position = 0;

                            } catch (IOException e) {

                                   e.printStackTrace();

                            }

                     }

              }

 

              public void surfaceDestroyed(SurfaceHolder holder) {

                     if(mediaPlayer.isPlaying()){

                            position = mediaPlayer.getCurrentPosition();

                            mediaPlayer.stop();

                     }

              }

          

    }

       protected void onDestroy() {

              mediaPlayer.release();//在onDestroy中释放,进行摧毁

              super.onDestroy();

       }

       private void play() throws IOException {

              mediaPlayer.reset();//考虑到以后重新播放

              mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置声音格式为流式音乐

              mediaPlayer.setDataSource(videoFile.getPath());//设置文件路径

              mediaPlayer.setDisplay(surfaceView.getHolder());

              mediaPlayer.prepare();

              mediaPlayer.start();

       }

 

       private final class ButtonClickListener implements View.OnClickListener{

              private boolean pause;

              public void onClick(View v) {

                     try {

                            switch (v.getId()) {

                            case R.id.playbutton:

                                   String filename = eFileName.getText().toString();//取得视频文件名称

                                   videoFile = new File(Environment.getExternalStorageDirectory(),filename);

                                   if(videoFile.exists()){

                                          play();

                                   }else{

                                          Toast.makeText(getApplicationContext(), R.string.filenoexist, 1).show();

                                          videoFile = null;

                                   }

                                   break;

                            case R.id.pausebutton:

                                   if(mediaPlayer.isPlaying()){

                                          mediaPlayer.pause();

                                          pause = true;

                                   }else{

                                          if(pause){

                                                 mediaPlayer.start();

                                                 pause = false;

                                          }

                                   }

                                   break;

                            case R.id.resetbutton:

                                          if(mediaPlayer.isPlaying()){

                                                 mediaPlayer.seekTo(0);

                                          }

                                          break;

                            case R.id.stopbutton:

                                                 if(mediaPlayer.isPlaying()){

                                                        mediaPlayer.stop();

                                                 }

                                                 break;

                            }

                     }catch (Exception e) {

                            e.printStackTrace();

                     }

              }

    }

}

阅读更多
个人分类: android学习
想对作者说点什么? 我来说一句

android 视频播放器

2017年09月03日 62KB 下载

android视频播放器-源码.

2011年12月21日 280KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭