標籤:
public class VideoActivity extends Activity implements Callback{
private SurfaceView view;
private SurfaceHolder holder;
private MediaPlayer mPlayer;
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_main);
initWidgets();
}
private void initWidgets() {
view=(SurfaceView) findViewById(R.id.surfaceView);
holder=view.getHolder();
holder.setFixedSize(320, 480);
holder.setKeepScreenOn(true);
//設定回調的監聽
holder.addCallback(this);
path=getIntent().getStringExtra("path");
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
mPlayer=new MediaPlayer();
try {
mPlayer.setDataSource(path);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDisplay(holder);
mPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
mPlayer.release();
}
public void play(View v){
if (!mPlayer.isPlaying()) {
mPlayer.start();
}
}
}
public class MainActivity extends Activity implements SurfaceHolder.Callback {//2.實現回調SurfaceHolder.Callback
private VideoView videoView;
// 第二種介面
private SurfaceView view;
private SurfaceHolder holder;
private MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vedio_main);
// String path = Environment.getExternalStorageDirectory()+"/move.mp4";
// //初始化視頻介面
// videoView = (VideoView)findViewById(R.id.video);
//
// //向視頻介面載入視頻源
// videoView.setVideoPath(path);
//
// MediaController controller = new MediaController(this);//控制中心
// controller.setMediaPlayer(videoView);//將播放介面用controller來控制
//
// videoView.setMediaController(controller);//介面與控制中心綁定
// videoView.requestFocus();//設定視頻有焦點
// 自訂的介面:視頻播放器,SurfaceView是專做介面的容器
view = (SurfaceView) findViewById(R.id.view);
// 1.綁定介面並設定參數
holder = view.getHolder();// 執行個體化視頻Holder對象,此時由holder來設定參數
holder.setFixedSize(320, 480);// 視頻解析度,不寫就是預設
holder.setKeepScreenOn(true);// 螢幕一直開著
//設定回調的監聽
holder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {//介面改變時
}
@Override
public void surfaceCreated(SurfaceHolder holder) {//建立介面的時候
mPlayer = new MediaPlayer();
try {
//Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null,null,null,null);
//while(cursor.moveToNext()){
//Log.i("path", cursor.getString(cursor.getColumnIndex(Media.DATA)));
//}
mPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/move.mp4");
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//音源類型
mPlayer.setDisplay(holder);//載入設定好的自訂介面
mPlayer.prepare();//準備
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {//銷毀介面
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.release();
}
}
//播放按鈕
public void play(View v){
if(!mPlayer.isPlaying()){
mPlayer.start();
}
}
}
媒體播放自訂介面