標籤:android
生命週期:
650) this.width=650;" src="http://hi.csdn.net/attachment/201005/26/521376_1274912556IBBI.gif" alt="521376_1274912556IBBI.gif" />
註:播放完畢之後進入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(); } } }
Android mediaplayer學習筆記