user239158
user239158

Reputation: 19

How can I use javascript to play audio data streaming from a machine on browser of a remote PC?

I got audio data streaming continuously from a machine (e.g. [-198 -144 -172 ... 469 472 543] ....). They were successfully captured with html and javascript on a browser of a remote PC and I tried to use the following code to play the captured sound data:

data: .... [-198, -144, -172, ...  469,  472,  543] ....

var context = new AudioContext();
function playByteArray( bytes ) {
    var buffer = new Int16Array( bytes.length );
    buffer.set( new Int16Array(bytes), 0 );
    context.decodeAudioData(buffer.buffer, play);
}
        
function play( audioBuffer ) {
    var source = context.createBufferSource();
    source.buffer = audioBuffer;
    source.connect( context.destination );
    source.start(0);
}

playByteArray(data);

I got an error message: Uncaught (in promise) DOMException: Unable to decode audio data

Upvotes: 1

Views: 456

Answers (1)

Carl
Carl

Reputation: 25

The buffer has to be a Float32Array, and not an Int16Array.

Upvotes: 1

Related Questions