So, for the background, I am trying to create an application that continuously records through a microphone and saves data in a buffer. The goal of the program would be to perform some type of data processing in the buffer and act on it. Currently, the application simply reads the data into the buffer and writes it as soon as possible. When you launch the application, you speak into the microphone, and then hear what you just said, go to the other end.
Now this is where my inexperience with Android begins to raise its head. I perform a read and write operation as an infinite loop. The service is below:
public class AudioService extends Service {
private final int MIC = AudioSource.MIC;
private final int SAMPLE = 44100;
private final int CHANNELI = AudioFormat.CHANNEL_IN_MONO;
private final int CHANNELO = AudioFormat.CHANNEL_OUT_MONO;
private final int FORMAT = AudioFormat.ENCODING_PCM_16BIT;
private final int BUFFERSIZE = AudioRecord.getMinBufferSize(SAMPLE,
CHANNELI, FORMAT);
private final int STREAM = AudioManager.STREAM_MUSIC;
private final int MODE = AudioTrack.MODE_STREAM;
private boolean play = true;
AudioRecord recorder = null;
AudioTrack track = null;
short[] buffer = null;
public void OnCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
recorder = new AudioRecord(MIC, SAMPLE, CHANNELI, FORMAT, BUFFERSIZE);
track = new AudioTrack(STREAM, SAMPLE, CHANNELO, FORMAT, BUFFERSIZE,
MODE);
buffer = new short[BUFFERSIZE];
recorder.startRecording();
track.play();
while (play) {
recorder.read(buffer, 0, buffer.length);
track.write(buffer, 0, buffer.length);
}
return START_STICKY;
}
, "" . , , . , :
private OnClickListener Recording = new OnClickListener() {
public void onClick(View v) {
serviceIntent = new Intent(getApplicationContext(),
AudioService.class);
serviceIntent.putExtra("extraData", "somedata");
startService(serviceIntent);
record.setEnabled(false);
cancel.setEnabled(true);
cancel.requestFocus();
};
. , startService (serviceIntent), .
, ? - , Android , . - , , ; startIntent " ".
. , , " ...", . - , , .