通过手机上安装的第三方播放器来播放APK内的视频文件
刚开始在网上查了很多资料,因为以前做过 通过第三方播放器播放本地视频的功能
大概代码如下
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v");
//调用系统自带的播放器
Intent intent = new Intent(Intent.ACTION_VIEW);
Log.v("URI:::::::::", uri.toString());
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
把视频文件放到raw文件夹中或者assets文件夹下,获取的URI,传递给Intent后都提示视频不存在
而这个URI通过videoView播放的话是没有问题的
所以判断是APK内的视频只能输出流给外部,无法通过URI让第三方调用
所以考虑 另一种方案:
在APK初次安装时把视频文件写入SD卡中,试过后可以实现功能,
代码如下
manifest中需要如下权限
//MainActivity的OnCreate()方法中执行createFile();方法
private String
fileDirPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()// 得到外部存储卡的数据库的路径名
+ "/wavebot/example/";// 我要存储的目录
private
String fileName
= "example.mp4";// 要存储的文件名
/**
* 复制文件到SD卡
*
*/
private void createFile() {
String filePath = fileDirPath + "/" + fileName;// 文件路径
try {
File dir = new File(fileDirPath);// 目录路径
if (!dir.exists()) {// 如果不存在,则创建路径名
* 复制文件到SD卡
*
*/
private void createFile() {
String filePath = fileDirPath + "/" + fileName;// 文件路径
try {
File dir = new File(fileDirPath);// 目录路径
if (!dir.exists()) {// 如果不存在,则创建路径名
Log.v (TAG,"要存储的目录不存在");
if (dir.mkdirs()) {// 创建该路径名,返回true则表示创建成功
Log.v (TAG,"已经创建文件存储目录");
} else {
Log.v (TAG,"创建目录失败");
}
}
// 目录存在,则将apk中raw中的需要的文档复制到该目录下
File file = new File(filePath);
}
// 目录存在,则将apk中raw中的需要的文档复制到该目录下
File file = new File(filePath);
if (!file.exists()) {// 文件不存在
Log.v(TAG,"要打开的文件不存在");
InputStream ins = getResources().openRawResource(
R.raw.example);// 通过raw得到数据资源
Log.v (TAG,"开始读入");
FileOutputStream fos = new FileOutputStream(file);
Log.v (TAG,"开始写出");
byte[] buffer = new byte[8192];
int count = 0;// 循环写出
while ((count = ins.read(buffer)) > 0) {
fos.write(buffer, 0, count);
fos.write(buffer, 0, count);
}
Log.v (TAG,"已经创建该文件");
fos.close();// 关闭流
ins.close();
}
} catch (Exception e) {
e.printStackTrace();
}
ins.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
播放视频的时候 直接调用
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(new File(android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()// 得到外部存储卡的数据库的路径名
+ "/wavebot/example/example.mp4"));
intent.setDataAndType(uri, "video/*");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(new File(android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()// 得到外部存储卡的数据库的路径名
+ "/wavebot/example/example.mp4"));
intent.setDataAndType(uri, "video/*");
startActivity(intent);
但是考虑会占用 手机SD卡的内存,也不是最完美的解决方案,在此记录,以后找到更完美的方案,再做修改