我创建了一个简单的音乐播放器。
按预期播放、暂停和停止所有工作。
但是,我不知道如何让函数在文件之间向前或向后跳转。
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/