javascript - 如何在面向对象的HTML5 JS音频播放器中显示可点击的播放列表?

标签 javascript jquery html audio playlist

我有一个使用HTML5和Javascript(带有一些基本CSS)构建的可操作音频播放器,但是我想添加一个可单击的播放列表,因此将显示正在播放的音轨的名称,并可以单击歌曲并立即开始播放。

这是我的JS:

function Jukebox() {
  this.playlist = ['song1.mp3', 'song2.mp3', 'song3.mp3', 'song4.mp3']
  this.currentIndex = 0
  this.audio = $('#audioPlayer')[0]
  this.audio.src = this.playlist[this.currentIndex]

  // console.log(this.audio)

  this.play = function(){
      this.audio.play();
      // console.log(this.currentIndex)
  }

  this.pause = function(){
      this.audio.pause();
  }

  this.stop = function(){
      this.audio.pause();
      this.audio.currentTime = 0;
  }

  this.next = function(){
      this.audio.pause();
      this.currentIndex++;
      if(this.currentIndex == this.playlist.length){
        this.currentIndex = 0
      };
      this.audio.src = this.playlist[this.currentIndex];
      this.play();
  }

  this.prev = function(){
      this.audio.pause();
      this.currentIndex--;
      if(this.currentIndex <= 0){
        this.currentIndex = this.playlist.length - 1
      };
      this.audio.src = this.playlist[this.currentIndex];
      this.play();
  }

}

var thisIsMyJukebox = new Jukebox()


//pass the play function by reference
$('#playBtn').click(function(){
  thisIsMyJukebox.play()
})
$('#pauseBtn').click(function(){
  thisIsMyJukebox.pause()
})
$('#stopBtn').click(function(){
  thisIsMyJukebox.stop()
})
$('#prevBtn').click(function(){
  thisIsMyJukebox.prev()
})
$('#nextBtn').click(function(){
  thisIsMyJukebox.next()
})

});

音频文件位于我的硬盘驱动器上,并且歌曲具有我可以写的专有名称,它们在代码文件夹中只是用这种方式命名,因为它更短。

谢谢你的帮助。

最佳答案

如果您想使用Bootstrap,则只需将所有歌曲添加为按钮列表即可。 ta田

关于javascript - 如何在面向对象的HTML5 JS音频播放器中显示可点击的播放列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44766528/

相关文章:

jquery - 此请求是否使用 SSL 加密?

javascript - 用与图像中心点相关的 x,y 绘制图像?

css - 当前上下文中不存在名称 'media'

javascript - NodeJS ZIP 文件下载损坏

javascript - 使用jQuery获取第N个元素的数据属性值

javascript - 如何在拖动或单击图像后删除奇怪的 a/img 边框或下划线?

html - Modernizr 检查 CSS3 和 css 动画将不起作用

javascript - 使用页面重新加载和 iframe 对 JS 应用程序进行单元测试

javascript - 如何定位和禁用 UL 列表中的第 n 个 LI?

javascript - 如何动态更改此随机数生成器的曲线?