gpt4 book ai didi

java - 当记录器应该停止时应用程序崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:26 25 4
gpt4 key购买 nike

我在 google play 上上传了一个应用程序。 This is the link.

尚未在许多设备上进行测试,因此错误很常见。今天一位用户给我发消息说,如果切换按钮打开并且按钮只被按下而不是按住,应用程序就会崩溃。

这是他发给我的 logcat 文件:

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)

引用:

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

我在我的手机上试过了,我只是触摸了按钮而不是按住它,它工作得很好,我不知道为什么他的手机会出现这种情况。

这是 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 方法:

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 甚至还没有完全启动。如果是这种情况,我该如何解决?如果不是这样,那又是怎么回事呢?为什么这样的错误会出现在另一部手机上而不是我的手机上?

最佳答案

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

public void stop ()

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调用中添加一个try catch

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

关于java - 当记录器应该停止时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18430090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com