简体   繁体   English

点播声音的最佳做法是什么?

[英]What are the best practices for on-demand sound?

Recently we've been developing some sort of a web based IM application and I would like to ask the best way to implement message alert tones preferably controllable via JavaScript. 最近,我们一直在开发某种基于Web的IM应用程序,我想询问实现消息警报音的最佳方法,最好通过JavaScript来控制它。 Are there any existing libraries we can use for this? 我们可以使用现有的任何库吗?

you can use the html5 audio tag , the benefit is that you are not using any additional plugins (quickTime, flash...), but the browser must support the audio tag 您可以使用html5音频标签 ,好处是您没有使用任何其他插件(quickTime,flash ...),但是浏览器必须支持音频标签

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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