如何知道什么时候MediaRecorder已完成将数据写入到文件 [英] How to know when MediaRecorder has finished writing data to file

查看:739
本文介绍了如何知道什么时候MediaRecorder已完成将数据写入到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用MediaRecorder录制视频做在实际拍摄前使用setOutputFile()外部存储的文件。

We're using MediaRecorder to record video to a file on the external storage using setOutputFile() before doing the actual recording.

一切工作正常,但主要的问题是,一旦完成录音后,我们要开始播放录像回来VideoView。

Everything works fine, but the main issue is that as soon as the recording is done, we want to start playing the recorded video back in a VideoView.

如何知道什么时候该文件已准备好被读取并播放?

How to know when the file is ready to be read and played back?

感谢。

推荐答案

FileObserver 类适合您的需求完美。 这里是文档。它易于使用。当观察到的文件写入后关闭时,的onEvent 回调调用 CLOSE_WRITE 作为参数。<​​/ P>

The FileObserver class suits your needs perfectly. Here is the documentation. Its easy to use. When a observed file is closed after writing, the onEvent callback is called with CLOSE_WRITE as the parameter.

MyFileObserver fb = new MyFileObserver(mediaFile_path, FileObserver.CLOSE_WRITE);
fb.startWatching();

class MyFileObserver extends FileObserver {

    public MyFileObserver (String path, int mask) {
        super(path, mask);
    }

    public void onEvent(int event, String path) {
        // start playing
    }
}

不要忘记调用 stopWatching() ..

这篇关于如何知道什么时候MediaRecorder已完成将数据写入到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆