简体   繁体   English

音频HTML5自动播放WebWorks

[英]Audio HTML5 Autoplay WebWorks

How does one get the HTML5 Audio tag to work in a webworks app with the Autoplay option? 如何通过自动播放选项使HTML5音频标签在Webworks应用程序中工作?

Ex: On document load Audio element's src changed via javascript then audioElement.play() - this doesn't work, you have to bind the click of another link or button to audioElement.play() and click AGAIN... 例如:在文档加载时,音频元素的src通过javascript进行了更改,然后通过audioElement.play()更改-这不起作用,您必须将另一个链接或按钮的单击绑定到audioElement.play()并再次单击。

Any ideas? 有任何想法吗?

function playSongNow(currentID) {
    var audioElement = document.createElement('audio');
    audioElement.setAttribute('autoplay', 'true');
    audioElement.setAttribute('preload', 'true');
    audioElement.setAttribute('src', 'http://' + sessionStorage.url + ':' + sessionStorage.port + '/rest/stream.view?u=' + sessionStorage.user + '&p=' + sessionStorage.pass + '&v=1.6.0&c=BerrySonic&id=' + currentID);
    audioElement.load();
    audioElement.addEventListener("load", function() {
        audioElement.play();
        $(".duration span").html(audioElement.duration);
        $(".filename span").html(audioElement.src);
    }, true);
    $('.play').click(function() {
        audioElement.play();
    });
    $('.pause').click(function() {
        audioElement.pause();
    });
    $('.volumeMax').click(function() {
        audioElement.volume=1;
    });
    $('.volumestop').click(function() {
        audioElement.volume=0;
    });
    $('.stop').click(function() {
        audioElement.pause();
        audioElement.currentTime = 0;
    });
    $('.playatTime').click(function() {
        audioElement.currentTime= 35; //Testing skipping so used 35
        audioElement.play();
    });
}

playSongNow is called by the HTML: HTML调用playSongNow:

<a onclick="playSongNow('2f6d656469612f52616964426f782f4d757369632f4164656c652f32312f4164656c65202d2052756d6f7572204861732049742e6d7033');"><li>Rumour Has It</li></a>

NEW: Actually I think I've narrowed the issue down to : 新:实际​​上,我认为我已经将问题缩小到:

audioElement.addEventListener("load", function() {
    audioElement.play();
    $(".duration span").html(audioElement.duration);
    $(".filename span").html(audioElement.src);
}, true);

I tried using "canplaythrough" But neither works to determine when enough data has passed in to actually play because if you do: 我尝试使用“ canplaythrough”,但都无法确定何时传递了足够的数据以进行实际播放,因为如果这样做:

audioElement.load();
audioElement.play();

I tried the following event: 我尝试了以下事件:

audioElement.addEventListener('canplay', function() {
    alert('canplay');
    audioElement.play();
});

And this works but this doesn't mean that the audio can play without buffering does it? 这可行,但这并不意味着音频可以在不缓冲的情况下播放吗? It also immidiately fires the "canplaythrough" event. 它也立即触发“ canplaythrough”事件。

Some guidance on these events would be handy. 对这些事件的一些指导将很方便。

You can handle this with the ended event like the following : 您可以使用ended事件处理此事件,如下所示:

audioElement.addEventListener('ended', function(){
playSongNow(......)
}, false);

canplaythrough不能在黑莓上启动,但是canplay可以。...不要问我为什么....

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

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