如何解决E / MediaRecorder:在无效状态下停止调用:4

时间:2019-04-14 06:27:11

标签: android audio record

我正在尝试录制音频并将其存储。问题是当我录制音频时,它开始了,但是在停止音频时,我得到了错误:

  

E / MediaRecorder:在无效状态下停止调用:4

我的代码是:

     public void  RecordVoice(){
        recordVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkPermissionFromDevice()){
                    pathSave  = Environment.getExternalStorageDirectory().getAbsolutePath() + " /" + UUID.randomUUID().toString() + "_audio_record.3gp";
                    setupMediaRecorder();
                    try {
                        mediaRecorder.prepare();
                        mediaRecorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    recordVoice.setVisibility(View.GONE);
                    recordVoiceStop.setVisibility(View.VISIBLE);
                    Toast.makeText(getApplicationContext(), "Recording..",Toast.LENGTH_SHORT).show();
                } else {
                    requestPermissionForAudio();
                }
            }
        });

        recordVoiceStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mediaRecorder != null){
                    mediaRecorder.stop();
                    mediaRecorder.release();
                }
                recordVoice.setVisibility(View.VISIBLE);
                recordVoiceStop.setVisibility(View.GONE);

            }
        });
}
 private void setupMediaRecorder() {
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    mediaRecorder.setOutputFile(pathSave);
}

private void requestPermissionForAudio() {
    ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_RECORD_VOICE);
}

private boolean checkPermissionFromDevice() {
    int writeExternalStorageDevice = ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int recordAudioResult = ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO);
    return  writeExternalStorageDevice == PackageManager.PERMISSION_GRANTED && recordAudioResult == PackageManager.PERMISSION_GRANTED;
}

PS:我已允许进行manifect。有人可以帮我解决这个问题吗?任何建议都是最欢迎的。预先感谢。

0 个答案:

没有答案