简体   繁体   English

在移动浏览器上播放随机音轨

[英]Playing Random Audio Tracks on mobile browsers

Objective: 目的:

User taps a button. 用户点击一个按钮。

A random audio track is selected (among 3 tracks) and plays in the background. 选择一个随机音轨(在3个音轨中)并在后台播放。

Problem: The track plays on desktop Chrome and Firefox, but does not play on Mobile Chrome. 问题:该曲目可在台式机Chrome和Firefox上播放,但不能在Mobile Chrome上播放。

JS: JS:

    function play() {
        var a = Math.random() * 3;
        a = Math.floor(a);

        if (a == 0) {
            document.getElementById('soundtrack').innerHTML = "<audio id='background_audio1' loop autoplay><source src='0.mp3' type='audio/ogg'>Your browser does not support the audio element.</audio>";
        }
        if (a == 1) {
            document.getElementById('soundtrack').innerHTML = "<audio id='background_audio1' loop autoplay><source src='1.mp3' type='audio/ogg'>Your browser does not support the audio element.</audio>";
        }
        if (a == 2) {
            document.getElementById('soundtrack').innerHTML = "<audio id='background_audio1' loop autoplay><source src='2.mp3' type='audio/ogg'>Your browser does not support the audio element.</audio>";
        }
    }

HTML: HTML:

    <a class="button" onclick="play()"><img src="image.png"/></a>

    <div id="soundtrack"></div>

How do I make this play on Mobile browsers? 如何在移动浏览器中播放该视频? This is event driven (tap button) and not auto-play. 这是事件驱动的(点击按钮),而不是自动播放。 It should play but does not. 它应该播放但不播放。

Actually it appears that it is autoplay, because you're not playing the file on event (tap), you're simply loading new HTML code which then requests for an autoplay. 实际上,它似乎是自动播放的,因为您没有在事件(tap)上播放文件,因此,您只是在加载新的HTML代码,然后请求自动播放。

It might work if you request the play action in javascript immediately after replacing the HTML: document.getElementById('background_audio1').play(); 如果您在替换HTML之后立即在javascript中请求播放操作,则可能会起作用: document.getElementById('background_audio1').play();

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

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