Reputation: 1035
I'm trying to record sound from the mic and then play it back, but all I here is a clicking sound. Both of the AudioTrack and AudioRecord are on the same settings, and initalised correctly. Here is my code:
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isRecording = false;
recordingThread.join();
player = findAudioTrack();
player.play();
for (int i = 0; i < audioQueue.size(); i++) {
int written = player.write(audioQueue.get(i), 0,
audioQueue.get(i).length);
}
player.stop();
player.release();
player = null;
}
});
}
private void startRecording() {
recorder = findAudioRecord();
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
addAudioToQueue();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
private void addAudioToQueue() {
short[] capturedAudio = new short[recordBufferSize/2];
while (isRecording) {
int read = recorder.read(capturedAudio, 0, capturedAudio.length);
audioQueue.add(capturedAudio);
}
recorder.stop();
recorder.release();
recorder = null;
}
}
Does anyone know why this is?
Here is the full source code: https://www.dropbox.com/s/h38cs9vjkztyyro/AudioTesting.java
Upvotes: 1
Views: 1736
Reputation: 2272
It probably has to do with the fact that you are using a short
array instead of a byte
array.
public int read (byte[] audioData, int offsetInBytes, int sizeInBytes)
It's likely that this function is only filling the first byte
of each short
so you are left with half of each index being 1 byte
of audio data, and the other byte
being junk or zeroes.
Try using a byte
array instead of short
.
Upvotes: 1