Android 使用意圖播放本地視頻

來源:互聯網
上載者:User

Android播放視頻的方式有三種:

一、使用意圖播放,調用本地安裝的播放器,選擇一個進行播放。

二、使用VideoView播放(VideoView其實是對MediaPlayer的封裝,使用起來很簡單,但是缺少靈活性)。

三、使用MediaPlayer播放(將MediaPlayer對象用於視頻播放能夠為控制播放本身提供最大的靈活性)。

本文章只講解使用意圖播放視頻,用於處理播放的具體機制也是MediaPlayer,其餘的播放將在後面的文章中講到。


原始碼:

布局檔案activity_main:

    

代碼檔案:

MainActivity:

package com.multimediademo10videointent;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * 通過使用意圖觸發內建的媒體播放器進行本地視頻播放。 *  */public class MainActivity extends Activity implements OnClickListener {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}/** * 點擊按鈕後,選擇系統已安裝的視頻播放器進行視頻的播放。 */@Overridepublic void onClick(View v) {/** * 使用Intent.ACTION_VIEW常量構造一個活動,並通過setDataAndType方法傳入檔案的URI和MIME類型 */Intent intent = new Intent(android.content.Intent.ACTION_VIEW);Uri data = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/1.mp4");intent.setDataAndType(data, "video/mp4");startActivity(intent);}}


需要說明的是,在運行改程式之前,你需要在你的sd卡的根目錄放置一個名為1.mp4的視頻檔案。


原始碼下載:

點擊下載源碼

聯繫我們

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