我有四个音频。让我们说s1,s2,s3和s4。我还有一个变量,其中一个和四个之间的随机整数,例如myVar = [2,1,3,4]。它可以长于或短于四个,但其中的整数总是在1到4之间。
我想创建一个循环,它将在myVar上查看并以相应的顺序播放我的声音(在这种情况下:s2,s1,s3,s4)。一个接一个的声音,这是非常重要的。不是一次全部。
到目前为止,我的所有解决方案都会立即播放声音。我知道有几个类似的问题,但我在JavaScript上非常糟糕,我无法使用答案。有谁知道怎么做?
当前解决方案之一(不起作用):
library(dplyr)
dft1 <- dft1 %>%
mutate(Presence = case_when(# If R.Detect is "moved" or "open" or "closed"
# AND ( (EP.Detect on previous row is "Movement" AND LP.Detect on previous row is "Movement" or "TimeOut)
# OR (EP.Detect on previous row is "TimeOut" AND LP.Detect on previous row is "Movement")
# )
# THEN set Presence to 1
.$R.Detect %in% c("moved","open", "closed")
& ( (lag(.$EP.Detect) == "Movement" & lag(.$LP.Detect) %in% c("Movement","TimeOut"))
| (lag(.$EP.Detect) == "TimeOut" & lag(.$LP.Detect) == "Movement")
) ~ 1,
# All other cases, set Presence to 0
TRUE ~ 0
)
)
答案 0 :(得分:2)
使用onended
事件播放下一首歌,如下所示:
var musics = ['s1', 's2', 's3', 's4'];
var order = [2,1,3,4];
var player; //get your audio tag here
player.autoplay = true;
function playNext() {
var current = order.shift();
player.src = musics[current];
}
player.addEventListener('ended', playNext);
playNext();
答案 1 :(得分:0)
也可以有一个没有'order'数组突变的解决方案,例如使用Symbol.iterator
为每个ended
事件调用:
var a1 = new Audio('http://developer.mozilla.org/@api/deki/files/2926/=AudioTest_(1).ogg'); a1.id = "1"; // id helps to see console.log is fine
var a2 = new Audio('http://developer.mozilla.org/@api/deki/files/2926/=AudioTest_(1).ogg'); a2.id = "2";
var a3 = new Audio('http://developer.mozilla.org/@api/deki/files/2926/=AudioTest_(1).ogg'); a3.id = "3";
var a4 = new Audio('http://developer.mozilla.org/@api/deki/files/2926/=AudioTest_(1).ogg'); a4.id = "4";
var order = [2, 1, 4, 3, 2, 1];
rotateAudio(order); // main function; pass order array here
function rotateAudio(order) {
var orderMap = {1: a1, 2: a2, 3: a3, 4: a4};
var iterator = order[Symbol.iterator]();
var value, done;
function rotate({value, done}) { // get next iteration
function next(e) {
e.target.removeEventListener('ended', next); // helps for garbage removal I guess
rotate(iterator.next())
};
if (done) { console.log('done!'); return };
var current = orderMap[value];
console.log('current: ', current);
current.play();
current.addEventListener('ended', next) // listen to 'ended' on audio, start next one
}
rotate(iterator.next()); // first call
}
答案 2 :(得分:0)
使用.onended代替.addEventListener,它非常可靠。
var musics = ['s1', 's2', 's3', 's4'];
var playArray=function(){
if(order[]!=null){
var voice =musics[0];
voice.play();
order.shift();
voice.onended = function () {
playArray();
};
}
}