Reputation: 28783
I'm making a HTML-based Adobe AIR application where I want to play a beep sound when a user hovers over a link. The mouseenter event is handled by jQuery and then calls a function. For the Web it uses the Web Audio API (powered by Howl.js) but because AIR does not support the API I fallback to use the Flash API.
What I have so far is:
// if Adobe AIR
if (window.runtime){
var myChannel = new window.runtime.flash.media.SoundChannel();
var beep;
$('ul.menu a').on('mouseenter', function(){
// stop all sounds... (should I be stopping all sounds though?)
window.runtime.flash.media.SoundMixer.stopAll();
// set beep as a new sound
beep = new window.runtime.flash.media.Sound();
// load mp3
beep.load(new URLRequest('./assets/beep.mp3'));
// once file has loaded
beep.addEventListener(air.Event.COMPLETE, function(){
myChannel = beep.play();
});
});
// we're running in the browser
} else {
var sound = new Howl({
urls: ['./assets/beep.mp3', './assets/beep.ogg']
});
$('ul.menu a').on('mouseenter', function(){
sound.play();
});
}
But it doesn't work... What have I done wrong?
Upvotes: 1
Views: 273
Reputation: 28783
This works:
var myChannel = new window.runtime.flash.media.SoundChannel();
var beep;
$('a').on('mouseenter', function(){
// stop all sounds... (should I be stopping all sounds though?)
window.runtime.flash.media.SoundMixer.stopAll();
// set beep as a new sound
beep = new window.runtime.flash.media.Sound();
// load mp3
beep.load(new air.URLRequest('./assets/beep.mp3'));
// once file has loaded
beep.addEventListener(air.Event.COMPLETE, function(){
myChannel = beep.play();
});
});
However you don't need myChannel
and could just call beep.play();
In fact you can do it with three lines:
var beep = new air.Sound();
beep.load(new air.URLRequest('./assets/beep.mp3'));
beep.play();
Upvotes: 3