音频处理器HTML5的JavaScript [英] Processor Audio JavaScript HTML5

查看:152
本文介绍了音频处理器HTML5的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个JS脚本:

<script>
function start1() {
var audio = new Audio();
audio.src = URL.createObjectURL(document.getElementById('f1').files[0]);
audio.autoplay = true;
}
function start2() {
var audio = new Audio();
audio.src = URL.createObjectURL(document.getElementById('f2').files[0]);
audio.autoplay = true;
}
...
function stop() {
var audio = new Audio();
audio.stop();
}
</script>

和HTML code:

and html code:

<body>
<input type="file" id="f1"></input>
<input type="file" id="f2"></input>
...
<button onmousedown="start1()" onmouseup="stop()">1</button>
<button onmousedown="start2()" onmouseup="stop()">2</button>
...

如可以通过该脚本可以理解,他播放声音文件,通过使用表格文件载荷。



但有两个问题:

1)要求的声音在事件pressing只打了(onmousedown事件)和停止时,松开鼠标按钮(onmouseup)。
现在声音播放到最后也不要紧,你是否点击鼠标左键,然后松开它(因为有这个条件不测试)。

2)还需要创建脚本的简化版本,作为用于处理的功能应该是16个,但不是手动注册同一每个函数。您必须创建一个生成的脚本,将进程ID(启动1,START2等,F1,F2等)的N个。



帮助修改code,你也可以完全code jQuery的。它不是关键的。

谢谢你。

As can be understood through the script, he plays a sound file that loads by using the form files.

But there are two problems:
1) requires that sound is played only during events pressing (onmousedown) and stops when the mouse button is released (onmouseup). Now the sound is played to the end it does not matter whether you clicked the mouse button and let go of it (because there is no test for this condition).
2) also need to create a simplified version of the script, as the functions for processing should be 16 pieces, but not manually register the same each function. You must create a generated script that will process the N-number of id (start1, start2, etc., f1, f2, etc).

Help to modify the code, you can also fully code jquery. It is not critical.
Thank you.

推荐答案

您可以使用局部变量,你只需要一个标识符后才找到音频元素。您还需要将音频元素添加到DOM。

You can use local variables, you just need an identifier to find the audio element later. You also need to append the audio element to the DOM.

另外,你必须使用暂停()不会停止()。

Also, you have to use pause() not stop().

        <script>
        function start(id, source) {
                var aud = document.getElementById(id);

                if (aud) {
                    aud.parentNode.removeChild(aud);
                }

                var audio = new Audio();
                audio.setAttribute("id", id);
                audio.src = URL.createObjectURL(document.getElementById(source).files[0]);
                document.body.appendChild(audio);
                audio.autoplay = true;
            }

            function stop(id) {
                var aud = document.getElementById(id);
                aud.pause();
            }
    </script>

    <input type="file" id="f1"></input>
    <input type="file" id="f2"></input>
    <button onmousedown="start('1', 'f1')" onmouseup="stop('1')">1</button>
    <button onmousedown="start('2', 'f2')" onmouseup="stop('2')">2</button>

这篇关于音频处理器HTML5的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆