简体   繁体   中英

Play sound on hover in Adobe AIR HTML app

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM