我的应用程序启动thread A
以保存一些数据。在这个线程中,我调用函数startRecording(audioFile.getAbsolutePath());
但是我收到以下错误:
start called in an invalid state: 16; at android.media.MediaRecorder.start(Native Method)
每次都不会发生此错误,但有时我会从我的应用程序中收到此错误报告。
下面是我的代码。
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;
}
}
答案 0 :(得分:0)
我认为您在前一个录音机未停止和释放时尝试录音 你可以使用我的班级
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;
}
}
}