简体   繁体   English

HTML5音频-声音只能播放一次

[英]HTML5 Audio - Sound only plays once

I am using Chrome and this is my code: 我正在使用Chrome,这是我的代码:

<audio id="letter_audio" src="audio/bomb.mp3" type="audio/mp3" preload="auto"></audio>

    $('.sound').live('click', function () {

        var sound = $("#letter_audio")[0];  

        sound.pause();
        sound.currentTime = 0;
        sound.play();

        return false;
    });

How come the sound plays the first time I click on play. 第一次单击播放时声音如何播放。 then when I click again, it pauses, like the code goes. 然后当我再次单击时,它会暂停,就像代码一样。 but the it justs stays on pause? 但是它停留在暂停状态吗? i want it to play, then when i click on it again, go back to 0 and play again as in the code? 我希望它播放,然后当我再次单击它时,返回到0并再次播放,如代码所示?

I've had similar problems replaying audio, but managed to solve it by calling the load() method before the play() call. 我在播放音频时也遇到了类似的问题,但是设法通过在play()调用之前调用load()方法来解决它。 Try this: 尝试这个:

$('.sound').live('click', function () {
    var sound = $("#letter_audio")[0];

    sound.load();
    sound.play();

    return false;
});

I think this is better than resetting the src attribute for two reasons: 我认为这比重置src属性更好,原因有两个:

  1. Keeps the src value in the page content, rather than in the JavaScript src值保留在页面内容中,而不是在JavaScript中
  2. Works when you embed multiple <source> elements of different audio formats: 当您嵌入不同音频格式的多个<source>元素时有效:

     <audio id="letter_audio"> <source src="audio/bomb.mp3" type="audio/mp3" preload="auto"/> <source src="audio/bomb.ogg" type="audio/ogg" preload="auto"/> </audio> 

try this.. basically, resetting the src attribute should reset the audio as desired. 尝试一下..基本上,重设src属性应按需要重设音频。

$('.sound').live('click', function () {

    var sound = $("#letter_audio")[0];  

    sound.src = "audio/bomb.mp3";
    sound.play();

    return false;
});

it seems that for the time being the currentTime attribute is read-only (and in 2011 it has got a wont-fix status ) 似乎目前currentTime属性是只读的(并且在2011年,它的状态不会修复

however, after setting src on your audio-object (again) - like also proposed by @Lloyd - the currentTime attribute becomes settable (at least on a the current chrome-version for linux (24.0.1312.69)). 但是,在(再次)在音频对象上设置src之后(如@Lloyd所建议的那样),currentTime属性变为可设置的(至少在Linux的当前chrome版本(24.0.1312.69)上)。

this means by adding the strange line 这意味着添加奇怪的行

audio.src = audio.src

your example does what it is supposed to. 您的示例执行了预期的操作。

in full: 在全:

$('.sound').live('click', function () {

    var sound = $("#letter_audio")[0];  
    sound.src = sound.src;  
    sound.pause();
    sound.currentTime = 0;
    sound.play();

    return false;
});

as it is enough set the src once, you might want to call it somewhere during the setup process. 由于一次设置src就足够了,您可能需要在设置过程中将其调用。

you can use php to add a query at the end of the url with time(); 您可以使用php使用time()在网址末尾添加查询;

<source src="URLtoTrack.mp3?<?php echo time(); ?>" />

works for me. 为我工作。

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

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