记录仪应停止时,应用程序崩溃 [英] App crashes when recorder is supposed to stop

查看:42
本文介绍了记录仪应停止时,应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Play上上传了一个应用.这是链接.

I have an app uploaded on google play. This is the link.

它尚未在许多设备上经过测试,因此错误非常普遍.一位用户今天告诉我说,如果将切换按钮设为ON并且仅按下而不是按住该按钮,则应用程序将崩溃.

It hasn't been tested on many devices yet so errors are pretty common. A user messaged me today saying that the app crashes if the togglebutton is turned ON and the button is only pressed, not held.

这是他发送给我的logcat文件:

This is the logcat file that he sent me:

E/MessageQueue-JNI(31135): java.lang.RuntimeException: stop failed.
E/MessageQueue-JNI(31135): at android.media.MediaRecorder.stop(Native Method)
E/MessageQueue-JNI(31135): at com.whizzappseasyvoicenotepad.MainActivity.stopRecording(MainActivity.java:183)

报价:

应用程序并不总是崩溃.有时确实如此,有时却没有.仅当切换按钮为ON时才会发生.如果我触摸并按住该按钮,则效果很好,但如果我仅触摸一下片刻,它就会崩溃.我正在使用Xperia S 4.1.2

App doesn't always crash. Sometimes it does, sometimes it doesn't. It only happens when the togglebutton is ON. If I touch and hold the button it works fine but if I only touch it for a moment it crashes. I'm using Xperia S 4.1.2

我在手机上尝试过此操作,只触摸了按钮而不是按住它,它工作得非常好,我不知道为什么在他的手机上会发生这种情况.

I tried this on my phone, I only touched the button instead of holding it and it worked perfectly fine, I don't know why this is happening on his phone.

这是onTouchListener的代码:

This is the code for onTouchListener:

recBtn.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    startRecording();
                }
                else if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    stopRecording();
                    nameAlert();
                }
            return true;
            }
        });

logcat说调用stopRecording时会发生问题,所以这是stopRecording方法:

And the logcat says the problem occurs when stopRecording is called, so here is the stopRecording method:

public void stopRecording() {
    final ImageButton recBtn = (ImageButton) findViewById(com.whizzappseasyvoicenotepad.R.id.recButton);
    final ToggleButton tBtn = (ToggleButton) findViewById(R.id.tBtn1);
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release(); 
        recorder = null;

        recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
        stopTimer();
        tBtn.setEnabled(true);
    }
}

我猜测问题是他只触摸了一下按钮,因此在完全调用startRecording之前,已经调用了stopRecoring,因此它崩溃了,因为还没有完全启动startRecording.如果是这种情况,我该如何解决?如果不是这种情况,那怎么了?为什么这样的错误会出现在另一部手机上却不会出现在我的手机上?

I'm guessing that the problem is that he only touches the button for a moment, so before startRecording is completely called, the stopRecoring is already called so it crashes because startRecording wasn't even completely intiated yet. If that's the case, how can I fix it? If it isn't the case, what's wrong then? And why would an error like this appear on another phone but not mine?

推荐答案

根据文档,这是正常行为:

According to documentation, this is the normal behavior:

公共无效停止()

在API级别1中添加停止记录.在start()之后调用此函数.一次录制已停止,您将不得不重新配置它,就像它已经刚刚建造.请注意,RuntimeException是有意的如果没有有效的音频/视频数据,则抛出该应用程序在调用stop()时收到.如果调用stop(),则会发生这种情况在start()之后立即使用.故障使应用程序采取采取相应措施清理输出文件(删除输出例如文件),因为输出文件的构造不正确发生这种情况时.

Added in API level 1 Stops recording. Call this after start(). Once recording is stopped, you will have to configure it again as if it has just been constructed. Note that a RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start(). The failure lets the application take action accordingly to clean up the output file (delete the output file, for instance), since the output file is not properly constructed when this happens.

因此,您只需将尝试捕获添加到您的 stop 调用

So you can just add a try catch to your stop call

if (null != recorder) {
   try{     
      recorder.stop();
   }catch(RuntimeException ex){
   //Ignore
   }
   ...
}

这篇关于记录仪应停止时,应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆