android開發學習之音視頻-VideoView

來源:互聯網
上載者:User

標籤:

VideoView實際上是android SDK中封裝好的一個用於播放視頻的組件,使用步驟如下:

1.在布局檔案中定義Video組件;

2.使用以下方法載入指定視頻:

1)setVideoPath(String path)

2)setVideoURI(Uri uri)

3.調用Video的start()、stop()、pause()方法來控制視頻播放。

 

例子如下:

布局檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"><!-- 定義VideoView播放視頻 --><VideoView    android:id="@+id/video"    android:layout_width="match_parent"    android:layout_height="match_parent" /></LinearLayout>

範例代碼,使用以下代碼可以實現播放視頻

videoView = (VideoView) findViewById(R.id.video);File video = new File("/mnt/sdcard/test.mp4");if(video.exists()){    videoView.setVideoPath(video.getAbsolutePath());    videoView.start();}

如果想實現一個有控制條的視頻播放效果,可以使用以下代碼

videoView = (VideoView) findViewById(R.id.video);mController = new MediaController(this);File video = new File("/mnt/sdcard/test.mp4");if(video.exists()){    videoView.setVideoPath(video.getAbsolutePath());
    // 設定videoView與mController建立關聯     videoView.setMediaController(mController);
    // 設定mController與videoView建立關聯     mController.setMediaPlayer(videoView);
}

效果如:

android開發學習之音視頻-VideoView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.