我有一组像这样的影城:
let pentagram = [
{
sound: new Audio('somePathtoAudio.wav')
},
{
sound: new Audio('somePathtoAudio.wav')
},
{
sound: new Audio('somePathtoAudio.wav')
},
{
sound: new Audio('somePathtoAudio.wav')
}
];我需要复制这4首录音带,一个接一个地复制每个人1秒。
function play() {
pentagrama[0].audio.play();
pentagrama.forEach((part, i) => {
setTimeout(() => {
part.audio.pause();
part.audio.currentTime = 0;
if (i + 1 < pentagrama.length) pentagrama[i + 1].audio.play();
}, (i + 1) * 1000);
});
}这就是我所做的,我并不真的喜欢它,它有效,但是有更好的方法吗?因为我一直在寻找看起来更优雅的东西,但我还没有找到。
发布于 2018-01-04 15:06:10
是的,有一个更好的方法--使用SoundPlayer.js!
<script type="text/javascript" src="https://gustavgenberg.github.io/handy-front-end/SoundPlayer.js"></script>这会很容易的!
const player = new SoundPlayer();
function play () {
player.load( 'sound1.mp3' ).play( 1 );
player.load( 'sound2.mp3' ).play( 2 );
player.load( 'sound3.mp3' ).play( 3 );
player.load( 'sound4.mp3' ).play( 4 );
}通过播放的数字是秒,直到它被播放。
编辑
如果您希望只播放1秒的声音,请使用:
const player = new SoundPlayer();
function play () {
player.load( 'sound1.mp3' ).play( 1, 1 );
player.load( 'sound2.mp3' ).play( 2, 1 );
player.load( 'sound3.mp3' ).play( 3, 1 );
player.load( 'sound4.mp3' ).play( 4, 1 );
}https://stackoverflow.com/questions/47810448
复制相似问题