首页 > 解决方案 > Android Studio:逐帧从 res/raw/ 目录中读取 .mp4

问题描述

对于我的应用程序,我需要将位于我的 res/raw/ 目录中的视频文件的单个图像加载到图像列表/数组中。从这个图像列表/数组中,我需要选择一些不合适的内容,这些内容将存储到一个新的视频文件中,该文件也位于 res/raw/目录中。

问题是我无法找到我的视频的路径。如果我尝试使用:

文件 f = new Flie("app/res/raw/test.mp4");

我得到错误:找不到文件。

我尝试使用 Uri 像:

字符串视频路径="android.resource://" + getPackageName()+ R.raw.test; 文件 f = new Flie(videopath.toString());

但这也不起作用。

这是我需要的伪代码:

List<Picture> video = new ArrayList<Picture>();
File file = new file("path_to_file/test.mp4");
FrameGrab grab = FrameGrab.createFrameGrab(NIOUtils.readableChannel(file));
Picture picture;

while (null != (picture = grab.getNativeFrame())) {
    video.add(picture);
}


List<picture> video_new = new ArrayList<picture>();
int[] idx = {1,2,4,6,8 ...}

for(int i= 0; i<idx.length; i++){
    picture= video.get(idx[i]);
    video_new.add(picture);
}

//stores the new video into the same path but with a different name
storefile("path_to_file/test_new.mp4", video_new);

标签: videoandroid-resourcesandroid-studio-3.0jcodec

解决方案


看起来您的文件路径中可能缺少斜杠。

尝试改变:

String videopath ="android.resource://" + getPackageName()+ R.raw.test

至:

String videopath ="android.resource://" + getPackageName()+ "/" + R.raw.test

使用它来访问和播放视频如下所示:

VideoView view = (VideoView)findViewById(R.id.videoView);
String videopath ="android.resource://" + getPackageName()+ "/" + R.raw.test
view.setVideoURI(Uri.parse(videopath));
view.start();

笔记

为了从媒体文件(例如视频)中提取帧,Android 提供了 MediaMetadataRetriever 类


推荐阅读