在上面的音频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);">4In 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.
更多推荐
发布评论