Reputation: 432
and i have to code play tune using below code
```
$('.play_tune_focous').click(function() {
let tune_name = $(this).data('bell');
if(tune_name !== 'None'){
$('audio').each(function(){
this.pause(); // Stop playing
this.currentTime = 0; // Reset time
});
var baseUrl = "{{ asset('tunes/FocusAlarm/') }}";
const audio = new Audio(`${baseUrl +'/'+ tune_name+'.mp3'}`);
audio.play();
}
```
I am facing issue if i play new tune then privious also running. How can i solve this issue
Upvotes: 1
Views: 158
Reputation: 558
you create new Audio every click.
let audio = null;
$('.play_tune_focous').click(function() {
let tune_name = $(this).data('bell');
if(tune_name !== 'None'){
$('audio').each(function(){
this.pause(); // Stop playing
this.currentTime = 0; // Reset time
});
if(audio){audio.stop();}
var baseUrl = "{{ asset('tunes/FocusAlarm/') }}";
audio = new Audio(`${baseUrl +'/'+ tune_name+'.mp3'}`);
audio.play();
}
Upvotes: 1