Javascript 音频文件 "previous/next"文件/歌曲跳过功能?

标签 javascript html angularjs

我创建了一个简单的音乐播放器。

按预期播放、暂停和停止所有工作。

但是,我不知道如何让函数在文件之间向前或向后跳转。

JS:

这是小部件其余部分的支持代码。

  var songs = ["violin", "stronger", "boom"]
  var songIndex = 1;
  var currentSong = new Audio("music/" + songs[songIndex] + ".mp3")
  $scope.playStatus = true;

这只是简单地切换播放与暂停:

  $scope.playPause = function() {
    if (!$scope.playStatus) currentSong.pause();
    else currentSong.play();
    $scope.playStatus = !$scope.playStatus;
  }

这会完全停止歌曲并将其返回到开头:

  $scope.stopSong = function() {
    currentSong.pause();
    currentSong.currentTime = 0;
    $scope.playStatus = true;
  }

这是我尝试创建“上一首歌曲”功能的地方:

  $scope.previousSong = function() {
    currentSong.pause()
    if (songIndex === 0) {
      songIndex = songs.length;
    } else {
      songIndex -= 1;
    }
    currentSong.play()
    $scope.playStatus = true;
  }

最后一个函数是我期望的最简单的逻辑。

暂停歌曲,在歌曲数组中向后移动一个索引,然后播放。然而,它只是继续播放同一首歌曲。没有给出错误。

最佳答案

这是因为您没有加载正确的 mp3。尝试类似的东西

  $scope.previousSong = function() {
    currentSong.pause()
    if (songIndex === 0) {
      songIndex = songs.length - 1;
    } else {
      songIndex -= 1;
    }
    currentSong = new Audio("music/" + songs[songIndex] + ".mp3"); // reload the new song
    currentSong.play();
    $scope.playStatus = true;
  }

关于Javascript 音频文件 "previous/next"文件/歌曲跳过功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33969751/

相关文章:

javascript - 颜色框语法错误 : unrecognized expression

html - 为什么属性 "perspective"对我不起作用?

css - 使主页为屏幕高度的 100%

javascript - Angular : check value ng-blur

javascript - 如何在 Protractor 中单击同一个按钮超过 50 次?

javascript - 循环对象的顺序可能仅在迭代期间被破坏?

javascript - 无服务器框架 : All of a sudden receiving "ERROR TypeError: e is not a function" after deployment of service

javascript - 从 session 数组中删除项目 Jquery Ajax

html - css 高度和溢出

javascript - 在 Angular 中,如何将 JSON 对象/数组传递给指令?