How to know when MediaRecorder has finished writing data to file

被刻印的时光 ゝ 提交于 2019-12-03 12:13:23

问题


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 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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


I had the same issue in xamarin and tried all these solutions (except the Ash solution). What I finally did to solve the issue was to call the below function in the releaseMediaRecorder() function before Reset and before Release is called.

mediaRecorder.setOutputFile("some new file")


来源:https://stackoverflow.com/questions/7418446/how-to-know-when-mediarecorder-has-finished-writing-data-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!