|
|
@ -8,12 +8,19 @@ import android.media.AudioFormat;
|
|
|
|
import android.media.AudioRecord;
|
|
|
|
import android.media.AudioRecord;
|
|
|
|
import android.media.MediaRecorder.AudioSource;
|
|
|
|
import android.media.MediaRecorder.AudioSource;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* This class combines an Android AudioRecord and our own AACEncoder
|
|
|
|
|
|
|
|
* to directly record an AAC audio file from the mic. Users should call
|
|
|
|
|
|
|
|
* startRecording() and stopRecording() in sequence, and then listen
|
|
|
|
|
|
|
|
* for the encodingFinished() callback to perform final actions like
|
|
|
|
|
|
|
|
* converting to M4A format.
|
|
|
|
|
|
|
|
* @author Sam
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class AACRecorder {
|
|
|
|
public class AACRecorder {
|
|
|
|
|
|
|
|
|
|
|
|
private AudioRecord audioRecord;
|
|
|
|
private AudioRecord audioRecord;
|
|
|
|
private AACEncoder encoder;
|
|
|
|
private AACEncoder encoder;
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
private String tempFile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean recording;
|
|
|
|
private boolean recording;
|
|
|
|
private AACRecorderCallbacks listener;
|
|
|
|
private AACRecorderCallbacks listener;
|
|
|
@ -34,7 +41,6 @@ public class AACRecorder {
|
|
|
|
int bytesRead = 0;
|
|
|
|
int bytesRead = 0;
|
|
|
|
while(recording) {
|
|
|
|
while(recording) {
|
|
|
|
bytesRead = audioRecord.read(readBuffer, 0, readBuffer.length);
|
|
|
|
bytesRead = audioRecord.read(readBuffer, 0, readBuffer.length);
|
|
|
|
System.err.println("Bytes read: " + bytesRead);
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
baos.write(readBuffer);
|
|
|
|
baos.write(readBuffer);
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
@ -50,7 +56,7 @@ public class AACRecorder {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AACRecorder(Context context) {
|
|
|
|
public AACRecorder() {
|
|
|
|
encoder = new AACEncoder();
|
|
|
|
encoder = new AACEncoder();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -58,7 +64,6 @@ public class AACRecorder {
|
|
|
|
if (recording)
|
|
|
|
if (recording)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
this.tempFile = tempFile;
|
|
|
|
|
|
|
|
audioRecord = new AudioRecord(AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
|
|
|
|
audioRecord = new AudioRecord(AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
|
|
|
|
AudioFormat.ENCODING_PCM_16BIT, MIN_BUFFER_SIZE);
|
|
|
|
AudioFormat.ENCODING_PCM_16BIT, MIN_BUFFER_SIZE);
|
|
|
|
|
|
|
|
|
|
|
@ -83,7 +88,6 @@ public class AACRecorder {
|
|
|
|
public synchronized void finishRecording() {
|
|
|
|
public synchronized void finishRecording() {
|
|
|
|
recording = false;
|
|
|
|
recording = false;
|
|
|
|
audioRecord.release();
|
|
|
|
audioRecord.release();
|
|
|
|
System.err.println("Uninit");
|
|
|
|
|
|
|
|
encoder.uninit();
|
|
|
|
encoder.uninit();
|
|
|
|
if (listener != null)
|
|
|
|
if (listener != null)
|
|
|
|
listener.encodingFinished();
|
|
|
|
listener.encodingFinished();
|
|
|
@ -93,17 +97,4 @@ public class AACRecorder {
|
|
|
|
this.listener = listener;
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized boolean convert(String outFile) {
|
|
|
|
|
|
|
|
if (recording || tempFile == null)
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
new AACToM4A().convert(context, tempFile, outFile);
|
|
|
|
|
|
|
|
tempFile = null;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|