Android mediaplayer學習筆記

來源:互聯網
上載者:User

標籤: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學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.