Javascript 每次传递新数组时,需要卸载(重新初始化)以前存在的howler(Howlerjs)
我使用howlerjs()按顺序播放12个mp3文件的随机数组。 每次运行Javascript 每次传递新数组时,需要卸载(重新初始化)以前存在的howler(Howlerjs),javascript,audio,mp3,media,Javascript,Audio,Mp3,Media,我使用howlerjs()按顺序播放12个mp3文件的随机数组。 每次运行randomise()函数时,每次传递新数组时,我都需要重新初始化howl。 有一个unload()方法,但我无法让它工作。 有人能帮忙吗? 代码如下: // Constants // Multidimensional array with all (unique) filenames in their columns const fileName = [ ["a01", "b01", "c01", "d01", "e
randomise()
函数时,每次传递新数组时,我都需要重新初始化howl
。
有一个unload()
方法,但我无法让它工作。
有人能帮忙吗?
代码如下:
// Constants
// Multidimensional array with all (unique) filenames in their columns
const fileName = [
["a01", "b01", "c01", "d01", "e01", "f01"],
["a02", "b02", "c02", "d02", "e02", "f02"],
["a03", "b03", "c03", "d03", "e03", "f03"],
["a04", "b04", "c04", "d04", "e04", "f04"],
["a05", "b05", "c05", "d05", "e05", "f05"],
["a06", "b06", "c06", "d06", "e06", "f06"],
["a07", "b07", "c07", "d07", "e07", "f07"],
["a08", "b08", "c08", "d08", "e08", "f08"],
["a09", "b09", "c09", "d09", "e09", "f09"],
["a10", "b10", "c10", "d10", "e10", "f10"],
["a11", "b11", "c11", "d11", "e11", "f11"],
["a12", "b12", "c12", "d12", "e12", "f12"]
];
// Variables
let randomSelection = [];
let sequence = [];
// Functions
// Randomise array
function randomise() {
sequence = fileName.map(option => {
const random = Math.floor(Math.random() * 6);
return option[random];
});
randomSelection = sequence.map(createURL);
function createURL(fileName) {
return `assets/audacity/${fileName}.mp3`;
}
defineSong();
console.log(randomSelection);
}
function createSequence(bars) {
let allHowls = [];
for (var i = 0; i <= bars; i++) {
let howl = new Howl({
src: [randomSelection[i]],
loop: false
});
allHowls.push(howl);
}
return allHowls;
}
function defineSong() {
let isPlaying = false;
let soundFiles = [];
if (soundFiles.length) {
soundFiles.forEach(sound => {
sound.unload();
});
}
soundFiles = createSequence(12);
// using var because let isn't in scope
for (var i = 0; i < soundFiles.length - 1; ++i) {
soundFiles[i].on(
"end",
(function(i) {
return function() {
soundFiles[i + 1].play();
};
})(i)
);
}
soundFiles[i].on("end", function() {
isPlaying = false;
});
handlers(isPlaying, soundFiles);
}
// Event Listeners
// Grid play event
$(".bar").on("click", function() {
let id = this.id;
let barPath = `assets/music/${id}.mp3`;
let bar = new Audio(barPath);
bar.play();
});
// Randomise event
$("#btn-randomise").on("click", randomise);
// Play & Pause event
function handlers(playing, soundFiles) {
let playButton = $("#play-minuetto"),
pauseButton = $("#pause-button");
playButton.on("click", function() {
if (!playing) {
isPlaying = true;
soundFiles[0].play();
}
});
pauseButton.on("click", function() {
if (playing) {
isPlaying = false;
soundFiles[0].stop();
}
});
}
// Calls randomise() on load to have a valid array in case
// playSong() is called from the page before randomising
randomise();
$(window).on("load", defineSong);
//常数
//列中包含所有(唯一)文件名的多维数组
常量文件名=[
[“a01”、“b01”、“c01”、“d01”、“e01”、“f01”],
[“a02”、“b02”、“c02”、“d02”、“e02”、“f02”],
[“a03”、“b03”、“c03”、“d03”、“e03”、“f03”],
[“a04”、“b04”、“c04”、“d04”、“e04”、“f04”],
[“a05”、“b05”、“c05”、“d05”、“e05”、“f05”],
[“a06”、“b06”、“c06”、“d06”、“e06”、“f06”],
[“a07”、“b07”、“c07”、“d07”、“e07”、“f07”],
[“a08”、“b08”、“c08”、“d08”、“e08”、“f08”],
[“a09”、“b09”、“c09”、“d09”、“e09”、“f09”],
[“a10”、“b10”、“c10”、“d10”、“e10”、“f10”],
[“a11”、“b11”、“c11”、“d11”、“e11”、“f11”],
[“a12”、“b12”、“c12”、“d12”、“e12”、“f12”]
];
//变数
让随机选择=[];
让序列=[];
//功能
//随机阵列
函数随机化(){
序列=文件名.map(选项=>{
const random=Math.floor(Math.random()*6);
返回选项[随机];
});
randomSelection=sequence.map(createURL);
函数createURL(文件名){
返回`assets/audacity/${fileName}.mp3`;
}
defineSong();
控制台日志(随机选择);
}
函数createSequence(条){
让所有的人都嚎啕大哭=[];
对于(var i=0;i{
sound.unload();
});
}
声音文件=createSequence(12);
//使用var,因为let不在范围内
对于(var i=0;i