Irshad Khan
Irshad Khan

Reputation: 432

How to stop all audio playing dynamicaly in jquery Laravel

I have 3 section of tune enter image description here

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

Answers (1)

KoIIIeY
KoIIIeY

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

Related Questions