2015-04-24 19 views
0

我的應用程序啓動thread A來保存一些數據。在這個線程我調用函數startRecording(audioFile.getAbsolutePath());Android上錄音時出錯

,但我得到了以下錯誤:

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

prbably將是有益的:[鏈接](http://stackoverflow.com/questions/ 19709677/mediarecorder-invalid-state-16) –

+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; 
    } 
    } 
}