final SurfaceView surfaceView = new SurfaceView(StartupActivity.this);
StartupActivity.this.mediaPlayer = new MediaPlayer();
surfaceView.setLayoutParams(params);
reLayout.addView(surfaceView, params);
// 设置SurfaceView自己不管理的缓冲区
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// 设置需要播放的视频
AssetFileDescriptor afd = getAssets().openFd("XXX.mp4");
mediaPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
// 把视频画面输出到SurfaceView
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
// 播放
mediaPlayer.start();
Log.i(TAG, "SurfaceView start play");
}
});
mediaPlayer.prepare();
} catch (Exception e) {
// TODO: handle exception
Log.i(TAG, "SurfaceView "
+ "start play exception : "+ e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
});
// 注销
if (null!=mediaPlayer)
{
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}