diff --git a/package.json b/package.json index b99eb95..4df87f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "audio_x", - "version": "1.0.10-beta.4", + "version": "1.0.10-beta.7", "description": "The audio player for the gen-x", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/audio.ts b/src/audio.ts index 725ff35..4c7cf96 100644 --- a/src/audio.ts +++ b/src/audio.ts @@ -118,7 +118,10 @@ class AudioX { } if (enableEQ) { - this.isEqEnabled = enableEQ; + this.attachEq(); + if (this.eqInstance) { + this.isEqEnabled = enableEQ; + } } if (enableHls) { @@ -132,6 +135,14 @@ class AudioX { return; } + const queue = this.getQueue(); + if (isValidArray(queue)) { + const index = queue.findIndex((track) => mediaTrack.id === track.id); + if (index > -1) { + this._currentQueueIndex = index; + } + } + const mediaType = mediaTrack.source.includes('.m3u8') ? 'HLS' : 'DEFAULT'; if (this.isPlayLogEnabled) { @@ -165,7 +176,7 @@ class AudioX { } attachEq() { - if (this.isEqEnabled && this.eqStatus === 'IDEAL') { + if (this.eqStatus === 'IDEAL') { try { const eq = new Equalizer(); this.eqStatus = eq.status(); @@ -218,7 +229,6 @@ class AudioX { this.addMedia(currentTrack).then(() => { if (audioInstance.HAVE_ENOUGH_DATA === READY_STATE.HAVE_ENOUGH_DATA) { setTimeout(async () => { - this.attachEq(); await this.play(); }, 950); } @@ -323,15 +333,27 @@ class AudioX { } setPreset(id: keyof Preset) { - this.eqInstance.setPreset(id); + if (this.isEqEnabled) { + this.eqInstance.setPreset(id); + } else { + console.error('Equalizer not initialized, please set enableEq at init'); + } } setCustomEQ(gains: number[]) { - this.eqInstance.setCustomEQ(gains); + if (this.isEqEnabled) { + this.eqInstance.setCustomEQ(gains); + } else { + console.error('Equalizer not initialized, please set enableEq at init'); + } } setBassBoost(enabled: boolean, boost: number) { - this.eqInstance.setBassBoost(enabled, boost); + if (this.isEqEnabled) { + this.eqInstance.setBassBoost(enabled, boost); + } else { + console.error('Equalizer not initialized, please set enableEq at init'); + } } addQueue(queue: MediaTrack[], playbackType: QueuePlaybackType) { @@ -360,20 +382,23 @@ class AudioX { } playNext() { - if (this._queue.length > this._currentQueueIndex + 1) { - this._currentQueueIndex++; - const nextTrack = this._queue[this._currentQueueIndex]; + const index = this._currentQueueIndex + 1; + if (this._queue.length > index) { + const nextTrack = this._queue[index]; this.addMediaAndPlay(nextTrack, this._fetchFn); + this._currentQueueIndex = index; } else { console.warn('Queue ended'); } } playPrevious() { - if (this._currentQueueIndex > 0) { - this._currentQueueIndex--; - const previousTrack = this._queue[this._currentQueueIndex]; + const index = this._currentQueueIndex - 1; + + if (index > 0) { + const previousTrack = this._queue[index]; this.addMediaAndPlay(previousTrack, this._fetchFn); + this._currentQueueIndex = index; } else { console.log('At the beginning of the queue'); }