首页 > 代码库 > SurfaceView+MediaPlayer播放视频

SurfaceView+MediaPlayer播放视频

  SurfaceView拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独立的线程中进行行绘制。又由于不占用主线程资源,SurfaceView一方面可以实现复杂而高效的UI,另一方面又不会导致用户输入得不到及时响应。

Android 三种播放视频的方式:

    http://www.cnblogs.com/eustoma/p/4479363.html

Android之SurfaceView实现视频播放:

         http://www.cnblogs.com/devinzhang/archive/2012/02/03/2337576.html

结合MediaPlayer类的简单播放器

public class SurfaceMediaPlayer extends Activity {

    SurfaceView surfaceView;

    MediaPlayer mediaPlayer;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_surface_media_player);

        mediaPlayer=new MediaPlayer();

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

        surfaceView.getHolder().addCallback(new Callback() {                

            @Override

            public void surfaceDestroyed(SurfaceHolder holder) { }

            @Override

            public void surfaceCreated(SurfaceHolder holder) {

                play();

            }                        

            @Override

public void surfaceChanged(SurfaceHolder h, int format, int w, int h) {  }

    });   

}

private void play() {

  try {

 String path = Environment.getExternalStorageDirectory().getPath()+"/Movies/run_to_you.3gp";

            mediaPlayer.setDataSource(path);

            mediaPlayer.setDisplay(surfaceView.getHolder());//视频画面输出到SurfaceView

            mediaPlayer.prepare();

            mediaPlayer.start();

    } catch (Exception e) { ……}

}

SurfaceView+MediaPlayer播放视频