简体   繁体   English

Android MediaRecorder 在某些设备上不工作

[英]Android MediaRecorder not working in some devices

I am running Media Recorder in background service which is running perfect in Samsung 6.0 and above devices, but it is not working in lower devices.我在后台服务中运行 Media Recorder,它在 Samsung 6.0 及更高版本的设备上运行完美,但在较低的设备上无法正常工作。 Throwing the below exception抛出以下异常

stop called in an invalid state: 4

FATAL EXCEPTION: main
Process: com.shoaibnwar.crighter, PID: 28547
java.lang.IllegalStateException
    at android.media.MediaRecorder._stop(Native Method)
    at android.media.MediaRecorder.stop(MediaRecorder.java:946)
    at com.shoaibnwar.crighter.Services.RecordingAudio.stopRecording(RecordingAudio.java:114)
    at com.shoaibnwar.crighter.Services.RecordingAudio.access$000(RecordingAudio.java:24)
    at com.shoaibnwar.crighter.Services.RecordingAudio$1.run(RecordingAudio.java:69)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:7007)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Here is my start recording code这是我开始录制的代码

private void startRecording(File file) {

    if (mediaRecorder != null) {
        mediaRecorder.reset();
        mediaRecorder.release();
    }
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_2_TS);
   // mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
    //mediaRecorder.setOutputFile(file.getAbsolutePath());

    mediaRecorder.setOutputFile(file.getAbsolutePath());
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

   /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
        mediaRecorder.setAudioEncodingBitRate(48000);
    } else {
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mediaRecorder.setAudioEncodingBitRate(64000);
    }
    mediaRecorder.setAudioSamplingRate(16000);*/
    try {
        mediaRecorder.prepare();
        mediaRecorder.start();
    } catch (IOException e) {
        Log.e("giftlist", "io problems while preparing [" +
                file.getAbsolutePath() + "]: " + e.getMessage());
    }
}

and stop recording function is和停止录音功能是

private void stopRecording() {
    if (mediaRecorder != null) {
        mediaRecorder.stop();
        mediaRecorder.release();
        mediaRecorder = null;
    }
}

i am setting the file path like bellow我正在设置文件路径,如下所示

private File getOutputFile() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);

    return new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()
            +File.separator
            +Environment.DIRECTORY_MUSIC
            +File.separator
            + "/CRighterVoice/RECORDING_"
            + dateFormat.format(new Date())
            + ".m4a");


}

I had the same issues.我有同样的问题。 What I got from search is that basic problem is in我从搜索中得到的是基本问题在
mediaRecorder.setAudioSource() because you are using AudioSource.MIC so some device does not support this. mediaRecorder.setAudioSource()因为你使用的是AudioSource.MIC所以有些设备不支持这个。

There are some Audio recording sources we can use while preparing the recording doc here , but sadly in each different device, it might be different.此处准备录音文档时,我们可以使用一些音频录音源,但遗憾的是,在每个不同的设备中,它可能会有所不同。

For some devices, VOICE_CALL works, and for some, others.对于某些设备, VOICE_CALL有效,而对于其他一些设备,则有效。 But at least we can try.但至少我们可以尝试。 So in my case, a device which is running Android below 6, VOICE_CALL is working fine.所以在我的例子中,一个运行 Android 6 以下的设备, VOICE_CALL工作正常。

Another reason is that Sometimes prepare takes some time to complete .另一个原因是有时 prepare 需要一些时间才能完成 I've found this sample project here , which for some reason sleeps for 2 seconds on the UI thread between prepare and start calls of the mediaRecorder .我在这里找到了这个示例项目,出于某种原因,它在mediaRecorder的准备和启动调用之间在 UI 线程上休眠了 2 秒。 It works fine.它工作正常。

Following is the link of the project that record call successfully you see and check how the media source is changed in all devices.以下是成功记录通话的项目链接,您可以看到并检查媒体源在所有设备中的变化情况。

Hope this solve your problem.希望这能解决您的问题。

Try calling试试打电话

mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

before

mediaRecorder.setOutputFile(file.getAbsolutePath());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM