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
javascript ios audio three.js
add a comment |
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
javascript ios audio three.js
add a comment |
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
javascript ios audio three.js
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
javascript ios audio three.js
edited Nov 4 at 12:08
Mugen87
2,1952517
2,1952517
asked Nov 4 at 9:49
JoakimNyland
154
154
add a comment |
add a comment |
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...
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
add a comment |
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...
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
add a comment |
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...
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
add a comment |
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...
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...
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password