简体   繁体   English

如何更改此功能,以便每次单击一次只能播放一首随机歌曲? (JavaScript)

[英]How do I alter this function so that only one, random song can be played at a time with each click? (JavaScript)

I'm essentially building a music generator/player app using HTML, CSS, and vanilla JavaScript where every time a user clicks a particular div with respect to a particular genre of music, a new song from that genre is played.我基本上是在使用 HTML、CSS 和 vanilla JavaScript 构建一个音乐生成器/播放器应用程序,每次用户单击特定音乐类型的特定 div 时,都会播放该类型的新歌曲。 To generate a random song, I created this JS function:为了生成一首随机歌曲,我创建了这个 JS 函数:

const randomizeSongs = playlist => {
    let song = playlist[Math.floor(Math.random() * playlist.length)];
    song.play();
  }

The issue I've run into is when I tried using the following code:我遇到的问题是当我尝试使用以下代码时:

const pop = document.querySelector(".pop") // Div containing audio elements
pop.addEventListener("click", (e) => {
      const popSongs = document.querySelectorAll(".pop audio");
      randomizeSongs(popSongs);
})

the function generated a new song with each click like I wanted to, but the previous songs generated with previous clicks didn't stop playing.该功能像我想要的那样每次点击都会生成一首新歌曲,但是以前点击生成的以前的歌曲并没有停止播放。 How can I modify the randomizeSongs function so that only one random song can play at a time with each new click?如何修改 randomizeSongs 功能,以便每次新点击一次只能播放一首随机歌曲?

I think you're looking for pause ( https://www.w3schools.com/jsref/met_audio_play.asp ).我认为您正在寻找pausehttps://www.w3schools.com/jsref/met_audio_play.asp )。

Haven't tried this but it would look like:没有试过这个,但它看起来像:

let previousSong;
const randomizeSongs = playlist => {
    if (previousSong) {
        previousSong.pause();
    }
    let song = playlist[Math.floor(Math.random() * playlist.length)];
    song.play();
    previousSong = song;
}

暂无
暂无

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

相关问题 如何使我的图片工作,以便每次玩游戏时只能单击一次? - How can I get my pictures to work so that they can only be clicked one time each time the game is played? 当其中一个停止 javascript 时,如何播放其他音频/歌曲? - How can i make other audio/song be played when one of them stops javascript? 播放歌曲时,如何让我的音乐机器人从 function 发送嵌入? - How do i make my music bot send embed from a function when song is played? 在javascript中,如何删除以前单击的兄弟div,以便一次仅显示一个答案? - In javascript, how do I remove previously clicked sibling div, so that only one answer displays at a time? 我如何使用 javascript 来制作它,以便用户只能在 Lottie 动画完全播放后触发悬停事件? - How can I use javascript to make it so the user can only trigger the hover event once the Lottie animation has played in full? 如何使 onclick function 不仅工作一次,而且每次我在纯香草 javascript 中单击它 - how to make onclick function work not only once but each time i click it in pure vanila javascript 如何确保我的click函数只用Jquery执行一次? - How can I make sure that my click function executes one time only with Jquery? onChange function 一键停止,我需要写什么代码才能无限期地继续? (Javascript) - onChange function stops after one click, what code do I need to write so that it continues indefinitely? (Javascript) 如何将一次性点击事件添加到函数中? - How can I add an event for a one time click to a function? jQuery / JavaScript如何获取单击它的按钮的值,每行按钮只能激活一个按钮 - jQuery/JavaScript how to get value of button that click on it, each row of buttons can only active only one button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM