anonym2048
anonym2048

Reputation: 103

How can I transfer audio buffer to play it directly

I tried to create an Audio buffer and send him to a new class. In this Class I would play this buffer with AudioTracker but it doesn't work. I can hear the sound on time but the sound is like a halleffect. I have no Idear for my mistake and didn't found an answer for this problem . I hope you can help me. (Sorry my English is not the best) Sorcecode:

public class input {
private static final String TAG = "Aufnahme";
private AudioRecord recorder = null;
private boolean isRecording = false;
private int SAMPLERATE = 8000;
private int CHANNELS = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
private int bufferSize = AudioRecord.getMinBufferSize(SAMPLERATE, CHANNELS,
        AUDIO_FORMAT);
private Thread recordingThread = null;

public void startRecording() {
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLERATE,
            CHANNELS, AUDIO_FORMAT, bufferSize);

    recorder.startRecording();
    isRecording = true;

    recordingThread = new Thread(new Runnable()

    {
        public void run() {
            writeAudioData();
        }

    });
    recordingThread.start();

}

public void stopRecording() {
    isRecording = false;
    recorder.stop();
    recorder.release();
    recorder = null;
    recordingThread = null;
}

private void writeAudioData() {

    byte data[] = new byte[bufferSize];

    while (isRecording) {

        recorder.read(data, 0, bufferSize);
        send(data);

    }
}

private void send(byte[] data) {

    int minBufferSize = AudioTrack.getMinBufferSize(8000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
            AudioTrack.MODE_STREAM);

    at.play();
    at.write(data, 0, bufferSize);
    at.stop();
    at.release();

}

Upvotes: 6

Views: 5419

Answers (2)

Mustafa
Mustafa

Reputation: 10413

The reason when you write data to AudioTrack object, you actually open it, write to buffer, close and release it, and if the minimum buffer size is so small, you cannot actually hear because of constant re-initilization of the AudioTrack. This is why it is solved by changing the buffer size. If you make it 8000 and your sample rate is 8000, you will hear the sound with one second delay. To solve this, get rid of your send method and change the writeAudio method to this:

private void writeAudioData() { byte data[] = new byte[bufferSize];

    int minBufferSize = AudioTrack.getMinBufferSize(8000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
            AudioTrack.MODE_STREAM);


    at.play();

    while (isRecording) {
        recorder.read(data, 0, bufferSize);
        at.write(data, 0, bufferSize);
    }
    at.stop();
    at.release();

Upvotes: 0

anonym2048
anonym2048

Reputation: 103

Ok, I figured out the problem. The hall effect came from the speaker sound which was recorded in real time. Bad mistake.

Upvotes: 4

Related Questions