2011-03-24 83 views
0

最近,我們一直在開發某種基於Web的即時消息應用程序,我想問一下最好的方法來實現最好通過JavaScript控制的消息提示音。我們可以使用任何現有的庫嗎?按需聲音的最佳做法是什麼?

+0

直到HTML5最可靠的是FLASH – mplungjan 2011-03-24 08:31:00

+0

HTML5是你的最好的選擇! :) – Shouvik 2011-03-24 09:09:57

回答

0

可以使用html5 audio tag,好處是不使用任何額外的插件(QuickTime的,閃光...),但browser must support the audio tag

function SoundPlay (src) 
{ 
    var soundEmbed = document.createElement("audio"); 
    soundEmbed.setAttribute("src", src); 
    soundEmbed.setAttribute("autoplay", 'autoplay'); 
    soundEmbed.setAttribute("type", 'audio/mpeg');  

    //jQuery can help you with the removal of the audio element 
    $(soundEmbed).one('ended', function(evt) 
    { 
     $(evt.target).remove(); 
    }); 

    // or, if you are not using jQuery 
    soundEmbed.onended = function() 
    { 
     document.body.removeChild(soundEmbed); 
    }; 

    document.body.appendChild(soundEmbed); 
}