Skip to content Skip to sidebar Skip to footer

Detect When Video File Has Been Really Written?

The cwac-camera library has event hooks that are called before a photo is saved: @Override public void saveImage(PictureTransaction xact, byte[] image) {} Have I just overlooked t

Solution 1:

FileObserver can be used to detect when a file has been finally written. It can be hooked into the getVideoDirectory() override in CameraHost:

@Override protected File getVideoDirectory() {
    fileObserver = new FileObserver(videoDirectory.getAbsolutePath(), FileObserver.CLOSE_WRITE) {
        @Override publicvoidonEvent(intevent, String videoPath) {
            System.out.println("**** CameraHost: FileObserver: onEvent: event = " + event + ", videoPath = " + videoPath);

            if (fileObserver != null && videoPath != null && event == FileObserver.CLOSE_WRITE) { // do not process directory
                System.out.println("**** CameraHost: write closed: videoPath = " + videoPath);

                // fileObserver.stopWatching(); // TODO this needs to be closed somewhere// fileObserver = null;

                raiseVideo(videoPath);
            }
        }
    };
    fileObserver.startWatching();

    return videoDirectory;
}

FileObserver has the limitation that it only works accurately when stopRecording() is explicitly called. It is raised too late if the recording is terminated because a pre-defined files size or duration has been reached (see also https://github.com/commonsguy/cwac-camera/issues/242 ).

Using recorder.setOnInfoListener() does not work, because it may report finalization of a file before it has been actually written to disk.

Post a Comment for "Detect When Video File Has Been Really Written?"