javascript - AbortError: The play() request was interrupted by a call to pause() - Stack Overflow

iam currenlty trying to fix this javascript code but i cant seem to get it right it stills throws out U

iam currenlty trying to fix this javascript code but i cant seem to get it right it stills throws out

Uncaught (in promise) AbortError: The play() request was interrupted by a call to pause()

and i don't understand whats wrong with it so if you guys could help me out i would be truly happy

Here's the code

<html>
<head>
    <script src="nui://game/ui/jquery.js" type="text/javascript"></script>
    <script>
        var audioPlayer = null;

        window.addEventListener('message', function(event) {
            if (event.data.transactionType == "playSound") {

              if (audioPlayer != null) {
              audioPlayer.pause();
              }

              audioPlayer = new Audio("./sounds/" + event.data.transactionFile + ".ogg");
              audioPlayer.volume = event.data.transactionVolume;

              audioPlayer.play();


            }
        });
    </script>
</head>

iam currenlty trying to fix this javascript code but i cant seem to get it right it stills throws out

Uncaught (in promise) AbortError: The play() request was interrupted by a call to pause()

and i don't understand whats wrong with it so if you guys could help me out i would be truly happy

Here's the code

<html>
<head>
    <script src="nui://game/ui/jquery.js" type="text/javascript"></script>
    <script>
        var audioPlayer = null;

        window.addEventListener('message', function(event) {
            if (event.data.transactionType == "playSound") {

              if (audioPlayer != null) {
              audioPlayer.pause();
              }

              audioPlayer = new Audio("./sounds/" + event.data.transactionFile + ".ogg");
              audioPlayer.volume = event.data.transactionVolume;

              audioPlayer.play();


            }
        });
    </script>
</head>

Share Improve this question edited Dec 12, 2018 at 2:13 Jack Bashford 44.1k11 gold badges55 silver badges82 bronze badges asked Dec 12, 2018 at 2:06 FlamethroneFlamethrone 611 gold badge1 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

I had this problem myself when playing with sounds. The below code fixed it for me:

var soundPromise = audioPlayer.play();

//If the promise exists
if (soundPromise != undefined) {

    soundPromise.then(function(_) {
        //Pause and reset the sound
        sound.pause();
        sound.currentTime = 0;

    });

}

Let me know if you have any issues!

This is the auto play issue in the browser: in short, if you do not make some operation (e.g. click, touch) the browser will not allow media to auto start to play.

Can refer to: https://developers.google./web/updates/2017/09/autoplay-policy-changes

To fix, you should put the calling of .play() in a call back function of click a button, e.g.

playBtn.onclick = function(){
  audioPlayer.play();
}

This is not rather an issue, it is actually a rule.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742256381a4410095.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信