* 根据索引加载指定单元 * @param {number} unitIndex - 单元索引 * @param {Object} [options={}] - 选项 * @returns {Promise }
(unitIndex, options = {})
| 292 | * @returns {Promise<void>} |
| 293 | */ |
| 294 | async loadUnitByIndex(unitIndex, options = {}) { |
| 295 | const { shouldScrollUnitIntoView = false } = options; |
| 296 | |
| 297 | this.state.currentUnitIndex = unitIndex; |
| 298 | saveCurrentUnitIndex(this.state.bookPath, unitIndex); |
| 299 | |
| 300 | const unit = this.state.units[unitIndex]; |
| 301 | if (!unit) return; |
| 302 | |
| 303 | this.resetPlayer(); |
| 304 | this.updateActiveUnit(unitIndex, { shouldScrollUnitIntoView }); |
| 305 | this.updateNavigationButtons(); |
| 306 | |
| 307 | // 加载歌词 |
| 308 | try { |
| 309 | let lrcText = this.lrcCache.get(unit.lrc); |
| 310 | if (!lrcText) { |
| 311 | const response = await fetch(unit.lrc); |
| 312 | lrcText = await response.text(); |
| 313 | this.lrcCache.set(unit.lrc, lrcText); |
| 314 | } |
| 315 | |
| 316 | this.state.currentLyrics = LRCParser.parse( |
| 317 | lrcText, |
| 318 | this.config.PLAYER.TIME_OFFSET |
| 319 | ); |
| 320 | this.renderLyrics(); |
| 321 | } catch (error) { |
| 322 | console.error(this.config.ERROR_MESSAGES.LOAD_LYRIC, error); |
| 323 | if (this.dom.lyricsDisplay) { |
| 324 | setHTML(this.dom.lyricsDisplay, `<p class="placeholder">${this.config.ERROR_MESSAGES.LOAD_FAILED}</p>`); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // 设置音频源 |
| 329 | if (this.dom.audioPlayer) { |
| 330 | this.setPlayButtonDisabled(true); |
| 331 | this.dom.audioPlayer.src = unit.audio; |
| 332 | this.dom.audioPlayer.loop = this.state.loopMode === 'list'; |
| 333 | this.dom.audioPlayer.load(); |
| 334 | } |
| 335 | |
| 336 | // 恢复播放进度和速度 |
| 337 | this.loadPlayTime(); |
| 338 | this.loadSavedSpeed(); |
| 339 | |
| 340 | // 预加载下一个单元 |
| 341 | this.prefetchUnit(unitIndex + 1); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * 重置播放器 |
no test coverage detected