Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select destination output device using Web Audio Api

I have been using web audio api and have created a context, and populated a source buffer with data. It plays fine over the default output device, but i don't understand how to choose the destination. In an old w3 spec you were able to pass in the correct deviceId to the audio context constructor, but i can't figure out how to do it now without using a media element. Any suggestions?

source = context.createBufferSource()
source.loop = true;
source.buffer = globalAudioBuffer;
source.connect(context.destination);
context.resume();
source.start(0);
like image 770
Rogue45 Avatar asked Jan 25 '17 22:01

Rogue45


1 Answers

Unfortunately, setting the destination audio device of a webaudio graph isn't yet implemented, and the api for this is not yet finalized.

What you can do for now, is connect the webaudio graph to an HTML element, and set the sinkid of the element (currently works on Chrome only)

Here is a simple example:

var ac = new AudioContext();
var audio = new Audio();
var o = ac.createOscillator();
o.start();
var dest = ac.createMediaStreamDestination();
o.connect(dest);
audio.src = URL.createObjectURL(dest.stream);
audio.play();

Now your oscillator will play through the audio element and you can now call audio.setSinkId() with the deviceId of a connected output device.

like image 100
Asher Avatar answered Nov 16 '22 22:11

Asher