在Adobe AIR HTML应用程序中悬停播放声音

时间:2014-07-04 14:17:22

标签: javascript jquery actionscript-3 flash air

我正在创建一个基于HTML的Adobe AIR应用程序,当用户将鼠标悬停在某个链接上时,我想发出一声哔声。 mouseenter事件由jQuery处理,然后调用一个函数。对于Web,它使用Web Audio API(由Howl.js提供支持),但由于AIR不支持API,因此我可以使用Flash API。

到目前为止我所拥有的是:

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

}

但它不起作用......我做错了什么?

1 个答案:

答案 0 :(得分:3)

这有效:

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

但是,您不需要myChannel,只能致电beep.play();

事实上,你可以用三行来做到这一点:

var beep = new air.Sound();
beep.load(new air.URLRequest('./assets/beep.mp3'));
beep.play();