简体   繁体   English

一次播放音频文件

[英]play an audio file once

is there a way to play a mp3/wav file in html only once if the condition is met. 如果满足条件,有没有办法只播放一次html中的mp3 / wav文件。 I am trying to do but the mp3 file is not playing after my required condition is met.. 我正在尝试这样做,但是在满足我的要求的条件后mp3文件没有播放。

<input type="text" id="question"/>
<audio src="one.mp3" id="one" preload="auto"></audio>
<input type="text" id="question1"/>
<audio src="two.mp3" id="two" preload="auto"></audio>

<script>
$("#question").blur(function() {
    if ($(this).val() == 'Paris') {
   document.getElementById("one").play();
    }
});    

$("#question1").blur(function() {
    if ($(this).val() == 'France') {
   document.getElementById("two").play();
    }
});    
</script>

Assuming that your condition is truly met, there are a few reasons why you might not be able to hear your sound play: 假设您的条件确实得到满足,则可能由于以下原因您可能听不到声音播放:

  • Your browser does not support the audio type. 您的浏览器不支持音频类型。
    • See the table here to find out if your browser supports your specific type of media (in this case, MP3 ). 请参阅此处的表以了解您的浏览器是否支持您特定类型的媒体(在这种情况下为MP3 )。
    • Another way you can check for this is by using the example . 您可以通过以下示例检查此情况的另一种方法。
    • If your browser does not accept your media type, get the proper media file and add it as a source as shown in the example . 如果您的浏览器不接受您的媒体类型,请获取正确的媒体文件并将其添加为source ,如示例所示。
  • The media file cannot be found. 找不到媒体文件。
    • Make sure that your media file can be found by the browser. 确保浏览器可以找到您的媒体文件。
    • You can check this by heading to the media's src attribute (in this case, ./one.mp3 or ./two.mp3 ). 您可以通过转到媒体的src属性(在本例中为./one.mp3./two.mp3 )进行检查。
  • Your volume is silent. 您的音量没有声音。
    • Although this may seem silly, a hard day of work can leave one forgetful. 尽管这看起来很愚蠢,但辛苦的一天可能会使人遗忘。
    • Check that your speakers / headphones are working and that your volume is at a high level. 检查您的扬声器/耳机是否工作正常并且音量是否较高。
  • Last but not least, the file is corrupted, damaged, or contains no information. 最后但并非最不重要的一点是,该文件已损坏,损坏或不包含任何信息。
    • To check for this, play the media file on your laptop. 要检查这一点,请在笔记本电脑上播放媒体文件。 Your program should let you know if the file is corrupted or damaged. 您的程序应让您知道文件是否已损坏。
    • Check the file's size to make sure there is information in it. 检查文件的大小,以确保其中包含信息。

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

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