请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

android - MediaRecorder 在启动时崩溃

[复制链接]
菜鸟教程小白 发表于 2022-11-6 16:17:18 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我有这个代码:

        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

请帮忙。



Best Answer-推荐答案


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

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

让你的媒体记录器成为你类(class)的一个领域。并将您的 mStartRecording bool 变量正确初始化为 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

关于android - MediaRecorder 在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10340400/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝0

帖子790776

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap