Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sound Play / Stop / Pause

I'm developing a sound JavaScript library. I can play sound with below code.

var soundPlayer = null;

function playSound(){

soundPlayer = new Audio(soundName).play();

}

How can I stop and pause this audio? When I try like this:

soundPlayer.pause();

Or

soundPlayer.stop();

But I get this error:

Uncaught TypeError: soundPlayer.stop is not a function

How can I do that?

like image 977
jsawyer Avatar asked Apr 02 '17 11:04

jsawyer


1 Answers

If you change that:

soundPlayer = new Audio(soundName).play();

To that:

soundPlayer = new Audio(soundName);
soundPlayer.play();

Your pause will be working. The problem is that you assigned "play" function to soundPlayer. SoundPlayer isnt an Audio object now.

Instead of stop() use:

soundPlayer.pause();
soundPlayer.currentTime = 0;

It works the same I guess.

like image 51
5ka Avatar answered Nov 15 '22 12:11

5ka