Recording Audio using MediaRecorder
in
Android Processing
•
2 years ago
Hi All,
Wondering if anyone has had any experience/luck with this...
I've seemingly integrated some native Java code / Android SDK code into my P5 sketch...
- // IMPORTS
- import java.io.File;
- import java.io.IOException;
- // Class taken from Benjamin McCann : http://www.benmccann.com/dev-blog/android-audio-recording-tutorial/
- public class AudioRecorder {
- final MediaRecorder recorder = new MediaRecorder();
- final String path;
- // Sets a New Path
- public AudioRecorder(String path) {
- this.path = sanitizePath(path);
- }
- private String sanitizePath(String path) {
- if(!path.startsWith("/")) {
- path = "//sdcard//SM//" + path;
- }
- if(!path.contains(".")) {
- path += "" + taggedLocation +".3gp";
- }
- return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
- }
- // Start
- public void start() throws IOException {
- String state = android.os.Environment.getExternalStorageState();
- if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
- throw new IOException("SD Card is not mounted. It is " + state + ".");
- }
- File directory = new File(path).getParentFile();
- if(!directory.exists() && !directory.mkdirs()) {
- throw new IOException("Path to file could not be created");
- }
- recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
- recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
- recorder.setOutputFile(path);
- recorder.prepare();
- recorder.start();
- }
- // Stop
- public void stop() throws IOException {
- recorder.stop();
- recorder.reset();
- //recorder = null;
- recorder.release();
- }
- }
earlier on in the sketch, I import
- import android.os.Environment;
- import android.media.AudioRecord;
- import android.media.AudioFormat;
- import android.media.MediaRecorder;
... and I think that's enough sufficient classes. I am not experiencing any errors per se, but when I call the methods
start()
and
stop()
- within
mouseClicked() - the sketch is hanging on the
stop()... I know it gets there, as I have a
println() just after it that prints properly, but the file never gets created, the sketch hangs, etc.
I'm pretty sure the class is constructed and called the right way, but I'm a little unsure when it comes to pure Java, so if anyone has any experience with this part of the Android SDK, or can spot any obvious Java programming errors that I may have made, that would be appreciated.
Thanks in advance,
~ J
1