1

我正在尝试在 windows 和 android 之间发送音频,我成功地将 windows 发送到 windows 但是当我从 android 流式传输音频时,它只会产生白噪声。我认为这是 android 和 Windows 中的 AudioFormat 的问题,因为我猜当我将示例 Bits 更改为 8 时,我在耳机的一侧听到了声音,但随后它也消失了。

在安卓端

        int BUFFER_MS = 15; // do not buffer more than BUFFER_MS milliseconds
        int bufferSize = 48000 * 2 * BUFFER_MS / 1000;
        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, 2,
                AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);


        byte[] buffer = new byte[bufferSize];
        int bytesRead;
        audioTrack.play();
        while (socket.isConnected()) {
            bytesRead = inputStream.read(buffer, 0, buffer.length);
            audioTrack.write(buffer,0,bytesRead);
        }

在 Windows 端

        AudioFormat format = getAudioFormat();
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

        // checks if system supports the data line
        if (!AudioSystem.isLineSupported(info)) {
            throw new LineUnavailableException(
                    "The system does not support the specified format.");
        }
        TargetDataLine audioLine = AudioSystem.getTargetDataLine(format);

        audioLine.open(format);
        audioLine.start();

        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead;

        while (socket.isConnected()) {
            bytesRead = audioLine.read(buffer, 0, buffer.length);
            outputStream.write(buffer,0,bytesRead);
        }

和 getAudioFormat 功能是

AudioFormat getAudioFormat() {
    float sampleRate = 48000;
    int sampleSizeInBits = 16;
    int channels = 2;
    boolean signed = true;
    boolean bigEndian = true;
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
            bigEndian);
}

只听到白噪音,如果有人可以帮忙,请做。

4

1 回答 1

0

Okayyyy 所以我发现了问题所在。我只需要将 bigEndian 设置为 false -_-

这是字节顺序的差异。我不明白为什么它在 android 和 pc 中有所不同,但似乎它可以解决问题。

于 2021-01-29T09:59:46.677 回答