Javascript音频文件;“上一个/下一个”;文件/歌曲跳过功能?
我创建了一个简单的音乐播放器 按预期播放、暂停和停止所有工作 但是,我不知道如何使函数在文件之间向前或向后跳过 JS: 这是小部件其余部分的支持代码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; 这只需切换播放与暂停:
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代码>(等于数组长度的索引超出范围)
$scope
),我建议您换一行。因此,$scope.playPause
将变成$scope[enter][4 space]。playPause
另请参见: