How can I transfer the sound buffer to play it directly

I tried to create a sound buffer and send it to a new class. In this class, I would play this buffer with AudioTracker, but it does not work. I hear the sound on time, but the sound is like a halleffect. I have no error for my error and I did not find an answer to 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();

}
+5
source share
2 answers

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

+4
source

, AudioTrack, , , , , - AudioTrack. . 8000, - 8000, ​​ . , writeAudio :

private void writeAudioData() {        [] = [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();
0

All Articles