生命周期:


注:播放完毕之后进入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();
}
}
}另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:Androidmediaplayer学习笔记-创新互联
网页URL:http://www.jxjierui.cn/article/diiged.html


咨询
建站咨询
