android視頻播放

來源:互聯網
上載者:User

視頻播放比音頻播放要複雜一些,除了音頻之外,還需要考慮一個視覺組件。為瞭解決這一問題,Android提供了一個專門的視圖控制器 android.widget.VideoView,封裝了MediaPlayer的建立和初始化過程,這個小組件可以用在任何布局管理器中,而且它提供了很多顯示選項,包括縮放和著色。要實現播放功能,我們要做的是,建立一個VideoView小組件並將其設定為使用者介面的內容,然後設定視圖的路徑或
URI並觸發start()方法。

    視頻播放除了需要按鈕控制項外,還需要一個顯示視頻的架構。對於我們這個例子,這裡使用VideoView組件顯示視頻內容,沒有建立我們自己的按鈕控制項,而是建立了一個MediaController來提供這些按鈕(如果需要另外建立的話,也可以自己建立)。

package video.player.activties;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

   private VideoView videoView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        videoView = (VideoView) this.findViewById(R.id.videoView);
        MediaController mc = new MediaController(this);
        videoView.setMediaController(mc);
        videoView.setVideoPath("/sdcard/music/test.mp4");
        videoView.requestFocus();
        videoView.start();
    }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <VideoView
     android:id="@+id/videoView"
     android:layout_width="320dip"
     android:layout_height="260dip"
    ></VideoView>
</LinearLayout>

相關文章

聯繫我們

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