Stop Android Media Recorder Exception

I am recording both video and audio from an android recorder.

I check the length of the clip, if less than 2 seconds remove it ...
But the problem is that if I check the length of the file from the file path, the MediaRecorder throws an exception every time after the exception is thrown, even when I record the sound in 10 seconds?
But when I comment out the code to check the duration of the generated video, it looks great ...
Below is my code

if (prMediaRecorder != null) {
    try {
        prMediaRecorder.stop();
        timer.cancel();
        PathNameArray.add(prRecordedFile.getPath());
        Log.e("No Exception", "File Added and Saved");

        ////////////// Check Length and Delete File
        if (prRecordedFile != null) {
            if (MediaPlayer.create(getApplicationContext(), Uri.fromFile(new File(
                            prRecordedFile.getPath()))).getDuration() <= 2000) {

                File file = new File(prRecordedFile.getPath());
                boolean deleted = file.delete();
                Toast.makeText(getApplicationContext(),
                        "Video Clip Length Too Short, Clip Not Added",
                        Toast.LENGTH_SHORT).show();

                PathNameArray.remove(PathNameArray.size() - 1);
            }
        }

    } catch (RuntimeException e) {
        Toast.makeText(getApplicationContext(),
                "Corrupt Clip, Clip Not Added",
                Toast.LENGTH_SHORT).show();
        File file = new File(prRecordedFile.getPath());
        boolean deleted = file.delete();
        timer.cancel();
        Log.e("Exception Caught", "File Not Added");

    } finally {
        try {
            prCamera.reconnect();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

prMediaRecorder = new MediaRecorder();
MarkerName = null;

      

Please help me, is there something wrong with my code or what?

+3


source to share


1 answer


I fixed the problem,
The problem was how I was getting the duration, from MediaPlayer


I used this code instead and it was fixed ...



//////////////// Check Length and Delete File
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(prRecordedFile.getPath());
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong(time);

      

+1


source







All Articles