html5 play(),html - HTML5 - Play Audio - Stack Overflow

It looks to me like your issue is not really related to audio but instead how to implement the buttons to get the effect you want. You say the code you posted works, so play and pause are working fine on your audio element. But the 3 buttons are starting the 3 different sounds and the last button stops any sound currently being played, whereas you want to select a sound with one of the 3 buttons and play/pause the selected one using the last button.

If you have a variable to store the id of the last clicked sound select buttons, then use the last button to toggle between calling play() and pause()...

var selSound = 8000; //default to first sound.

var play = false; //default to paused.

function selectSound(id) {

selSound = id;

}

function togglePlay() {

var a = document.getElementById(selSound);

var b = document.getElementById("playbtn");

play = !play;

if (play) {

b.value = "Stop Playing";

a.play();

}

else {

b.value = "Start Playing";

a.pause();

}

}

16kHz

18kHz

20kHz

Start Playing

Here the first 3 buttons simply set variable selSound to be either '8000', '18000' or '20000', then when you press the last button to play the sound the selsound variable is used to identify the id of the audio element it will use. To begin with the first click will play a sound and the next click will stop it, and it will continue to toggle back and forth.

This isn't complete though and I've not tested it so it might not even work fully. But you will probably want the play/pause button to toggle back to 'Start Playing' once a sound has finished playing. Otherwise you'll click play, the selected sound will play and finally stop, but the button will still show 'Stop Playing', then when you click it it will toggle to say 'Start Playing' and then you'll have to click it again to start a sample playing again. So you will probably want to trap the event when a sound stops and toggle the button back to 'Start Playing'.

HTH

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值