Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Created August 20, 2015 03:22
Show Gist options
  • Save Noitidart/623504c57892eda24d74 to your computer and use it in GitHub Desktop.
Save Noitidart/623504c57892eda24d74 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-playSound - Trying to use Audio API to play sound.
function audioContextCheck() {
if (typeof Services.appShell.hiddenDOMWindow.AudioContext !== 'undefined') {
return new Services.appShell.hiddenDOMWindow.AudioContext();
} else if (typeof Services.appShell.hiddenDOMWindow.mozAudioContext !== 'undefined') {
return new Services.appShell.hiddenDOMWindow.mozAudioContext();
} else {
throw new Error('AudioContext not supported');
}
}
var audioContext = audioContextCheck();
var audioBuffer;
var getSound = new XMLHttpRequest();
getSound.open('get', OS.Path.toFileURI(OS.Path.join(OS.Constants.Path.desktopDir, 'zirzir.mp3')), true);
getSound.responseType = 'arraybuffer';
getSound.onload = function() {
audioContext.decodeAudioData(getSound.response, function(buffer) {
audioBuffer = buffer;
var playSound = audioContext.createBufferSource();
playSound.buffer = audioBuffer;
playSound.connect(audioContext.destination);
playSound.start(audioContext.currentTime);
});
};
@Noitidart
Copy link
Author

README

Rev1

  • Works, have a file on your desktop named zirzir.mp3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment