Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

windows - Audio routing between Android and PC produces white noise

I am trying to send audio between windows and android, I was successfully able to do that windows to windows but when I stream audio from android, it produces a white noise only. I think it is an issue with the AudioFormat in android and Windows because when I changed the sample Bits to 8 I guess, I heard the voice in one side of my headphones but then it went away too.

On Android Side

        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);
        }

On Windows Side

        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);
        }

and getAudioFormat function is

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);
}

Only hearing a white noise, if someone can help please do.

question from:https://stackoverflow.com/questions/65937832/audio-routing-between-android-and-pc-produces-white-noise

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okayyyy So I found out the problem. I just had to put bigEndian to false -_-

It's the byte order difference. I don't understand why it's different in android and pc but seems like it does the trick.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...