开发者

Changing src attribute of HTML5 audio element

开发者 https://www.devze.com 2023-01-17 17:20 出处:网络
I am trying use JS to playback some sounds. What i want to do is have piece of music playing but after a certain time replace it with another audio file. I thought the most effective way would be to r

I am trying use JS to playback some sounds. What i want to do is have piece of music playing but after a certain time replace it with another audio file. I thought the most effective way would be to reuse the same audio object, but开发者_如何学C it seems to not kill the original sound so all gets messy. What is the best way to do this?

My code is below, all im doing is passing in a new source at a certain time

function inGameSndCreate(src) {
    inGameSnd = new Audio(src)
    inGameSnd.loop = true;
    inGameSnd.play();
}

thanks in advance


I expect you'll need to change the source in the DOM using JavaScript. The following should work.

document.getElementById('audio_id').src = 'different_file.wav';
0

精彩评论

暂无评论...
验证码 换一张
取 消