5

我试图创建一个音频缓冲区并将他送到一个新班级。在这个类中,我会用 AudioTracker 播放这个缓冲区,但它不起作用。我可以按时听到声音,但声音就像霍尔效应。我对我的错误没有想法,也没有找到这个问题的答案。我希望你能帮助我。(对不起,我的英语不是最好的)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();

}
4

2 回答 2

4

好的,我发现了问题所在。霍尔效应来自实时录制的扬声器声音。糟糕的错误。

于 2012-05-10T13:21:09.200 回答
0

将数据写入AudioTrack对象的原因实际上是打开它,写入缓冲区,关闭并释放它,如果最小缓冲区大小如此之小,由于 AudioTrack 不断重新初始化,您实际上听不到。这就是为什么通过更改缓冲区大小来解决它的原因。如果您将其设为 8000 并且您的采样率为 8000,您将听到延迟一秒的声音。为了解决这个问题,去掉你的 send 方法并将 writeAudio 方法更改为:

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();
于 2014-03-04T20:07:20.353 回答