android – 如何传输音频缓冲区直接播放它

前端之家收集整理的这篇文章主要介绍了android – 如何传输音频缓冲区直接播放它前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

}
最佳答案
好的,我发现了问题.大厅效果来自实时录制的扬声器声音.错误.
原文链接:https://www.f2er.com/android/430601.html

猜你在找的Android相关文章