You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioRecorder.java 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.example.greenwatch.alarmrecorder.runnables;
  2. import android.media.MediaRecorder;
  3. import android.view.SurfaceHolder;
  4. import android.widget.Toast;
  5. import com.example.greenwatch.MainActivity;
  6. import java.io.IOException;
  7. public class AudioRecorder implements Runnable {
  8. private MediaRecorder audioRecorder;
  9. private String audioPath;
  10. public AudioRecorder() {
  11. audioRecorder = new MediaRecorder();
  12. }
  13. public void setAudioPath(String audioPath) {
  14. this.audioPath = audioPath;
  15. }
  16. public void stopAudioRecording(){
  17. if (audioRecorder != null) {
  18. audioRecorder.stop();
  19. audioRecorder.reset();
  20. audioRecorder.release();
  21. audioRecorder = null;
  22. }
  23. }
  24. @Override
  25. public void run() {
  26. audioRecorder = new MediaRecorder();
  27. audioRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
  28. audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  29. audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
  30. audioRecorder.setOutputFile(audioPath);
  31. try {
  32. audioRecorder.prepare();
  33. audioRecorder.start();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }