Skip to content

Commit

Permalink
[Refactor] Windows empty queue bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
canxin121 committed May 13, 2024
1 parent c5f4dc2 commit 5e49309
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
18 changes: 6 additions & 12 deletions lib/types/music.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,26 @@ Future<PlayMusic?> 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) {
Expand Down
34 changes: 14 additions & 20 deletions lib/util/audio_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,7 @@ class AudioHandler extends GetxController {
final RxList<PlayMusic> playMusicList = RxList<PlayMusic>([]);
final Rx<PlayMusic?> playingMusic = Rx<PlayMusic?>(null);
final ConcatenatingAudioSource playSourceList =
ConcatenatingAudioSource(children: [
if (Platform.isWindows)
AudioSource.asset(
"assets/nature.mp3",
tag: const MediaItem(
title: "Empty",
id: 'default',
),
),
]);
ConcatenatingAudioSource(children: []);

Future<void> _init() async {
// 先默认开启所有的循环
Expand All @@ -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");
Expand All @@ -76,10 +69,6 @@ class AudioHandler extends GetxController {

Future<void> addMusicPlay(DisplayMusic music) async {
try {
if (Platform.isWindows && isWindowsFirstPlay) {
isWindowsFirstPlay = false;
await clear();
}
PlayMusic? playMusic;
var index = -1;
if (music.info.defaultQuality != null) {
Expand All @@ -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);

Expand Down Expand Up @@ -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<PlayMusic> newPlayMusics = [];
Expand Down Expand Up @@ -248,7 +243,6 @@ class AudioHandler extends GetxController {
}
if (playSourceList.length > 0) {
await playSourceList.clear();
update();
}
updateRx();
log2List("Afer Clear all musics");
Expand Down
5 changes: 1 addition & 4 deletions rust/src/api/http_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5e49309

Please sign in to comment.