package com.example.videoplayer; import java.io.FileDescriptor; import android.app.Activity; import android.content.res.AssetFileDescriptor; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnBufferingUpdateListener; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.SurfaceHolder; import android.view.SurfaceView; public class VideoPlayer extends Activity implements SurfaceHolder.Callback, OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener { private static final String TAG = "VideoPlayer"; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private MediaPlayer mMediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSurfaceView = new SurfaceView(this); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); //类型必须设置成SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); setContentView(mSurfaceView); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.video_player, menu); return true; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.v(TAG, "surfaceChanged"); } @Override public void surfaceCreated(SurfaceHolder holder) { Log.v(TAG, "surfaceCreated"); //SurfaceCreated 创建成功才可以播放视频 PlayVideo(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub Log.v(TAG, "surfaceDestroyed"); } @Override public void onBufferingUpdate(MediaPlayer arg0, int arg1) { // TODO Auto-generated method stub Log.v(TAG, "onBufferingUpdate"); } @Override public void onCompletion(MediaPlayer arg0) { // TODO Auto-generated method stub Log.v(TAG, "onCompletion"); } @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub Log.v(TAG, "onPrepared"); } public void PlayVideo(){ Log.v(TAG, "PlayVideo"); try { //视频文件放在 assert中 AssetFileDescriptor aFD = this.getAssets().openFd("welcome.3gp"); FileDescriptor fileDescriptor = aFD.getFileDescriptor(); this.mMediaPlayer = new MediaPlayer(); this.mMediaPlayer.setDataSource(fileDescriptor, aFD.getStartOffset(), aFD.getLength()); aFD.close(); //视频放在SDCard中 //File sdFile = Environment.getExternalStorageDirectory(); //this.mMediaPlayer = new mMediaPlayer(); //this.mMediaPlayer.setDataSource(sdFile.getAbsoluteFile() + File.separator + "welcome.3gp"); //视频放在res中 //this.mMediaPlayer = mMediaPlayer.create(this, R.raw.welcome); //网上的视频,没有测试 //this.mMediaPlayer // .setDataSource("http://xy2.163.com/download/down/wukong.mp3"); this.mMediaPlayer.setDisplay(this.mSurfaceHolder); this.mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); //准备完成后才可以播放,另外如果文件特别大或者从网上获得的资源 //会在这里等待时间过长,造成堵塞,这样的话就得用 //this.mMediaPlayer.prepareAsync(),然后监听是否准备完毕在开始 this.mMediaPlayer.prepare(); this.mMediaPlayer.start(); } catch (Exception e) { // TODO: handle exception } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (this.mMediaPlayer != null) { this.mMediaPlayer.release(); this.mMediaPlayer = null; } } }