我想在后台的网页上播放一个音乐播放列表,我使用的是铬,我不想要控制台,我想要自动播放,但是,虽然我写了自动播放,但是音频只从控制台的play按钮开始。这就是我在HTML中所写的:
<div id="music_list">
<audio controls autoplay></audio>
</div>在Javascript里
(function () {
// Playlist array
var files = [
"ms4/14_1.30.mp3",
"ms4/20_20.mp3",
"ms4/21_34.mp3"
];
// Current index of the files array
var i = 0;
// Get the audio element
var music_player = document.querySelector("#music_list audio");
// function for moving to next audio file
function next() {
// Check for last audio file in the playlist
if (i === files.length - 1) {
i = 0;
} else {
i++;
}
// Change the audio element source
music_player.src = files[i];
}
// Check if the player is slected
if (music_player === null) {
throw "Playlist Player does not exists ...";
} else {
// Start the player
music_player.src = files[i];
// Listen for the music ended event, to play the next audio file
music_player.addEventListener('ended', next, false)
}
})();我怎么才能解决呢?我在HTML和JS方面真的很新,我被困在这个问题上了。
发布于 2020-11-14 17:26:24
我一年前就遇到这个问题了。问题在于你使用的互联网浏览器,例如,在谷歌Chrome控件中,自动播放不起作用,问题是用户的权限、安全性和隐私。
我认为有一些方法可以让它在任何互联网浏览器中工作,但是,“控制自动播放”不起作用,只是在一些互联网探索者。
发布于 2020-11-14 17:30:23
Bug玩家认为自动播放是一件坏事,因为广告。最后,你将被迫点击来玩。现在,您可以尝试在自动播放的同时向音频元素添加静音属性,并在执行第一个src之后删除它。
发布于 2020-11-14 17:31:17
这取决于您的浏览器,在不同的浏览器中运行代码,并尝试这个javascript函数。
var mp3 = document.getElementByTagName("audio");
mp3.autoplay = true;
mp3.load();https://stackoverflow.com/questions/64836582
复制相似问题