Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript音频文件;“上一个/下一个”;文件/歌曲跳过功能?_Javascript_Html_Angularjs - Fatal编程技术网

Javascript音频文件;“上一个/下一个”;文件/歌曲跳过功能?

Javascript音频文件;“上一个/下一个”;文件/歌曲跳过功能?,javascript,html,angularjs,Javascript,Html,Angularjs,我创建了一个简单的音乐播放器 按预期播放、暂停和停止所有工作 但是,我不知道如何使函数在文件之间向前或向后跳过 JS: 这是小部件其余部分的支持代码 var songs = ["violin", "stronger", "boom"] var songIndex = 1; var currentSong = new Audio("music/" + songs[songIndex] + ".mp3") $scope.playStatus = true; 这只需切换播放与暂停:

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

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

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

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;
  }

我在您的代码中发现两个错误:

  • var-songIndex=1
    应该是
    var-songIndex=0
    (您希望从第一个开始)
  • songIndex=songs.length应该是
    songIndex=songs.length-1(等于数组长度的索引超出范围)

@CodeiSir,这对我来说是全新的。你是对的,我不完全理解我的代码,因此我的问题。请有建设性。@CharlesWatson我假设您使用的是Angular(
$scope
),我建议您换一行。因此,
$scope.playPause
将变成
$scope[enter][4 space]。playPause
另请参见: