Skip to content

Commit d5b0169

Browse files
committed
verify that volume set in options is a number
1 parent 740e158 commit d5b0169

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/library/audio.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,17 @@ export class PlayerAudio {
534534
protected async _changePlayerGainValue(gainValue: number): Promise<void> {
535535

536536
if (this._audioNodes.gainNode instanceof GainNode) {
537+
537538
const audioContext = await this.getAudioContext();
538539
const timeConstantInMilliseconds = (!isNaN(this._options.volumeTransitionTime) && this._options.volumeTransitionTime > 0) ? this._options.volumeTransitionTime : 100
539540
const timeConstantInSeconds = timeConstantInMilliseconds / 1000;
541+
540542
try {
541543
this._audioNodes.gainNode.gain.setTargetAtTime(gainValue, audioContext.currentTime, timeConstantInSeconds);
542544
} catch (error) {
543545
console.error('gainValue: ' + gainValue + ' ' + error)
544546
}
545-
547+
546548
}
547549

548550
}
@@ -590,27 +592,34 @@ export class PlayerAudio {
590592
let volume: number;
591593

592594
// check if volume has already been set
593-
if (this._volume !== null && !isNaN(this._volume)) {
595+
if (this._volume !== null) {
596+
594597
volume = this._volume;
595-
} else {
596-
if (this._options.persistVolume) {
597-
// if persist volume is enabled
598-
// check if there already is a user volume in localstorage
599-
const userVolumeInPercent = parseInt(localStorage.getItem('WebAudioAPIPlayerVolume'));
600-
601-
if (!isNaN(userVolumeInPercent)) {
602-
volume = userVolumeInPercent;
603-
}
604-
}
605598

606-
// if volume is not persisted
607-
// or the persited value has not been set yet
608-
if (typeof volume === 'undefined') {
599+
} else if (this._options.persistVolume) {
600+
601+
// if persist volume is enabled
602+
// check if there already is a user volume in localstorage
603+
const userVolumeInPercent = parseInt(localStorage.getItem('WebAudioAPIPlayerVolume'));
604+
605+
volume = userVolumeInPercent;
606+
607+
}
608+
609+
// if still no value, fallback to default options value
610+
if (typeof volume === 'undefined' || isNaN(volume)) {
611+
612+
if (!isNaN(this._options.volume)) {
609613
volume = this._options.volume;
614+
} else {
615+
volume = 80;
616+
console.error('player options volume is not a number')
610617
}
611-
this._volume = volume;
618+
612619
}
613620

621+
this._volume = volume;
622+
614623
return volume;
615624

616625
}

0 commit comments

Comments
 (0)