How to know when MediaRecorder has finished writing data to file

前端 未结 6 963
迷失自我
迷失自我 2021-02-05 08:01

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

Everything works fine, but the main

相关标签:
6条回答
  • 2021-02-05 08:18

    In my tests irrespective of the size of the recording mediaRecorder.stop() is a blocking method that only returns after the file has been completely written and closed by the media recorder.

    So JPMs answer is actually correct.

    You can verify this by calling File.length() immediately after stop(). You will find that the output file length is the final length of the file at this point. In other words media recorder does not write anything further to the file after stop() has returned.

    0 讨论(0)
  • 2021-02-05 08:21

    Apparently there is no way to detect when the recording has stopped in Media player, but there is a stop() that you can override if you create a custom class that implements MediaRecorder. here I would do something like this:

    public class MyRecorder implements MediaRecorder {
        public boolean stopped;
    
        .... implement all the methods that MediaRecorder has making 
             sure to call super for each method.
    
        @Override
        public void myStop() {
            this.stopped = true;
            super.stop(); 
        }
    }
    

    Then you can access the boolean to see if it has stopped recording.

    0 讨论(0)
  • 2021-02-05 08:26

    The FileObserver class suits your needs perfectly. Here is the documentation. It's 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
        }
    }
    

    Don't forget to call stopWatching().

    0 讨论(0)
  • 2021-02-05 08:34

    I haven't tried this myself but this might work:

    public void release () Since: API Level 1

    Releases resources associated with this MediaRecorder object. It is good practice to call this method when you're done using the MediaRecorder.

    If it does what it says, then I guess if you call this and after this method returns you know the file is ready.

    0 讨论(0)
  • 2021-02-05 08:35

    A dirty way would be to check the lastModified() value of the File and open the VideoView if the File wasn't modified for 2 seconds.

    0 讨论(0)
  • 2021-02-05 08:36

    We solved similar problem with the following algo:

    while (file not complete)
        sleep for 1 sec
        read the fourth byte of the file
        if it is not 0 (contains 'f' of the 'ftyp' header) then
            file is complete, break
    

    The key point is that MediaRecorder writes the ftyp box at the very last moment. If it is in place, then the file is complete.

    0 讨论(0)
提交回复
热议问题