【Android】20.2 視頻播放

來源:互聯網
上載者:User

標籤:

分類:C#、Android、VS2015;

建立日期:2016-03-11 一、簡介

本節例子和上一節的音頻播放例子相似,也是最簡單的樣本,比如並沒有考慮視頻播放過程中電話打入的情況,也沒有考慮複雜的控制。總之,如果你希望進一步學習複雜的例子,首先要先把最核心的簡單例子搞明白,否則你連基本的設計思路都不知道,直接看複雜的例子或者實際項目中的代碼肯定“事倍功半”,主要原因是你做不到舉一反三,只會照搬,稍微讓你修改一下功能你就暈了。

實現視頻播放的常見方式有:

1、用VideoView和MediaController類實現視頻播放

常用,在布局檔案中使用VideoView結合MediaController來實現對其控制。本節主要示範它的基本用法。

VideoView控制項可以從各種不同的來源(如檔案系統、網站)下載視頻檔案並將視頻播放出來。該控制項提供了各種控制選項,例如控制視頻介面的大小、縮放、著色等。

如果需要更細粒度的控制,也可以用MediaPlayer類來實現。

2、用SurfaceView和MediaPlayer類實現視頻播放

這是早期版本提供的辦法,實現代碼較多。對這種實現有興趣的可參考其他資料。 二、基本用法樣本

1、運行

下面的是單擊【播放RAW下的視頻】按鈕後看到的視頻播放效果。

單擊【停止播放】按鈕後,可繼續單擊【播放SD卡Download下的視頻】按鈕觀察,這裡就不再了。

2、設計步驟

(1)添加視頻檔案

由於只是為了示範,所以該例子Raw檔案夾下和SD卡的Download檔案夾下存放的是同一個視頻檔案(videoviewdemo.mp4)。

至於如何將檔案複製到SD卡上,請參看【常見問題集】。

(2)添加ch2002Main.axml

<?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">    <Button        android:id="@+id/ch2002_btnRawStart"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="播放RAW下的視頻" />  <Button      android:id="@+id/ch2002_btnSdcardStart"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="播放SD卡Dwonload下的視頻" />  <Button        android:id="@+id/ch2002_btnStop"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="停止播放" />    <VideoView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/ch2002_videoView1"        android:layout_margin="20dp" /></LinearLayout>

(3)添加ch2002MainActivity.cs

using Android.App;using Android.Content;using Android.OS;using Android.Widget;using Android.Net;namespace MyDemos.SrcDemos{    [Activity(Label = "例20-2 視頻播放基本用法")]    [IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { ch.MyDemosCategory })]    public class ch2002MainActivity : Activity    {        VideoView mVideoView;        Button btnRawStart, btnSdcardStart, btnStop;        protected override void OnCreate(Bundle savedInstanceState)        {            base.OnCreate(savedInstanceState);            SetContentView(Resource.Layout.ch2002Main);            mVideoView = FindViewById<VideoView>(Resource.Id.ch2002_videoView1);            btnRawStart = FindViewById<Button>(Resource.Id.ch2002_btnRawStart);            btnSdcardStart = FindViewById<Button>(Resource.Id.ch2002_btnSdcardStart);            Uri rawUri = Uri.Parse($"android.resource://{PackageName}/{Resource.Raw.videoviewdemo}");            Uri sdcardUri = Uri.Parse($"{Environment.ExternalStorageDirectory.Name}/Download/videoviewdemo.mp4");            btnRawStart.Click += delegate            {                StartVideo(rawUri);            };            btnSdcardStart.Click += delegate            {                StartVideo(sdcardUri);            };            btnStop = FindViewById<Button>(Resource.Id.ch2002_btnStop);            btnStop.Click += delegate            {                mVideoView.StopPlayback();                ChangePlayingState(true, false);            };        }        private void StartVideo(Uri uri)        {            mVideoView.SetVideoURI(uri);            mVideoView.SetMediaController(new MediaController(this));            mVideoView.Start();            ChangePlayingState(false, true);        }        private void ChangePlayingState(bool startEnabled,bool stopEnabled)        {            btnRawStart.Enabled = btnSdcardStart.Enabled = startEnabled;            btnStop.Enabled = stopEnabled;        }    }}

【Android】20.2 視頻播放

聯繫我們

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