Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. start called in an invalid state: 16; at android.media.MediaRecorder.start(Native Method)
  2.  
  3. public void startRecording(String outputPath) {
  4. if (recorder == null) {
  5. recorder = new MediaRecorder();
  6. recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  7. recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  8. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
  9. recorder.setAudioSamplingRate(RECORDER_SAMPLERATE_22050);
  10. recorder.setOutputFile(outputPath);
  11. try {
  12. recorder.prepare();
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. recorder.start();
  18. SampleRecordThread thread = new SampleRecordThread(outputPath);
  19. thread.start();
  20.  
  21. }
  22.  
  23. private class SampleRecordThread extends Thread {
  24. private volatile boolean running = true;
  25.  
  26. public void exit() {
  27. running = false;
  28. }
  29.  
  30. @Override
  31. public void run() {
  32. while (running) {
  33. try {
  34. Thread.sleep(10 * 1000);//to record 10 seconds sound
  35. } catch (InterruptedException e) {
  36. e.printStackTrace();
  37. }
  38. recorder.stop();
  39. recorder.reset();
  40. recorder.release();
  41. recorder = null;
  42.  
  43. // upload the data to cloud
  44. if (isWifiActive && isInstallationSaved) {
  45. .....
  46. record.saveInBackground();
  47. }
  48.  
  49. break;
  50. }
  51. }
  52.  
  53. public class AudioRecordManager {
  54.  
  55. public interface OnAudioRecordCallback {
  56.  
  57. void onComplete(String path);
  58.  
  59. void onFailed(int code);
  60.  
  61. }
  62.  
  63. private OnAudioRecordCallback onAudioRecordCallback;
  64.  
  65. public void setOnAudioRecordCallback(OnAudioRecordCallback onAudioRecordCallback) {
  66. this.onAudioRecordCallback = onAudioRecordCallback;
  67. }
  68.  
  69. private MediaRecorder myRecorder;
  70. private String outputFile;
  71. private AudioRecordThread audioRecordThread;
  72.  
  73. public AudioRecordManager() {
  74. audioRecordThread = new AudioRecordThread();
  75. }
  76.  
  77. private AudioRecordThread audioRecordThread;
  78.  
  79. public void startRecord() {
  80. if(audioRecordThread == null || !audioRecordThread.isRunning()){
  81. new Thread(audioRecordThread).start();
  82. }
  83. }
  84.  
  85. public void stopRecord() {
  86. if(audioRecordThread!=null && audioRecordThread.isRunning()) {
  87. audioRecordThread.stopRecording();
  88. }
  89. }
  90.  
  91. class AudioRecordThread implements Runnable {
  92.  
  93. private boolean isRunning;
  94.  
  95. private boolean isStop;
  96.  
  97. private long startTime;
  98.  
  99. private void startRecord() {
  100. isRunning = true;
  101. isStop = false;
  102. File folder = new File(Content.AUDIO_DIR);
  103. if (!folder.exists()) {
  104. boolean b = folder.mkdirs();
  105. }
  106. startTime = System.currentTimeMillis();
  107. outputFile = folder.getPath() + "/rec_" + startTime + ".mp3";
  108.  
  109. myRecorder = new MediaRecorder();
  110. myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  111. myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  112. myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
  113. myRecorder.setOutputFile(outputFile);
  114. try {
  115. myRecorder.prepare();
  116. myRecorder.start();
  117.  
  118. startTime = System.currentTimeMillis();
  119.  
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. }
  124.  
  125. private void stopRecord() {
  126.  
  127. final long stopTime = System.currentTimeMillis();
  128.  
  129. try {
  130. if(System.currentTimeMillis() - startTime < 500){
  131. try{
  132. Thread.sleep(500);
  133. }catch (Exception e){
  134. e.printStackTrace();
  135. }
  136. }
  137. myRecorder.stop();
  138.  
  139. myRecorder.release();
  140. myRecorder = null;
  141. } catch (Exception e) {
  142. myRecorder = null;
  143. e.printStackTrace();
  144. }
  145.  
  146. if (stopTime - startTime > 1000) {
  147. if (onAudioRecordCallback != null) {
  148. onAudioRecordCallback.onComplete(outputFile);
  149. }
  150. } else {
  151. File current = new File(outputFile);
  152. current.delete();
  153. if (onAudioRecordCallback != null) {
  154. onAudioRecordCallback.onFailed(2);
  155. }
  156. }
  157.  
  158. isRunning = false;
  159. }
  160.  
  161. @Override
  162. public void run() {
  163. startRecord();
  164. while (!isStop) {
  165. try {
  166. Thread.sleep(30);
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. }
  170. }
  171. stopRecord();
  172. }
  173.  
  174. public void stopRecording() {
  175. isStop = true;
  176. }
  177.  
  178. public boolean isRunning() {
  179. return isRunning;
  180. }
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement