简体   繁体   English

在Android上录音时出错

[英]An error upon audio recording on Android

My application starts a thread A to save some data. 我的应用程序启动thread A来保存一些数据。 In this thread I call the function startRecording(audioFile.getAbsolutePath()); 在此线程中,我调用函数startRecording(audioFile.getAbsolutePath());

But I get the following error: 但是我收到以下错误:

start called in an invalid state: 16; at android.media.MediaRecorder.start(Native Method)

This error does not occur everytime, but sometimes I get this error report from my application. 并非每次都会发生此错误,但是有时我会从应用程序中获取此错误报告。

Below, there's my code. 下面是我的代码。

public void startRecording(String outputPath) {
    if (recorder == null) {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        recorder.setAudioSamplingRate(RECORDER_SAMPLERATE_22050);
        recorder.setOutputFile(outputPath);
        try {
            recorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    recorder.start();
    SampleRecordThread thread = new SampleRecordThread(outputPath);
    thread.start();

}

private class SampleRecordThread extends Thread {
    private volatile boolean running = true;

    public void exit() {
        running = false;
    }

    @Override
public void run() {
    while (running) {
        try {
            Thread.sleep(10 * 1000);//to record 10 seconds sound
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;

        // upload the data to cloud
        if (isWifiActive && isInstallationSaved) {
                    .....
                    record.saveInBackground();
            }

        break;
    }
}

I think you try recording while previous recorder not stopped and released You can use my class 我认为您尝试在以前的录像机未停止和释放的情况下进行录像可以使用我的课程

public class AudioRecordManager {

public interface OnAudioRecordCallback {

    void onComplete(String path);

    void onFailed(int code);

}

private OnAudioRecordCallback onAudioRecordCallback;

public void setOnAudioRecordCallback(OnAudioRecordCallback onAudioRecordCallback) {
    this.onAudioRecordCallback = onAudioRecordCallback;
}

private MediaRecorder myRecorder;
private String outputFile;
private AudioRecordThread audioRecordThread;

public AudioRecordManager() {
    audioRecordThread = new AudioRecordThread();
}

private AudioRecordThread audioRecordThread;

public void startRecord() {
    if(audioRecordThread == null || !audioRecordThread.isRunning()){
        new Thread(audioRecordThread).start();
    }
}

public void stopRecord() {
    if(audioRecordThread!=null && audioRecordThread.isRunning()) {
        audioRecordThread.stopRecording();
    }
}

class AudioRecordThread implements Runnable {

    private boolean isRunning;

    private boolean isStop;

    private long startTime;

    private void startRecord() {
        isRunning = true;
        isStop = false;
        File folder = new File(Content.AUDIO_DIR);
        if (!folder.exists()) {
            boolean b = folder.mkdirs();
        }
        startTime = System.currentTimeMillis();
        outputFile = folder.getPath() + "/rec_" + startTime + ".mp3";

        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myRecorder.setOutputFile(outputFile);
        try {
            myRecorder.prepare();
            myRecorder.start();

            startTime = System.currentTimeMillis();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stopRecord() {

        final long stopTime = System.currentTimeMillis();

        try {
            if(System.currentTimeMillis() - startTime < 500){
                try{
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            myRecorder.stop();

            myRecorder.release();
            myRecorder = null;
        } catch (Exception e) {
            myRecorder = null;
            e.printStackTrace();
        }

        if (stopTime - startTime > 1000) {
            if (onAudioRecordCallback != null) {
                onAudioRecordCallback.onComplete(outputFile);
            }
        } else {
            File current = new File(outputFile);
            current.delete();
            if (onAudioRecordCallback != null) {
                onAudioRecordCallback.onFailed(2);
            }
        }

        isRunning = false;
    }

    @Override
    public void run() {
        startRecord();
        while (!isStop) {
            try {
                Thread.sleep(30);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        stopRecord();
    }

    public void stopRecording() {
        isStop = true;
    }

    public boolean isRunning() {
        return isRunning;
    }
  }
}

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

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