From 5e4930910004477cb12aec006737c20dcdc296ec Mon Sep 17 00:00:00 2001 From: canxin Date: Mon, 13 May 2024 17:43:00 +0800 Subject: [PATCH] [Refactor] Windows empty queue bug fix. --- lib/types/music.dart | 18 ++++++------------ lib/util/audio_controller.dart | 34 ++++++++++++++-------------------- rust/src/api/http_helper.rs | 5 +---- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/lib/types/music.dart b/lib/types/music.dart index 6994d7a..61acd14 100644 --- a/lib/types/music.dart +++ b/lib/types/music.dart @@ -33,32 +33,26 @@ Future display2PlayMusic(DisplayMusic music, return null; } var (cacheFileName, extra) = result; + // 尝试获取本地缓存 var cache = await useCacheFile( file: "", cachePath: musicCachePath, filename: cacheFileName); + // 有本地缓存直接返回 if (cache != null) { talker.info("[Display2PlayMusic] 使用本地歌曲缓存转化歌曲: ${music.info.name}"); return PlayMusic(music.ref, music.info, PlayInfo(cache, finalQuality), music.ref.getExtraInto(quality: finalQuality)); } + // 没有本地缓存,也没有第三方api,直接返回null if (globalExternApi == null) { talker.error("[Display2PlayMusic] 无第三方音乐源,无法获取播放信息"); } - PlayInfo? playinfo; - for (int attempt = 0; attempt < 3; attempt++) { - try { - playinfo = - await globalExternApi!.getMusicPlayInfo(music.info.source, extra); - if (playinfo != null) break; - } catch (e) { - if (attempt == 2) { - playinfo = null; - } - } - } + // 有第三方api,使用api进行请求 + var playinfo = + await globalExternApi!.getMusicPlayInfo(music.info.source, extra); // 如果第三方api查找不到,直接返回null if (playinfo == null) { diff --git a/lib/util/audio_controller.dart b/lib/util/audio_controller.dart index 3c0ea01..f9792df 100644 --- a/lib/util/audio_controller.dart +++ b/lib/util/audio_controller.dart @@ -39,16 +39,7 @@ class AudioHandler extends GetxController { final RxList playMusicList = RxList([]); final Rx playingMusic = Rx(null); final ConcatenatingAudioSource playSourceList = - ConcatenatingAudioSource(children: [ - if (Platform.isWindows) - AudioSource.asset( - "assets/nature.mp3", - tag: const MediaItem( - title: "Empty", - id: 'default', - ), - ), - ]); + ConcatenatingAudioSource(children: []); Future _init() async { // 先默认开启所有的循环 @@ -62,7 +53,9 @@ class AudioHandler extends GetxController { talker.error('[PlaybackEventStream Error] $e'); }); // 将playSourceList交给player作为列表,不过目前是空的 - _player.setAudioSource(playSourceList); + if (!Platform.isWindows) { + _player.setAudioSource(playSourceList); + } _player.currentIndexStream.listen((event) { talker.info("[Music Handler] currentIndexStream updated"); @@ -76,10 +69,6 @@ class AudioHandler extends GetxController { Future addMusicPlay(DisplayMusic music) async { try { - if (Platform.isWindows && isWindowsFirstPlay) { - isWindowsFirstPlay = false; - await clear(); - } PlayMusic? playMusic; var index = -1; if (music.info.defaultQuality != null) { @@ -105,6 +94,11 @@ class AudioHandler extends GetxController { updateRx(music: playMusic); await playSourceList.add(playMusic.toAudioSource()); + if (Platform.isWindows && isWindowsFirstPlay) { + await _player.setAudioSource(playSourceList); + isWindowsFirstPlay = false; + } + // 播放新的音乐 await seek(Duration.zero, index: playSourceList.length - 1); @@ -186,16 +180,17 @@ class AudioHandler extends GetxController { } await clear(); - if (Platform.isWindows) { - isWindowsFirstPlay = false; - } - var firstMusic = await display2PlayMusic(musics[0]); if (firstMusic == null) return; playMusicList.add(firstMusic); await playSourceList.add(firstMusic.toAudioSource()); updateRx(music: firstMusic); + if (Platform.isWindows && isWindowsFirstPlay) { + await _player.setAudioSource(playSourceList); + isWindowsFirstPlay = false; + } + await play(); List newPlayMusics = []; @@ -248,7 +243,6 @@ class AudioHandler extends GetxController { } if (playSourceList.length > 0) { await playSourceList.clear(); - update(); } updateRx(); log2List("Afer Clear all musics"); diff --git a/rust/src/api/http_helper.rs b/rust/src/api/http_helper.rs index a08aa4d..a58dc33 100644 --- a/rust/src/api/http_helper.rs +++ b/rust/src/api/http_helper.rs @@ -10,10 +10,7 @@ pub async fn send_request( payload: &str, ) -> String { let method = method.to_uppercase(); - let headers = match (&headers).try_into() { - Ok(h) => h, - Err(_) => return "".to_string(), - }; + let headers: HeaderMap = (&headers).try_into().unwrap_or_default(); let response = match method.as_str() { "GET" => send(method, url, headers, None).await,