简体   繁体   English

如何使用 javascript 从 mp3 数组中随机播放音频

[英]How to play an audio randomly from an array of mp3 using javascript

I have here a button, where if a user presses it, will play a background music.我这里有一个按钮,如果用户按下它,它将播放背景音乐。 If pressed again, it will then pause the music.如果再次按下,它将暂停音乐。

This works fine for single mp3 file, but I wanted to add more songs for variation.这适用于单个 mp3 文件,但我想添加更多歌曲以进行变化。

I don't want to make it a complicated music player.我不想让它成为一个复杂的音乐播放器。 Just a single button that play/pause the music and when bg1 is done, play next one in the array.只需一个按钮即可播放/暂停音乐,当bg1完成后,播放数组中的下一个。

Also, I'm hoping if the music to played could be selected at random?另外,我希望是否可以随机选择要播放的音乐?

Please take a look at what my code looks like so far:请看一下我的代码到目前为止的样子:

 var myAudio = document.getElementById("myAudio"); var isPlaying = false; function togglePlay() { isPlaying? myAudio.pause(): myAudio.play(); }; myAudio.onplaying = function() { isPlaying = true; }; myAudio.onpause = function() { isPlaying = false; };
 <audio id="myAudio" src="bg1.mp3" preload="auto" loop></audio> <a href="#" onclick="togglePlay()">Play Music</a>

And here's my trial script for array, but I cannot finish it myself这是我的数组试用脚本,但我自己无法完成

let musicPlayer=[

`bg1.mp3`,
`bg2.mp3`,
`bg3.mp3`,
`bg4.mp3`

];

function playMusic(){

    let index=Math.floor(Math.random()*musicPlayer.length);
    
    let div=document.querySelector('#music');

Lastly, I'm planning to make an array of about 70 mp3 files which can cause long loading time.最后,我打算制作一个包含大约 70 个 mp3 文件的数组,这可能会导致加载时间过长。 Can it be made so that it won't load until its turn.可以制作成直到轮到它才加载。 Sort of like lazy loading for audio files?有点像音频文件的lazy loading

I would really appreciate it.我真的很感激。 Thanks a lot in advance!提前非常感谢!

I would tackle it following way.我会按照以下方式解决它。

 const audio = document.querySelector('#audio'); const audioButton = document.querySelector('#audioButton'); const audioUrls = [ "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3" ]; const randomAudio = () => { const index = Math.floor(Math.random() * audioUrls.length); const audioUrl = audioUrls[index]; return audioUrl; } audioButton.addEventListener("click", () => { audio.addEventListener("ended", () => { const audioUrl = randomAudio(); const temp = new Audio(); temp.addEventListener("loadeddata", () => { audio.src = audioUrl; }); temp.src = audioUrl; }) const audioUrl = randomAudio(); audio.addEventListener("loadeddata", () => { audio.play(); }); audio.src = audioUrl; })
 <audio id="audio" src="" preload="auto" loop></audio> <a id="audioButton" href="#">Play Music</a>

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

相关问题 如何使用 Javascript 在浏览器中播放从 Google Text 到 Speech 的 base64 响应作为 mp3 音频 - How to play a base64 response from Google Text to Speech as an mp3 audio in browser using Javascript FF可以播放mp3,但不能使用javascript音频API - FF can play mp3, but not using the javascript Audio API 如何使用HTML5或JavaScript中的动态URL播放MP3音频? - How to play MP3 audio with dynamic URL in HTML5 or JavaScript? 通过javascript在标签音频中播放mp3 - play mp3 in tag audio by javascript 如何使用JavaScript在MVC项目中播放mp3文件? - How to play mp3 file in a mvc project using javascript? 如何使用JavaScript在phonegap中播放mp3文件 - how to play mp3 file in phonegap using javascript 如何在网页上播放.mp3或.m4a音频并同时显示嵌入式专辑封面? (使用Javascript,HTML5或Flash) - How to play a .mp3 or .m4a audio on a webpage and show the embedded album art at the same time? (using Javascript, HTML5, or Flash) Javascript - 从 mp3 文件录制音频 - Javascript - Record audio from mp3 file 如何使用javascript随机播放音频标签中的多个音频文件? - How to randomly play multiple audio files in audio tag using javascript? 使用 javascript 播放 mp3 文件 - Play mp3 file using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM