Successful recording, wooooot

pull/14/head
Sam Bosley 13 years ago
parent 5a0f3d3f1a
commit 2d968f9583

@ -16,6 +16,7 @@ public class AACRecorder {
private String tempFile;
private boolean recording;
private AACRecorderCallbacks listener;
private static final int SAMPLE_RATE = 8000;
private static final int NOTIFICATION_PERIOD = 160;
@ -24,20 +25,28 @@ public class AACRecorder {
private byte[] buffer = new byte[NOTIFICATION_PERIOD * 2];
public interface AACRecorderCallbacks {
public void encodingFinished();
}
private Thread readerThread = new Thread() {
private byte[] readBuffer = new byte[NOTIFICATION_PERIOD * 2];
public void run() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead = 0;
while(recording) {
int bytesRead = audioRecord.read(readBuffer, 0, readBuffer.length);
bytesRead = audioRecord.read(readBuffer, 0, readBuffer.length);
System.err.println("Bytes read: " + bytesRead);
try {
baos.write(readBuffer);
} catch (IOException e) {
//
}
if (bytesRead <= 0)
break;
}
encoder.encode(baos.toByteArray());
finishRecording();
baos.reset();
}
};
@ -61,20 +70,29 @@ public class AACRecorder {
encoder.init(64000, 1, SAMPLE_RATE, 16, tempFile);
recording = true;
readerThread.start();
audioRecord.startRecording();
readerThread.start();
}
public synchronized void stopRecording() {
if (!recording)
return;
recording = false;
audioRecord.stop();
}
public synchronized void finishRecording() {
recording = false;
audioRecord.release();
System.err.println("Uninit");
encoder.uninit();
if (listener != null)
listener.encodingFinished();
}
public void setListener(AACRecorderCallbacks listener) {
this.listener = listener;
}
public synchronized boolean convert(String outFile) {

@ -13,12 +13,13 @@ import android.widget.Toast;
import com.timsu.astrid.R;
import com.todoroo.aacenc.AACRecorder;
import com.todoroo.aacenc.AACRecorder.AACRecorderCallbacks;
import com.todoroo.aacenc.AACToM4A;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.service.ThemeService;
public class AACRecordingActivity extends Activity {
public class AACRecordingActivity extends Activity implements AACRecorderCallbacks {
public static final String EXTRA_TEMP_FILE = "tempFile"; //$NON-NLS-1$
public static final String EXTRA_TASK_ID = "taskId"; //$NON-NLS-1$
@ -28,6 +29,8 @@ public class AACRecordingActivity extends Activity {
private String tempFile;
private long taskId;
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -40,6 +43,7 @@ public class AACRecordingActivity extends Activity {
taskId = getIntent().getLongExtra(EXTRA_TASK_ID, 0L);
recorder = new AACRecorder(this);
recorder.setListener(this);
recorder.startRecording(tempFile);
}
@ -50,7 +54,6 @@ public class AACRecordingActivity extends Activity {
stopRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.err.println("On click");
stopRecording();
}
});
@ -70,13 +73,14 @@ public class AACRecordingActivity extends Activity {
@SuppressWarnings("nls")
private void stopRecording() {
System.err.println("Stopping...");
recorder.stopRecording();
System.err.println("Stopped recorder");
ProgressDialog pd = DialogUtilities.progressDialog(this, "Encoding...");
pd = DialogUtilities.progressDialog(this, "Encoding...");
pd.show();
System.err.println("Passed pd");
}
@SuppressWarnings("nls")
public void encodingFinished() {
try {
StringBuilder filePathBuilder = new StringBuilder();
filePathBuilder.append(getExternalFilesDir("audio").toString())
@ -87,14 +91,11 @@ public class AACRecordingActivity extends Activity {
.append("_audio.mp4");
String outFile = filePathBuilder.toString();
System.err.println("Converting");
new AACToM4A().convert(this, tempFile, outFile);
System.err.println("Finished Converting");
Intent result = new Intent();
result.putExtra(RESULT_OUTFILE, outFile);
setResult(RESULT_OK, result);
System.err.println("Finishing");
finish();
} catch (IOException e) {
e.printStackTrace();
@ -103,5 +104,4 @@ public class AACRecordingActivity extends Activity {
pd.dismiss();
}
}

Loading…
Cancel
Save