您的位置:首页 > 移动开发 > Android开发

Android VideoView简单播放视频

2015-11-21 01:39 453 查看
给Android VideoView一个文件目录,就可以直接播放智能设备中的视频文件,现在以播放事先用手机拍好并重命名的视频文件test.mp4为例。
(1) 需要在布局文件中写一个ViedoView:

<RelativeLayout 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"
tools:context="com.example.videoview.MainActivity" >

<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/videoView" />

</RelativeLayout>


(2)不要忘记在AndroidManifest.xml文件中添加读写外部存储的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


(3)在java代码中给VideoView设置文件目录,然后start播放:

package com.example.videoview;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.VideoView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

VideoView videoView = (VideoView) findViewById(R.id.videoView);

// 获得的path等于:/storage/emulated/0/DCIM
File path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

// 拼接完整路径
File f = new File(path, "/Camera/test.mp4");

// 此时的f.getAbsolutePath()=/storage/emulated/0/DCIM//\Camera/test.mp4
videoView.setVideoPath(f.getAbsolutePath());

// 开始播放视频
videoView.start();

// VideiView获焦点
// videoView.requestFocus();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: