我想我的网页哔哔每当用户超过我的<textarea>的最大字符限制。


当前回答

以下是我如何使用HTML5让它发出哔哔声: 首先我复制并将windows wav文件转换为mp3,然后我使用以下代码:

var _beep = window.Audio("Content/Custom/Beep.mp3")

function playBeep() { _beep.play()};

全局声明声音文件并在需要时引用它会更快。

其他回答

这在JavaScript中是不可能直接实现的。您需要在HTML中嵌入一个简短的WAV文件,然后通过代码播放该文件。

一个例子:

<script>
function PlaySound(soundObj) {
  var sound = document.getElementById(soundObj);
  sound.Play();
}
</script>

<embed src="success.wav" autostart="false" width="0" height="0" id="sound1"
enablejavascript="true">

然后从JavaScript代码中调用它,如下所示:

PlaySound("sound1");

这应该完全是你想要的-你只需要自己找到/创建哔哔声,这应该是微不足道的。

当我们读到这个答案时,HTML5将为你解决这个问题,如果你愿意这样做的话。所有现代浏览器都支持HTML5音频。

下面是示例的副本:

var snd = new Audio("file.wav"); // buffers automatically when created
snd.play();

以下是我如何使用HTML5让它发出哔哔声: 首先我复制并将windows wav文件转换为mp3,然后我使用以下代码:

var _beep = window.Audio("Content/Custom/Beep.mp3")

function playBeep() { _beep.play()};

全局声明声音文件并在需要时引用它会更快。

function Sound(url, vol, autoplay, loop)
{
    var that = this;

    that.url = (url === undefined) ? "" : url;
    that.vol = (vol === undefined) ? 1.0 : vol;
    that.autoplay = (autoplay === undefined) ? true : autoplay;
    that.loop = (loop === undefined) ? false : loop;
    that.sample = null;

    if(that.url !== "")
    {
        that.sync = function(){
            that.sample.volume = that.vol;
            that.sample.loop = that.loop;
            that.sample.autoplay = that.autoplay;
            setTimeout(function(){ that.sync(); }, 60);
        };

        that.sample = document.createElement("audio");
        that.sample.src = that.url;
        that.sync();

        that.play = function(){
            if(that.sample)
            {
                that.sample.play();
            }
        };

        that.pause = function(){
            if(that.sample)
            {
                that.sample.pause();
            }
        };
    }
}

var test = new Sound("http://mad-hatter.fr/Assets/projects/FreedomWings/Assets/musiques/freedomwings.mp3");
test.play();

http://jsfiddle.net/sv9j638j/

function beep(wavFile){
    wavFile = wavFile || "beep.wav"
    if (navigator.appName == 'Microsoft Internet Explorer'){
        var e = document.createElement('BGSOUND');
        e.src = wavFile;
        e.loop =1;
        document.body.appendChild(e);
        document.body.removeChild(e);
    }else{
        var e = document.createElement('AUDIO');
        var src1 = document.createElement('SOURCE');
        src1.type= 'audio/wav';
        src1.src= wavFile;
        e.appendChild(src1);
        e.play();
    }
}

适用于Chrome,IE,Mozilla使用Win7操作系统。 需要服务器上的beep.wav文件。