且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-20 23:49:22

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