Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement