I’ve been playing around with playing sound using MediaPlugin and NativeAudioPlugin, but I would prefer to use WebAudio. Is this possible in ionic 2 (mobile, not web)?
I managed to synthesize a sine-wave with the following code (only tested using iOS emulator!)
let audioCtx = new (window["AudioContext"] || window["webkitAudioContext"])();
let gainNode = audioCtx.createGain();
gainNode.connect(audioCtx.destination);
gainNode.gain.value = 0.5;
let oscillator = audioCtx.createOscillator();
oscillator.connect(gainNode);
oscillator.type = 'sine'; // sine wave — other values are 'square', 'sawtooth', 'triangle' and 'custom'
oscillator.frequency.value = 1500; // in hertz
oscillator.start(0); // error if 'when' argument is not provided!
oscillator.stop(1); // in seconds; error if 'when' argument is not provided!
Some issues though:
- if I write
let audioCtx = new (window.AudioContext || window.webkitAudioContext)()
then the typescript compiler complains that window.AudioContext
doesn’t exist.
_
2. oscillator.start()
or oscillator.stop()
without arguments causes a runtime error: I had to write the ‘when’ argument explicitly to get it working (even though the API says the argument defaults to zero).
Any ideas about these issues?
I haven’t tried it on Android emulator, nor on a real device. Would that work?