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

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

二、使用VideoView播放(VideoView其实是对MediaPlayer的封装,使用起来很简单,但是缺少灵活性)。

三、使用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;@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的视频文件。


源代码下载:

点击下载源码

更多相关文章

  1. Android(安卓)studio 导入github上的源码库
  2. Android中各种Adapter的用法
  3. Android(安卓)Studio使用Apache POI在Android中生成.xls的Excel
  4. 转载-Android运行时异常“Binary XML file line # : Error inflat
  5. android屏幕的适配
  6. 从Eclipse迁移到Android(安卓)Stdio中
  7. Android中的ListView设置setOnItemClickListener后发现onItemCli
  8. Android(安卓)Studio 的项目结构
  9. android之创建和读取自定义资源文件

随机推荐

  1. Android(安卓)Studio 4.1 中 Design Tool
  2. android BottomSheetDialog新控件解析,实
  3. Android———ContentProvider理解与使用
  4. Android(安卓)Studio导入android-support
  5. 四大组件及Window简要运行时序图
  6. Layout的编写
  7. java 获取当前所有线程的两种方法 group
  8. android lint的使用
  9. Android(安卓)EditText默认不弹出软件键
  10. 关于android 数据库SQLite的使用日记