Audio pause not working properly
See original GitHub issueHello everyone,
It’s my first time posting an issue and I am new to Github so please be tolerant. I am using the latest version of three.js. I noticed that pausing an audio source (Positional or not) wasn’t working properly. When playing after pause, there was a gap between the “offset” of the audio when pausing and that of the audio when playing again afterwards. Indeed, as the code is using AudioBufferSourceNodes that can only be played once, it is necessary to create a new source node when playing, but it has to be set to the right offset. After looking at the code, I suggest the following fix : (modifications appear in the Audio constructor (this.offset), and the methods play, pause and stop)
function Audio( listener ) {
Object3D.call( this );
this.type = 'Audio';
this.context = listener.context;
this.gain = this.context.createGain();
this.gain.connect( listener.getInput() );
this.autoplay = false;
this.buffer = null;
this.loop = false;
this.startTime = this.context.currentTime;
this.playbackRate = 1;
this.isPlaying = false;
this.hasPlaybackControl = true;
this.sourceType = 'empty';
this.offset = 0;
this.filters = [];
this.offset = 0;
}
play: function () {
if ( this.isPlaying === true ) {
console.warn( 'THREE.Audio: Audio is already playing.' );
return;
}
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return;
}
var source = this.context.createBufferSource();
source.buffer = this.buffer;
source.loop = this.loop;
source.onended = this.onEnded.bind( this );
this.startTime = this.context.currentTime;
source.playbackRate.setValueAtTime( this.playbackRate, this.startTime );
source.start( this.startTime, this.offset );
this.isPlaying = true;
this.source = source;
return this.connect();
},
pause: function () {
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return;
}
if( this.isPlaying ) {
this.source.stop();
this.offset += this.context.currentTime - this.startTime;
this.isPlaying = false;
}
return this;
},
stop: function () {
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return;
}
this.source.stop();
this.offset = 0;
this.isPlaying = false;
return this;
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:8
Top Results From Across the Web
HTML5 Audio pause not working - Stack Overflow
play(); If I manually enter on the js console the pause code the audio will stop... so no idea whats goin on. console....
Read more >HTML DOM Audio pause() Method - W3Schools
Definition and Usage. The pause() method halts (pauses) the currently playing audio. Tip: This method is often used together with the play() method....
Read more >How to fix play/pause button on keyboard not working?
How to fix play/pause button on keyboard not working? · Fix 1. Run keyboard troubleshooter · Fix 2. Disable Google Play Music (if...
Read more >No sound after resuming from pause in mediaplayer or games ...
Audio doesn't work after pause/ resume in any software like groove music / vlc ... Right click on the driver and select "Update...
Read more >Audio and Video play and pause automatically on Windows ...
Sometimes, sound and playback issues occur due to some defects in hardware, like faulty cables, audio jacks, speakers, etc., whereas sometimes, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you for your answers, I will try to do the pull request on monday. I’ll tell you if I need help, thanks for proposing. Pablo
After some testing i can confirm this bug. Using just
this.context.currentTime
in order to determine the offset is not sufficient.I’ve prepared a demo to test the new code. It sounds better than before, although the difference is a little bit inconvenient to spot.
@pablolh Do you think the result is okay like that?
Sample track, no pause Same track with play/pause pattern | with fix Same track with play/pause pattern | without fix