【问题标题】:MediaRecorder crashes on startMediaRecorder 在启动时崩溃
【发布时间】:2012-04-26 19:49:41
【问题描述】:

我搜索了很多主题,但没有直接的答案。

我有这个代码:

        recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

    recorder.setOutputFile(mFileName);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    if(!mStartRecording)
    {
        btn.setText("Stop Recording");
        try {
            recorder.prepare();

        }  catch (IOException e) {
            e.printStackTrace();
        }
        recorder.start();
        mStartRecording = true;
    }
    else
    {
        btn.setText("Start Recording");
        mStartRecording = false;
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
    }

我已经添加了:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" /> 
<uses-permission android:name="android.permission.RECORD_AUDIO" />

(在某处看到添加 STORAGE 解决了它,对我没有好处)

我正在 API 级别 7 (Android 2.1) 上进行开发

堆栈跟踪显示“启动失败” 堆栈跟踪:

    04-26 19:27:41.955: D/dalvikvm(890): GC freed 809 objects / 58272 bytes in 433ms
04-26 19:27:44.772: D/dalvikvm(890): GC freed 95 objects / 3936 bytes in 371ms
04-26 19:28:54.973: E/MediaRecorder(890): start failed: -1
04-26 19:28:54.993: D/AndroidRuntime(890): Shutting down VM
04-26 19:28:54.993: W/dalvikvm(890): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-26 19:28:54.993: E/AndroidRuntime(890): Uncaught handler: thread main exiting due to uncaught exception
04-26 19:28:55.105: E/AndroidRuntime(890): java.lang.IllegalStateException: Could not execute method of the activity
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.View$1.onClick(View.java:2031)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.View.performClick(View.java:2364)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.View.onTouchEvent(View.java:4179)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.View.dispatchTouchEvent(View.java:3709)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-26 19:28:55.105: E/AndroidRuntime(890):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
04-26 19:28:55.105: E/AndroidRuntime(890):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
04-26 19:28:55.105: E/AndroidRuntime(890):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.os.Looper.loop(Looper.java:123)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-26 19:28:55.105: E/AndroidRuntime(890):  at java.lang.reflect.Method.invokeNative(Native Method)
04-26 19:28:55.105: E/AndroidRuntime(890):  at java.lang.reflect.Method.invoke(Method.java:521)
04-26 19:28:55.105: E/AndroidRuntime(890):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-26 19:28:55.105: E/AndroidRuntime(890):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-26 19:28:55.105: E/AndroidRuntime(890):  at dalvik.system.NativeStart.main(Native Method)
04-26 19:28:55.105: E/AndroidRuntime(890): Caused by: java.lang.reflect.InvocationTargetException
04-26 19:28:55.105: E/AndroidRuntime(890):  at shibby.whisper.WhisperGameActivity.recordAudio(WhisperGameActivity.java:94)
04-26 19:28:55.105: E/AndroidRuntime(890):  at java.lang.reflect.Method.invokeNative(Native Method)
04-26 19:28:55.105: E/AndroidRuntime(890):  at java.lang.reflect.Method.invoke(Method.java:521)
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.view.View$1.onClick(View.java:2026)
04-26 19:28:55.105: E/AndroidRuntime(890):  ... 21 more
04-26 19:28:55.105: E/AndroidRuntime(890): Caused by: java.lang.RuntimeException: start failed.
04-26 19:28:55.105: E/AndroidRuntime(890):  at android.media.MediaRecorder.start(Native Method)
04-26 19:28:55.105: E/AndroidRuntime(890):  ... 25 more
04-26 19:28:55.223: I/dalvikvm(890): threadid=7: reacting to signal 3
04-26 19:28:55.335: I/dalvikvm(890): Wrote stack trace to '/data/anr/traces.txt'
04-26 19:28:57.123: I/Process(890): Sending signal. PID: 890 SIG: 9

请帮忙。

【问题讨论】:

  • 请提供所有堆栈跟踪
  • 你可以在 /data/anr/traces.txt 中找到什么
  • 这可能看起来很愚蠢,但我不知道在哪里可以找到该文件。是在工作区吗?
  • 在控制台中:adb shell pull /data/anr/traces.txt。然后您将在当前目录中找到该文件。
  • 我通过 CMD 试过了,不工作。抱歉,我是新手。

标签: android android-emulator mediarecorder android-mediarecorder


【解决方案1】:

好的,我明白了。我猜你将 mStartRecording 初始化为 true。

因此,您的 if 将进入 else 块。在其中,您停止了一个全新的 MediaRecorder 实例,而状态图不允许这样做。

让你的媒体记录器成为你班级的一个领域。并将您的 mStartRecording 布尔变量正确初始化为 false。仅当您的字段为空时才重新实例化您的媒体记录器。

if( recorder == null ) {
   recorder = new MediaRecorder();
   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
   recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

   recorder.setOutputFile(mFileName);
   recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}//if
if(!mStartRecording) {
    btn.setText("Stop Recording");
    try {
        recorder.prepare();
        recorder.start();
        mStartRecording = true;
    }  catch (IOException e) {
        e.printStackTrace();
    }//catch
} else {
    btn.setText("Start Recording");
    mStartRecording = false;
    recorder.stop();
    recorder.reset();
    recorder.release();
    recorder = null;
}//else

【讨论】:

  • 对不起,我没有提到,我认为这是给定的。 mStartRecording 是假的。
  • 尝试添加一些跟踪 (Log.d) 语句以查看您的程序做了什么。它是否在 if 或 else 块中崩溃
  • mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); mFileName += "/audiorecordtest4.3gp";在 onCreated 方法中调用它
  • 在设备上尝试,尝试创建文件并尝试不同的路径。
  • 如果你愿意,我可以聊聊这个问题
【解决方案2】:

尝试将start 函数与prepare 函数放在同一块中。也许有一个异常阻止执行准备并直接开始,从而导致IllegalStateException

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());


    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

【讨论】:

  • 你的 setoutputfile 是在 setaudioencoder 之后吗?尝试改变它!
  • 如果您的 mstartrecording 初始化为 true,则可能会发生错误。是否初始化为假?
  • 你在模拟器上做这个吗?可能是sd卡问题。你在设备上试过了吗?
  • 我在模拟器上运行。我已将其设置为启用录音。虽然他们的文档提到模拟器不能这样做,但我读过它确实可以。我已经用 100mb 的 SD 设置了模拟器。
【解决方案3】:

我正在使用以下代码,非常适合我..

protected void startRecording() {
    // TODO Auto-generated method stub
    i++;
     mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
     mFileName += "/audiorecordtest"+i+".3gp";
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(mFileName);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

      try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "IllegalStateException called", Toast.LENGTH_LONG).show();


    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "prepare() failed", Toast.LENGTH_LONG).show();

    }

      recorder.start();
}

 private void stopRecording() {
     recorder.stop();
     recorder.release();
     recorder = null;
    }

【讨论】:

    【解决方案4】:

    这些方法必须安排其运行的顺序。这里:

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(mFileName);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    

    【讨论】:

      【解决方案5】:

      尝试仅在准备好后启动您的录音机:

          try {
              recorder.prepare();
              recorder.start();
              mStartRecording = true;
          }  catch (IOException e) {
              Log.e( LOG_TAG, "Error when preparing or starting recorder", e);
          }
      

      【讨论】:

        【解决方案6】:

        我遇到了同样的问题。这是因为我在为录制的音频设置文件名时遗漏了一个斜线。

        改变

        this.fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        this.fileName += "yourfilename.3gp";
        

        this.fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        this.fileName += "/yourfilename.3gp";
        

        【讨论】:

          【解决方案7】:

          我遇到了同样的问题,因为我的 SurfaceView 已变得不可见。所以让它可见

            mSurfaceView.setVisibility(View.VISIBLE);
          

          【讨论】:

            【解决方案8】:

            必须先调用setOutputFile(),再调用其他方法。

            你必须先创建文件。

            【讨论】:

            • 这是不正确的。根据documentation,首先要选择源类型,然后决定输出格式,然后设置剩下的属性。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-22
            • 2016-12-07
            • 1970-01-01
            相关资源
            最近更新 更多