Android的媒体录像机停止异常

Android的媒体录像机停止异常

本文介绍了Android的媒体录像机停止异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记录从android媒体录像机视频和音频...结果
我检查的短片长度,如果小于2秒,删除...结果
但问题是,如果我从文件路径检查文件的时间,MediaRecorder抛出异常后,每次一旦抛出异常,甚至当我10秒的录音?结果
但是,当我评论了code,检查视频的创建时间,它worsk罚款...结果
以下是我的code

I am recording both video and audio from the android media recorder...
I am checking the clip length, if less then 2 sec, delete it...
But the problem is if i check the duration of file from file path, MediaRecorder throws exception everytime after once exception is thrown, even when i record audio of 10 seconds?
But when i comment the code to check the duration of video created, it worsk fine...
Following 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;

请帮我了,有毛病我code还是什么?

Please Help me out, something wrong with my code or what ?

推荐答案

我固定的问题,结果
问题是我居然也得到了持续的方式,从的MediaPlayer 结果
相反,我用这个code和它是固定...

I fixed the issue,
The problem was the way i was getting the duration, from MediaPlayer
Instead i used this code 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);

这篇关于Android的媒体录像机停止异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:53