Skip to content

Commit d368b11

Browse files
committed
new player option "volumeTransitionTime"
1 parent 13db624 commit d368b11

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/library/audio.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface IAudioOptions {
99
persistVolume: boolean;
1010
loadPlayerMode: string;
1111
addAudioElementsToDom: boolean;
12+
volumeTransitionTime: number;
1213
}
1314

1415
// https://developer.mozilla.org/en-US/docs/Web/API/AudioNode
@@ -534,7 +535,9 @@ export class PlayerAudio {
534535

535536
if (this._audioNodes.gainNode instanceof GainNode) {
536537
const audioContext = await this.getAudioContext();
537-
this._audioNodes.gainNode.gain.setTargetAtTime(gainValue, audioContext.currentTime, 0.1);
538+
const timeConstantInMilliseconds = (!isNaN(this._options.volumeTransitionTime) && this._options.volumeTransitionTime > 0) ? this._options.volumeTransitionTime : 100
539+
const timeConstantInSeconds = timeConstantInMilliseconds / 1000;
540+
this._audioNodes.gainNode.gain.setTargetAtTime(gainValue, audioContext.currentTime, timeConstantInSeconds);
538541
}
539542

540543
}

src/library/core.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ICoreOptions {
2929
loadPlayerMode?: typePlayerMode;
3030
audioContext?: AudioContext;
3131
addAudioElementsToDom?: boolean;
32+
volumeTransitionTime?: number;
3233
}
3334

3435
export interface ISoundsQueueOptions {
@@ -112,6 +113,7 @@ export class PlayerCore {
112113
loadPlayerMode: PLAYER_MODE_AUDIO,
113114
audioContext: null,
114115
addAudioElementsToDom: false,
116+
volumeTransitionTime: 100,
115117
};
116118

117119
const options = Object.assign({}, defaultOptions, playerOptions);
@@ -158,6 +160,7 @@ export class PlayerCore {
158160
persistVolume: this._options.persistVolume,
159161
loadPlayerMode: this._options.loadPlayerMode,
160162
addAudioElementsToDom: this._options.addAudioElementsToDom,
163+
volumeTransitionTime: this._options.volumeTransitionTime,
161164
};
162165

163166
return audioOptions;
@@ -313,7 +316,9 @@ export class PlayerCore {
313316

314317
if (currentSound !== null) {
315318

316-
if (!isNaN(currentSound.duration) && (soundPositionInSeconds > currentSound.duration)) {
319+
// round duration up as numbers are not integers
320+
// so sometimes it is a tiny bit above
321+
if (!isNaN(currentSound.duration) && (soundPositionInSeconds > Math.ceil(currentSound.duration))) {
317322
console.warn('soundPositionInSeconds > sound duration')
318323
}
319324

@@ -646,7 +651,9 @@ export class PlayerCore {
646651
sound.sourceNode.start(0, sound.playTime);
647652
} else {
648653
if (sound.playTimeOffset > 0) {
649-
if (sound.playTimeOffset > sound.duration) {
654+
// round duration up as numbers are not integers
655+
// so sometimes it is a tiny bit above
656+
if (sound.playTimeOffset > Math.ceil(sound.duration)) {
650657
console.warn('playTimeOffset > sound duration');
651658
}
652659
// if an offset is defined start playing at that position
@@ -675,7 +682,9 @@ export class PlayerCore {
675682
} else {
676683
// if an offset is defined start playing at that position
677684
if (sound.playTimeOffset > 0) {
678-
if (sound.playTimeOffset > sound.duration) {
685+
// round duration up as numbers are not integers
686+
// so sometimes it is a tiny bit above
687+
if (sound.playTimeOffset > Math.ceil(sound.duration)) {
679688
console.warn('playTimeOffset > duration');
680689
}
681690
sound.audioElement.currentTime = sound.playTimeOffset;
@@ -1237,4 +1246,10 @@ export class PlayerCore {
12371246

12381247
}
12391248

1249+
public getCurrentSound(): ISound {
1250+
1251+
return this._getSoundFromQueue({ whichSound: PlayerCore.CURRENT_SOUND });
1252+
1253+
}
1254+
12401255
}

0 commit comments

Comments
 (0)