【问题标题】:The application crashes when the media recorder starts?媒体记录器启动时应用程序崩溃?
【发布时间】:2022-04-26 18:41:24
【问题描述】:

我正在使用媒体记录器在 android 中记录通话。有时 mediaRecorder 正在录制音频,有时它不录制音频。当我们播放录制的音频时,错误显示为“播放器不支持这种类型的音频文件”。

以下代码用于录制音频

 try {
                    f1 = File.createTempFile("Sound", ".mp3", dir);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fileName = f1.getName();
                mediaRecorder = new MediaRecorder();
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                mediaRecorder.setOutputFile(f1.getAbsolutePath());
                try {
                    mediaRecorder.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mediaRecorder.start();
                recordstarted = true;

我的 logcat 错误如下所示

 E/MediaRecorder: start failed: -38
07-03 11:10:23.718 8558-8558/com.seyali.callLog E/UncaughtException: java.lang.RuntimeException: Unable to start receiver com.seyali.callLog.receiver.CallReceiver: java.lang.IllegalStateException
                                                                         at android.app.ActivityThread.handleReceiver(ActivityThread.java:2593)
                                                                         at android.app.ActivityThread.access$1700(ActivityThread.java:139)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:149)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                         at dalvik.system.NativeStart.main(Native Method)
                                                                      Caused by: java.lang.IllegalStateException
                                                                         at android.media.MediaRecorder.start(Native Method)
                                                                         at com.seyali.callLog.receiver.CallReceiver.onReceive(CallReceiver.java:175)
                                                                         at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
                                                                         at android.app.ActivityThread.access$1700(ActivityThread.java:139) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:149) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5257) 
                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
                                                                         at dalvik.system.NativeStart.main(Native Method) 

请帮我解决这个问题。

【问题讨论】:

    标签: android android-mediarecorder


    【解决方案1】:

    非法状态意味着您的媒体记录器尚未准备好进行记录。可能有几个原因。首先,确保您可以访问要播放的文件。如果没问题,请查看您应该播放哪个状态的媒体记录器图表。 https://developer.android.com/reference/android/media/MediaRecorder.html


    另外,如果 mediaRecorder.prepare() 会发生什么?您应该将mediaRecorder.start() 放入try 块中。

    【讨论】:

    • 存储在外部存储中的录制文件。当我点击播放“播放器不支持此音频”时出现错误
    • 我也将 mediaRecorder.start() 插入到 try 块中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多