尝试在android studio中使用媒体录制器录制和播放音频

7xllpg7q  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(219)

每当我按下录音机上的“停止”按钮,应用程序就会崩溃。我可以在下面链接错误日志:

2020-11-15 18:58:46.019 14879-14879/com.example.recordertry2 E/MediaRecorder: stop called in an invalid state: 4
2020-11-15 18:58:46.019 14879-14879/com.example.recordertry2 D/AndroidRuntime: Shutting down VM
2020-11-15 18:58:46.021 14879-14879/com.example.recordertry2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.recordertry2, PID: 14879
    java.lang.IllegalStateException
        at android.media.MediaRecorder._stop(Native Method)
        at android.media.MediaRecorder.stop(MediaRecorder.java:1440)
        at com.example.recordertry2.MainActivity.stopRecord(MainActivity.java:167)
        at com.example.recordertry2.MainActivity$2.onClick(MainActivity.java:64)
        at android.view.View.performClick(View.java:7869)
        at android.widget.TextView.performClick(TextView.java:14958)
        at android.view.View.performClickInternal(View.java:7838)
        at android.view.View.access$3600(View.java:886)
        at android.view.View$PerformClick.run(View.java:29362)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8125)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

我希望我的代码记录在m4a格式的音频,然后存储在手机的存储。我也可以包括下面的录音代码:

public void startRecord() throws Exception {

        if (record!=null){
            record.release();
        }
        File fileOut = new File(FILE);
        if (fileOut!=null){
            fileOut.delete(); //overwrite existing file
        }
        record = new MediaRecorder();
        record.setAudioSource(MediaRecorder.AudioSource.MIC);
        record.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        record.setAudioSamplingRate(44100);
        record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        record.setOutputFile(FILE);
        Log.i("MyApp", "Sup Sucka");
        try {
            record.prepare();
            record.start();
        }
        catch (IOException e) {
            throw new RuntimeException("Exception preparing recorder", e);
        }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题