Android 使用意图播放本地视频

Android播放视频的方式有三种:

一、使用意图播放,调用本地安装的播放器,选择一个进行播放。

二、使用VideoView播放(VideoView事实上是对MediaPlayer的封装,使用起来非常easy,可是缺少灵活性)。

三、使用MediaPlayer播放(将MediaPlayer对象用于视频播放可以为控制播放本身提供最大的灵活性)。

本文章仅仅解说使用意图播放视频,用于处理播放的详细机制也是MediaPlayer。其余的播放将在后面的文章中讲到。



源码:

布局文件activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放" />

</LinearLayout>

代码文件:

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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(this); } /** * 点击按钮后。选择系统已安装的视频播放器进行视频的播放。 */ @Override public 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的视频文件。


源码下载:

点击下载源代码

原文地址:https://www.cnblogs.com/cxchanpin/p/6819407.html