Audio.play() causes tiny lag











up vote
2
down vote

favorite












Problem:
I have made a flappy-bird like game using Three.js. Everytime I tap the screen, a "flap"-sound is played. However playing this audio causes a tiny lagg, which makes the game less smooth. If I remove the sound, all lagg disappears. The lagg only appears as the audio is played, not while the audio is playing.



Audio set-up:
First I set up the audio like this:



var soundFly = new Audio();
soundFly.src = "https://dl.dropbox.com/s/tj7mxg26egzo4zx/flap.wav?dl=0";
soundFly.preload = "auto";


Next I load all audio upon starting the game (on button click):



function loadAudio()
{
soundFly.play().then(function () {
soundFly.pause()
}).catch(function (e) {
console.log("soundFly - " + e)
});
};


Then, everytime I touch the screen, I play the sound:



this.soundFly.play().catch(function (e)     {
console.log("soundFly - " + e)
})


This works great, but the tiny lagg appears.



Rendering:
This is how I set up the rendering of the scene:



var clock = new THREE.Clock();
var delta=0;
clock.start(); //makes rendering timedependent

var render = function ()
{

delta = clock.getDelta();

//All movement is set using speed*delta

requestAnimationFrame(render);
renderer.render(scene, camera);
};


FPS:
I have also checked how the audio.play() affects the FPS. My FPS is steady at about 59. But when I play the audio, the FPS drops to between 20-30, which probably is what causes the tiny lagg (see picture):



Audio.play() affecting FPS



I checked FPS using this:



console.log(1/delta);


And also outputting "flapp" to the console when touching screen.



I hope someone has an explanation to this!



Best regards,
Joakim










share|improve this question




























    up vote
    2
    down vote

    favorite












    Problem:
    I have made a flappy-bird like game using Three.js. Everytime I tap the screen, a "flap"-sound is played. However playing this audio causes a tiny lagg, which makes the game less smooth. If I remove the sound, all lagg disappears. The lagg only appears as the audio is played, not while the audio is playing.



    Audio set-up:
    First I set up the audio like this:



    var soundFly = new Audio();
    soundFly.src = "https://dl.dropbox.com/s/tj7mxg26egzo4zx/flap.wav?dl=0";
    soundFly.preload = "auto";


    Next I load all audio upon starting the game (on button click):



    function loadAudio()
    {
    soundFly.play().then(function () {
    soundFly.pause()
    }).catch(function (e) {
    console.log("soundFly - " + e)
    });
    };


    Then, everytime I touch the screen, I play the sound:



    this.soundFly.play().catch(function (e)     {
    console.log("soundFly - " + e)
    })


    This works great, but the tiny lagg appears.



    Rendering:
    This is how I set up the rendering of the scene:



    var clock = new THREE.Clock();
    var delta=0;
    clock.start(); //makes rendering timedependent

    var render = function ()
    {

    delta = clock.getDelta();

    //All movement is set using speed*delta

    requestAnimationFrame(render);
    renderer.render(scene, camera);
    };


    FPS:
    I have also checked how the audio.play() affects the FPS. My FPS is steady at about 59. But when I play the audio, the FPS drops to between 20-30, which probably is what causes the tiny lagg (see picture):



    Audio.play() affecting FPS



    I checked FPS using this:



    console.log(1/delta);


    And also outputting "flapp" to the console when touching screen.



    I hope someone has an explanation to this!



    Best regards,
    Joakim










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Problem:
      I have made a flappy-bird like game using Three.js. Everytime I tap the screen, a "flap"-sound is played. However playing this audio causes a tiny lagg, which makes the game less smooth. If I remove the sound, all lagg disappears. The lagg only appears as the audio is played, not while the audio is playing.



      Audio set-up:
      First I set up the audio like this:



      var soundFly = new Audio();
      soundFly.src = "https://dl.dropbox.com/s/tj7mxg26egzo4zx/flap.wav?dl=0";
      soundFly.preload = "auto";


      Next I load all audio upon starting the game (on button click):



      function loadAudio()
      {
      soundFly.play().then(function () {
      soundFly.pause()
      }).catch(function (e) {
      console.log("soundFly - " + e)
      });
      };


      Then, everytime I touch the screen, I play the sound:



      this.soundFly.play().catch(function (e)     {
      console.log("soundFly - " + e)
      })


      This works great, but the tiny lagg appears.



      Rendering:
      This is how I set up the rendering of the scene:



      var clock = new THREE.Clock();
      var delta=0;
      clock.start(); //makes rendering timedependent

      var render = function ()
      {

      delta = clock.getDelta();

      //All movement is set using speed*delta

      requestAnimationFrame(render);
      renderer.render(scene, camera);
      };


      FPS:
      I have also checked how the audio.play() affects the FPS. My FPS is steady at about 59. But when I play the audio, the FPS drops to between 20-30, which probably is what causes the tiny lagg (see picture):



      Audio.play() affecting FPS



      I checked FPS using this:



      console.log(1/delta);


      And also outputting "flapp" to the console when touching screen.



      I hope someone has an explanation to this!



      Best regards,
      Joakim










      share|improve this question















      Problem:
      I have made a flappy-bird like game using Three.js. Everytime I tap the screen, a "flap"-sound is played. However playing this audio causes a tiny lagg, which makes the game less smooth. If I remove the sound, all lagg disappears. The lagg only appears as the audio is played, not while the audio is playing.



      Audio set-up:
      First I set up the audio like this:



      var soundFly = new Audio();
      soundFly.src = "https://dl.dropbox.com/s/tj7mxg26egzo4zx/flap.wav?dl=0";
      soundFly.preload = "auto";


      Next I load all audio upon starting the game (on button click):



      function loadAudio()
      {
      soundFly.play().then(function () {
      soundFly.pause()
      }).catch(function (e) {
      console.log("soundFly - " + e)
      });
      };


      Then, everytime I touch the screen, I play the sound:



      this.soundFly.play().catch(function (e)     {
      console.log("soundFly - " + e)
      })


      This works great, but the tiny lagg appears.



      Rendering:
      This is how I set up the rendering of the scene:



      var clock = new THREE.Clock();
      var delta=0;
      clock.start(); //makes rendering timedependent

      var render = function ()
      {

      delta = clock.getDelta();

      //All movement is set using speed*delta

      requestAnimationFrame(render);
      renderer.render(scene, camera);
      };


      FPS:
      I have also checked how the audio.play() affects the FPS. My FPS is steady at about 59. But when I play the audio, the FPS drops to between 20-30, which probably is what causes the tiny lagg (see picture):



      Audio.play() affecting FPS



      I checked FPS using this:



      console.log(1/delta);


      And also outputting "flapp" to the console when touching screen.



      I hope someone has an explanation to this!



      Best regards,
      Joakim







      javascript ios audio three.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 4 at 12:08









      Mugen87

      2,1952517




      2,1952517










      asked Nov 4 at 9:49









      JoakimNyland

      154




      154
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The problem is that you use HTML5 audio for interactive sound effects. The API is not intended for this purpose. Instead, use the Web Audio based classes like THREE.Audio or THREE.PositionalAudio which allow sound effects without delay and appropriate timing.



          https://threejs.org/examples/#webaudio_timing



          Also read the following guide for more information. It says:



          Timing is controlled with high precision and low latency, allowing developers to write code that responds accurately to events...






          share|improve this answer





















          • Thank you very much, I'm gonna try this!
            – JoakimNyland
            Nov 4 at 14:28






          • 1




            Update: I now implemented Three.Audio, and ALL lagg is removed! Awesome! Thanks a lot @Mugen87!
            – JoakimNyland
            Nov 4 at 15:48











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139521%2faudio-play-causes-tiny-lag%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          The problem is that you use HTML5 audio for interactive sound effects. The API is not intended for this purpose. Instead, use the Web Audio based classes like THREE.Audio or THREE.PositionalAudio which allow sound effects without delay and appropriate timing.



          https://threejs.org/examples/#webaudio_timing



          Also read the following guide for more information. It says:



          Timing is controlled with high precision and low latency, allowing developers to write code that responds accurately to events...






          share|improve this answer





















          • Thank you very much, I'm gonna try this!
            – JoakimNyland
            Nov 4 at 14:28






          • 1




            Update: I now implemented Three.Audio, and ALL lagg is removed! Awesome! Thanks a lot @Mugen87!
            – JoakimNyland
            Nov 4 at 15:48















          up vote
          1
          down vote



          accepted










          The problem is that you use HTML5 audio for interactive sound effects. The API is not intended for this purpose. Instead, use the Web Audio based classes like THREE.Audio or THREE.PositionalAudio which allow sound effects without delay and appropriate timing.



          https://threejs.org/examples/#webaudio_timing



          Also read the following guide for more information. It says:



          Timing is controlled with high precision and low latency, allowing developers to write code that responds accurately to events...






          share|improve this answer





















          • Thank you very much, I'm gonna try this!
            – JoakimNyland
            Nov 4 at 14:28






          • 1




            Update: I now implemented Three.Audio, and ALL lagg is removed! Awesome! Thanks a lot @Mugen87!
            – JoakimNyland
            Nov 4 at 15:48













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          The problem is that you use HTML5 audio for interactive sound effects. The API is not intended for this purpose. Instead, use the Web Audio based classes like THREE.Audio or THREE.PositionalAudio which allow sound effects without delay and appropriate timing.



          https://threejs.org/examples/#webaudio_timing



          Also read the following guide for more information. It says:



          Timing is controlled with high precision and low latency, allowing developers to write code that responds accurately to events...






          share|improve this answer












          The problem is that you use HTML5 audio for interactive sound effects. The API is not intended for this purpose. Instead, use the Web Audio based classes like THREE.Audio or THREE.PositionalAudio which allow sound effects without delay and appropriate timing.



          https://threejs.org/examples/#webaudio_timing



          Also read the following guide for more information. It says:



          Timing is controlled with high precision and low latency, allowing developers to write code that responds accurately to events...







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 4 at 12:04









          Mugen87

          2,1952517




          2,1952517












          • Thank you very much, I'm gonna try this!
            – JoakimNyland
            Nov 4 at 14:28






          • 1




            Update: I now implemented Three.Audio, and ALL lagg is removed! Awesome! Thanks a lot @Mugen87!
            – JoakimNyland
            Nov 4 at 15:48


















          • Thank you very much, I'm gonna try this!
            – JoakimNyland
            Nov 4 at 14:28






          • 1




            Update: I now implemented Three.Audio, and ALL lagg is removed! Awesome! Thanks a lot @Mugen87!
            – JoakimNyland
            Nov 4 at 15:48
















          Thank you very much, I'm gonna try this!
          – JoakimNyland
          Nov 4 at 14:28




          Thank you very much, I'm gonna try this!
          – JoakimNyland
          Nov 4 at 14:28




          1




          1




          Update: I now implemented Three.Audio, and ALL lagg is removed! Awesome! Thanks a lot @Mugen87!
          – JoakimNyland
          Nov 4 at 15:48




          Update: I now implemented Three.Audio, and ALL lagg is removed! Awesome! Thanks a lot @Mugen87!
          – JoakimNyland
          Nov 4 at 15:48


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139521%2faudio-play-causes-tiny-lag%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini