如何知道什么时候MediaRecorder已完成将数据写入到文件什么时候、文件、数据、MediaRecorder

2023-09-05 06:49:17 作者:孤独酒馆

我们正在使用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 作为参数。

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() ..