MediaRecorder记录并制作文件但抛出异常

发布于 2024-11-27 06:42:50 字数 2160 浏览 1 评论 0原文

我的 MediaRecorder 有奇怪的问题。它记录声音,用录音制作文件。但是当我想使用 stop() 方法停止录制时,它会抛出 IllegalStateException。通常我使用 setMaxDuration() 方法,所以通常我使用 OnInfoListener 结束录制并且它工作正常。但我还想在 ImageView 的 OnTouchListener 中停止 MediaRecorder。我的代码在这里:

private static String OUTPUT_FILE;

private void prepareRecording() throws Exception {      
    OUTPUT_FILE = "/sdcard/temp.3gpp";      
    File outFile = new File(OUTPUT_FILE);

    if (outFile.exists())
        outFile.delete();       

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(OUTPUT_FILE);
    recorder.setMaxDuration(10000);
    try {
        recorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }       
    recorder.start();       
}

private void startRecording() {  
     ImageView image = (ImageView) this.findViewById(R.id.image);

     image.post(new Runnable() { 
     @Override
     public void run() {
        try {
                prepareRecording();
            } catch (Exception e) {
                e.printStackTrace();                    
            }   

        image.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (recorder != null)
                    recorder.stop();
                return true;
            }
        });

        recorder.setOnInfoListener(new OnInfoListener() {

            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {                     
                if (recorder != null && what == 800){
                    recorder.stop();                            
                }                       
            }
        });     

    }});
    }

当我触摸 ImageView 时,我在代码 recorder.stop() 行出现致命异常 java.lang.IllegalStateException 。我测试了我的代码,发现 MediaRecorder 似乎没有启动。因此,我在 recorder.stop() 处遇到 IllegalStateException。但是当录音停止时,我可以在我的SD卡上找到录音文件。我的代码有什么问题吗?

我还将 AnimationDrawable 连接到我的图像。它工作正常。

I have strange problem with my MediaRecorder. It records voice, make a file with recording. But when I want to stop recording using stop() method, it throws IllegalStateException. Generally I have used setMaxDuration() method, so usually I have ending recording using OnInfoListener and it works properly. But I want also to stop MediaRecorder in the OnTouchListener of the ImageView. My code is here:

private static String OUTPUT_FILE;

private void prepareRecording() throws Exception {      
    OUTPUT_FILE = "/sdcard/temp.3gpp";      
    File outFile = new File(OUTPUT_FILE);

    if (outFile.exists())
        outFile.delete();       

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(OUTPUT_FILE);
    recorder.setMaxDuration(10000);
    try {
        recorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }       
    recorder.start();       
}

private void startRecording() {  
     ImageView image = (ImageView) this.findViewById(R.id.image);

     image.post(new Runnable() { 
     @Override
     public void run() {
        try {
                prepareRecording();
            } catch (Exception e) {
                e.printStackTrace();                    
            }   

        image.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (recorder != null)
                    recorder.stop();
                return true;
            }
        });

        recorder.setOnInfoListener(new OnInfoListener() {

            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {                     
                if (recorder != null && what == 800){
                    recorder.stop();                            
                }                       
            }
        });     

    }});
    }

When I have touched the ImageView I have FATAL EXCEPTION java.lang.IllegalStateException at the line with code recorder.stop(). I have tested my code and I have realized that MediaRecorder seems to be not started. Therefore I have IllegalStateException at recorder.stop(). But when recording stops, I can find on my sdcard recording file. Whats wrong with my code?

I have also AnimationDrawable connected to the my image. It works properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2024-12-04 06:42:50

我已经解决了我的问题。需要在onTouch方法中启动新线程以避免异常。

image.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        Thread thread = new Thread(){
        public void run(){                              
                if (recorder != null)                                   
                    recorder.stop();
        }
    };
    thread.start();                             
    return false;
    }
});

I have resolved my problem. It is necessary to start new thread in onTouch method to avoid eceptions.

image.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        Thread thread = new Thread(){
        public void run(){                              
                if (recorder != null)                                   
                    recorder.stop();
        }
    };
    thread.start();                             
    return false;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文