Javascript音频随机停止 [英] Javascript audio randomly stopping

查看:81
本文介绍了Javascript音频随机停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在进行的一个项目中,我有一个JS文件可以播放一段音乐,当这个片段完成播放时,下一个就取代它。



  var  soundtrack = {
0 new 音频( ./ music / piece1。 ogg),
2 new 音频( ./ music / piece2.ogg),
1 new 音频( ./ music / piece3.ogg),
3 new 音频( ./ music / piece4.ogg
}
var cur rentTrack = 0 ;
function startMusic(){
soundtrack [currentTrack] .play();
soundtrack [currentTrack] .addEventListener( 已结束功能(){nextSong()});
}
function nextSong(){
currentTrack + = 1 ;
if (currentTrack == 4 ){
currentTrack = 0 ;
}
soundtrack [currentTrack] .play();
}
$( document ).ready( function (){
setTimeout( function (){startMusic();}, 15500 );
} );



注意:我对对象soundtrack的使用可能是非常规的,但是有一个使用数组的错误,所以我尝试了这个并且它有效。



第一部分正常,下一部分很好。大约10秒钟,播放就停止了。



任何帮助解决这个问题都将非常感谢,谢谢!



注意:我知道不是所有的作品都会在一个之后播放,我等到这个问题得到解决才继续。



我尝试过:



重新订购件



Google



stackoverflow



删除事件监听器,因为有人建议可能是问题

解决方案

document ).ready( function (){
setTimeout( function (){startMusic();}, 15500 );
});



注意:我对对象soundtrack的使用可能是非常规的,但是有一个使用数组的错误,所以我尝试了这个并且它有效。



第一个片段播放精细,下一个开始很好。大约10秒钟,播放就停止了。



任何帮助解决这个问题都将非常感谢,谢谢!



注意:我知道不是所有的作品都会在一个之后播放,我等到这个问题得到解决才继续。



我尝试过:



重新订购件



Google



stackoverflow



删除事件监听器,因为有人建议可能是问题


< blockquote>

引用:

第一部分播放正常,下一部分播放正常。大约10秒钟,比赛暂停。

我不知道为什么第二件在10秒后停止,但我知道为什么它不去第三件。

startMusic()中,当你应该将事件监听器添加到每个部分时,你将一个事件监听器添加到0,因为你有4个音频 objects。


In a project I'm working on, I have a JS file that should play a piece of music, and when that piece is done playing, the next one takes its place.

var soundtrack = {
 0: new Audio("./music/piece1.ogg"),
 2: new Audio("./music/piece2.ogg"),
 1: new Audio("./music/piece3.ogg"),
 3: new Audio("./music/piece4.ogg")
}
var currentTrack = 0;
function startMusic(){
 soundtrack[currentTrack].play();
 soundtrack[currentTrack].addEventListener("ended", function(){ nextSong()});
}
function nextSong(){
 currentTrack += 1;
 if(currentTrack == 4){
     currentTrack = 0;
 }
 soundtrack[currentTrack].play();
}
$( document ).ready(function() {
 setTimeout(function() { startMusic(); }, 15500);
});


Note: My use of the object "soundtrack" may be unconventional, but there was a bug using an array so I tried this and it worked.

The first piece plays through fine, and the next one starts up fine. About maybe 10 seconds in, the playing just stops.

Any help to solve this issue would be greatly appreciated, thanks!

Note: I am aware not all pieces will play after one other, I'm waiting until this issue is fixed before continuing.

What I have tried:

Reordering the pieces

Google

stackoverflow

Removing the event listener as someone suggested it may be the problem

解决方案

( document ).ready(function() { setTimeout(function() { startMusic(); }, 15500); });


Note: My use of the object "soundtrack" may be unconventional, but there was a bug using an array so I tried this and it worked.

The first piece plays through fine, and the next one starts up fine. About maybe 10 seconds in, the playing just stops.

Any help to solve this issue would be greatly appreciated, thanks!

Note: I am aware not all pieces will play after one other, I'm waiting until this issue is fixed before continuing.

What I have tried:

Reordering the pieces

Google

stackoverflow

Removing the event listener as someone suggested it may be the problem


Quote:

The first piece plays through fine, and the next one starts up fine. About maybe 10 seconds in, the playing just stops.

I don't know why second piece stops after 10 seconds, but I know a reason why it don't go to third piece.
In startMusic(), you add an event listener to piece 0 when you should add the event listener to every piece since you have 4 audio objects.


这篇关于Javascript音频随机停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆