生命周期:
注:播放完毕之后进入PlaybackCompleted状态。
播放视频:
public void setDisplay (SurfaceHolder sh)
Since: API Level 1
设置用于视频显示的SurfaceHolder。不论是surface holder或是surface,如果视频库需要,就必须设置。当播放一个视频而没有调用这个函数或是没有调用setSurface(Surface),那么只会播放音频了。一个空的surface holder或空的surface将会导致仅仅播放音频。
android.view.Surface:Handle onto a raw buffer that is being managed by the screen compositor.
android.view.SurfaceView:Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen.
SurfaceHolder是控制surface的一个抽象接口,你可以通过SurfaceHolder来控制surface的尺寸和格式,或者修改surface的像素,监视surface的变化等等,SurfaceHolder是SurfaceView的典型接口。
SurfaceView和Surface的关系:Surface是管理显示内容的数据(implementsParcelable),包括存储于数据的交换。而SurfaceView就是把这些数据显示出来到屏幕上面。
SurfaceHolder.Callback是监听surface改变的一个接口。它的方法有:
public abstract void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
public abstract voidsurfaceCreated(SurfaceHolder holder)
public abstract voidsurfaceDestroyed(SurfaceHolder holder)
代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mediaPlayer = new MediaPlayer();
nameText = (EditText) this.findViewById(R.id.filename);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
//把输送给surfaceView的视频画面,直接显示到屏幕上,不要维持它自身的缓冲区
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144);
surfaceView.getHolder().setKeepScreenOn(true);
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(position>0 && path!=null){
play(position);
position = 0;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if(mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。