【问题标题】:Playing a video from res/raw on Android Wear在 Android Wear 上播放来自 res/raw 的视频
【发布时间】:2015-02-06 15:06:49
【问题描述】:

更具体:我正在尝试使用 jcodec 的 FrameGrab 从 res/raw 加载视频。

FrameGrab 需要一个 SeekableBiteChannel,所以一个文件可以工作。

如何从资产中获取视频文件作为文件?

我无法将视频放在 sd 卡或类似的东西上,我正在为 Android Wear 开发。

编辑:

String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.hyperlapse2;
mVideoTestUri = Uri.parse(videoPath);
Log.d("VideoPlayer", "Video uri is " + mVideoTestUri);
File file = new File(videoPath);
Log.d("VideoPlayer", "Video file is " + file+", "+file.getName()+", "+file.getAbsolutePath()+", "+file.length());

【问题讨论】:

标签: android file assets wear-os android-videoview


【解决方案1】:

最后我成功了。我不知道这是 Android Wear 特有的东西还是错误,但事实证明

String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
File file = new File(path);

不授予对 Android Wear 设备上文件的访问权限。

相反,必须先将文件转换为临时文件:

InputStream ins = MainActivityBackup.this.getResources().openRawResource (R.raw.hyperlapse2);
File tmpFile = null;
OutputStream output;

try {
    tmpFile = File.createTempFile("video","mov");
    output = new FileOutputStream(tmpFile);

    final byte[] buffer = new byte[102400];
    int read;

    while ((read = ins.read(buffer)) != -1) {
        output.write(buffer, 0, read);
    }
    output.flush();
    output.close();
    ins.close();
} catch (IOException e) {
    e.printStackTrace();
}

然后就可以加载到videoView中了

mVideoView.setVideoPath(tmpFile.getPath());

如果您使用自己的视频解码器或 ffmpeg 或 vitamio 等库,因为 Android Wear 尚不支持原生视频播放。

【讨论】:

    【解决方案2】:

    将视频放在资源目录下名为 raw 的文件夹下。

    String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
    File file = new File(path);
    
    VideoView view = (VideoView)findViewById(R.id.videoView);
    view.setVideoURI(Uri.parse(path));
    view.start();
    

    【讨论】:

    • 也许刷新页面,它就在那里
    • 我测试了相同的代码,即使文件长度不正确,文件在视频视图中也能正常播放。
    • 你得到的文件长度是 0 还是一些不同的长度?
    • 我的文件长度返回 0。文件名返回为在资源创建时分配的随机 int。
    • 很奇怪,我尝试用 jcodec 读取它,但它一直给我一个 java.io.FileNotFoundException: /2130968577: open failed: ENOENT (No such file or directory)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多