Android(安卓)使用意图播放本地视频
16lz
2021-01-24
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);}}
源代码下载:
点击下载源码
更多相关文章
- Android(安卓)studio 导入github上的源码库
- Android中各种Adapter的用法
- Android(安卓)Studio使用Apache POI在Android中生成.xls的Excel
- 转载-Android运行时异常“Binary XML file line # : Error inflat
- android屏幕的适配
- 从Eclipse迁移到Android(安卓)Stdio中
- Android中的ListView设置setOnItemClickListener后发现onItemCli
- Android(安卓)Studio 的项目结构
- android之创建和读取自定义资源文件