在javascript音频中看到双重,三重声音,如何解决?(Seeing double, triple sound in javascript audio, how to solve it?)

编程入门 行业动态 更新时间:2024-10-23 07:29:56
在javascript音频中看到双重,三重声音,如何解决?(Seeing double, triple sound in javascript audio, how to solve it?) **This function is in javascript:** function quesfl(qsvalue) { sngno = qsvalue + ".wav"; var audiophile = new Audio(); audiophile.src = sngno; audiophile.play(); } **html part:** <input type="radio" name="qno" value="02" onClick="quesfl(this.value);">2 <input type="radio" name="qno" value="03" onClick="quesfl(this.value);">3 <input type="radio" name="qno" value="04" onClick="quesfl(this.value);">4

在上面的音频java脚本代码中,我有一个变量'sngno'用于不同的src。 但是,当我改为不同的src时,所有的歌曲都在播放,但我只想要一首。 当我的网页打开时,如果我重新加载页面,每次播放第一首选定的歌曲,但是当我为另一首歌曲选择其他按钮时,两者都会播放。 请帮助。

**This function is in javascript:** function quesfl(qsvalue) { sngno = qsvalue + ".wav"; var audiophile = new Audio(); audiophile.src = sngno; audiophile.play(); } **html part:** <input type="radio" name="qno" value="02" onClick="quesfl(this.value);">2 <input type="radio" name="qno" value="03" onClick="quesfl(this.value);">3 <input type="radio" name="qno" value="04" onClick="quesfl(this.value);">4

In the above java script code for audio, I have a variable 'sngno' for different src. However, when I change to different src, all the songs are playing, but I want only the one. While my web page is open, if I reload the page, each time the first selected song is played, but when i select other button for another song, both plays. Help please.

最满意答案

得到了解决方案。

请尝试以下代码。

<html> <head> <script> audiophile = new Audio(); function quesfl(qsvalue) { sngno = qsvalue + ".wav"; audiophile.src = sngno; audiophile.play(); } </script> </head> <body> <input type="radio" name="qno" value="02" onClick="quesfl(this.value);">2 <input type="radio" name="qno" value="03" onClick="quesfl(this.value);">3 <input type="radio" name="qno" value="04" onClick="quesfl(this.value);">4 </body> </html>

之所以发生这种情况,是因为每次选择不同的单选按钮时,都会创建一个新的Audio object并进行播放,从而同时播放所有选定的音频文件。 这是从面向对象编程的基本概念中解决的。

Got the solution.

Try the following code.

<html> <head> <script> audiophile = new Audio(); function quesfl(qsvalue) { sngno = qsvalue + ".wav"; audiophile.src = sngno; audiophile.play(); } </script> </head> <body> <input type="radio" name="qno" value="02" onClick="quesfl(this.value);">2 <input type="radio" name="qno" value="03" onClick="quesfl(this.value);">3 <input type="radio" name="qno" value="04" onClick="quesfl(this.value);">4 </body> </html>

It is happening because each time you select a different radio button a new Audio object is created and gets played and hence all of the selected audio files get played simultaneously. This got solved from the very basic concept of Object Oriented Programming.

更多推荐

本文发布于:2023-08-05 07:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1430226.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   音频   声音   javascript   double

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!