媒体记录器停止在无效状态

时间:2018-09-26 14:50:10

标签: android mobile mediarecorder

我正在尝试创建一个简单的应用程序来记录音频。我正在使用标准的MediaRecorder来实现这一目标。这是我到目前为止的内容:

  1. 我已使用此方法成功启动mediaRecorder

    public void startRecording(String filePath) throws IOException {
    
    String state = android.os.Environment.getExternalStorageState();
    if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
        throw new IOException("SD Card is not mounted.  It is " + state + ".");
    }
    
    // make sure the directory we plan to store the recording in exists
    File directory = new File(filePath).getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Path to file could not be created.");
    }
    
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC );
    
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(filePath);
    
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);
    
    try {
        recorder.prepare();
    
    } catch (Exception e) {
        Timber.e("Error while prepareing media recorder " + e.getMessage());
    }
    
    try {
    
        recorder.start();
        SharedPrefs.setIsRecording(true);
        Timber.d("Started to record " + SharedPrefs.isRecording());
    
    } catch (Exception e) {
        Timber.e("Error while starting the media recorder " + e.getMessage());
    } }
    

这很好,我可以在本地看到该文件是在指定文件夹中创建的。

  1. 现在这是我用来停止录制的内容:

    public void stopRecording() {
    
    if (recorder == null) {
            Timber.e("recorder is null");
        }
    
    if (SharedPrefs.isRecording()) {
    
        try {
            recorder.stop();
    
        } catch (Exception r) {
            Timber.e("Error while attempting to stop " + r.getMessage());
        }
    
        try {
            recorder.release();
    
        } catch (Exception e) {
            Timber.e("Error while attempting to release " + e.getMessage());
        }
    
        Timber.e("We stopped recording");
        SharedPrefs.setIsRecording(false);
    
    } else {
        Timber.e("Attempting to stop recording while not recording ");
    } }
    

    现在,当调用stopRecording()时,这是我得到的错误消息:

     E/MediaRecorder: stop called in an invalid state:1
    

    AudioRecorder:尝试停止为空时出错

注意:当我在本地转到文件所在的文件夹并刷新它时,我看到文件的大小正在增加。

谢谢!

0 个答案:

没有答案