Skip to content

Commit 5e49309

Browse files
committed
[Refactor] Windows empty queue bug fix.
1 parent c5f4dc2 commit 5e49309

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

lib/types/music.dart

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,26 @@ Future<PlayMusic?> display2PlayMusic(DisplayMusic music,
3333
return null;
3434
}
3535
var (cacheFileName, extra) = result;
36+
3637
// 尝试获取本地缓存
3738
var cache = await useCacheFile(
3839
file: "", cachePath: musicCachePath, filename: cacheFileName);
40+
3941
// 有本地缓存直接返回
4042
if (cache != null) {
4143
talker.info("[Display2PlayMusic] 使用本地歌曲缓存转化歌曲: ${music.info.name}");
4244
return PlayMusic(music.ref, music.info, PlayInfo(cache, finalQuality),
4345
music.ref.getExtraInto(quality: finalQuality));
4446
}
47+
4548
// 没有本地缓存,也没有第三方api,直接返回null
4649
if (globalExternApi == null) {
4750
talker.error("[Display2PlayMusic] 无第三方音乐源,无法获取播放信息");
4851
}
4952

50-
PlayInfo? playinfo;
51-
for (int attempt = 0; attempt < 3; attempt++) {
52-
try {
53-
playinfo =
54-
await globalExternApi!.getMusicPlayInfo(music.info.source, extra);
55-
if (playinfo != null) break;
56-
} catch (e) {
57-
if (attempt == 2) {
58-
playinfo = null;
59-
}
60-
}
61-
}
53+
// 有第三方api,使用api进行请求
54+
var playinfo =
55+
await globalExternApi!.getMusicPlayInfo(music.info.source, extra);
6256

6357
// 如果第三方api查找不到,直接返回null
6458
if (playinfo == null) {

lib/util/audio_controller.dart

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,7 @@ class AudioHandler extends GetxController {
3939
final RxList<PlayMusic> playMusicList = RxList<PlayMusic>([]);
4040
final Rx<PlayMusic?> playingMusic = Rx<PlayMusic?>(null);
4141
final ConcatenatingAudioSource playSourceList =
42-
ConcatenatingAudioSource(children: [
43-
if (Platform.isWindows)
44-
AudioSource.asset(
45-
"assets/nature.mp3",
46-
tag: const MediaItem(
47-
title: "Empty",
48-
id: 'default',
49-
),
50-
),
51-
]);
42+
ConcatenatingAudioSource(children: []);
5243

5344
Future<void> _init() async {
5445
// 先默认开启所有的循环
@@ -62,7 +53,9 @@ class AudioHandler extends GetxController {
6253
talker.error('[PlaybackEventStream Error] $e');
6354
});
6455
// 将playSourceList交给player作为列表,不过目前是空的
65-
_player.setAudioSource(playSourceList);
56+
if (!Platform.isWindows) {
57+
_player.setAudioSource(playSourceList);
58+
}
6659

6760
_player.currentIndexStream.listen((event) {
6861
talker.info("[Music Handler] currentIndexStream updated");
@@ -76,10 +69,6 @@ class AudioHandler extends GetxController {
7669

7770
Future<void> addMusicPlay(DisplayMusic music) async {
7871
try {
79-
if (Platform.isWindows && isWindowsFirstPlay) {
80-
isWindowsFirstPlay = false;
81-
await clear();
82-
}
8372
PlayMusic? playMusic;
8473
var index = -1;
8574
if (music.info.defaultQuality != null) {
@@ -105,6 +94,11 @@ class AudioHandler extends GetxController {
10594
updateRx(music: playMusic);
10695
await playSourceList.add(playMusic.toAudioSource());
10796

97+
if (Platform.isWindows && isWindowsFirstPlay) {
98+
await _player.setAudioSource(playSourceList);
99+
isWindowsFirstPlay = false;
100+
}
101+
108102
// 播放新的音乐
109103
await seek(Duration.zero, index: playSourceList.length - 1);
110104

@@ -186,16 +180,17 @@ class AudioHandler extends GetxController {
186180
}
187181
await clear();
188182

189-
if (Platform.isWindows) {
190-
isWindowsFirstPlay = false;
191-
}
192-
193183
var firstMusic = await display2PlayMusic(musics[0]);
194184
if (firstMusic == null) return;
195185
playMusicList.add(firstMusic);
196186
await playSourceList.add(firstMusic.toAudioSource());
197187
updateRx(music: firstMusic);
198188

189+
if (Platform.isWindows && isWindowsFirstPlay) {
190+
await _player.setAudioSource(playSourceList);
191+
isWindowsFirstPlay = false;
192+
}
193+
199194
await play();
200195

201196
List<PlayMusic> newPlayMusics = [];
@@ -248,7 +243,6 @@ class AudioHandler extends GetxController {
248243
}
249244
if (playSourceList.length > 0) {
250245
await playSourceList.clear();
251-
update();
252246
}
253247
updateRx();
254248
log2List("Afer Clear all musics");

rust/src/api/http_helper.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ pub async fn send_request(
1010
payload: &str,
1111
) -> String {
1212
let method = method.to_uppercase();
13-
let headers = match (&headers).try_into() {
14-
Ok(h) => h,
15-
Err(_) => return "".to_string(),
16-
};
13+
let headers: HeaderMap = (&headers).try_into().unwrap_or_default();
1714

1815
let response = match method.as_str() {
1916
"GET" => send(method, url, headers, None).await,

0 commit comments

Comments
 (0)