diff --git a/lib/data/services/spotify_service.dart b/lib/data/services/spotify_service.dart index 6ccf56389..983868394 100644 --- a/lib/data/services/spotify_service.dart +++ b/lib/data/services/spotify_service.dart @@ -15,8 +15,8 @@ class SpotifService { /// 初始化OpenAI服务 Future initialize() async { if (!isInitialized) { - getAccessToken(); - connectToSpotifyRemote(); + await getAccessToken(); + await connectToSpotifyRemote(); isInitialized = true; printInfo(info: "SpotifService----------- initialize"); } @@ -68,6 +68,28 @@ class SpotifService { } } + //获取当前播放状态 + Future isPaused() async { + try { + var state = await SpotifySdk.getPlayerState(); + if (state != null) { + printInfo(info: "SpotifService----------- isPaused $state"); + return state.isPaused; + } else { + printError(info: "SpotifService----------- isPaused null"); + return true; + } + } on PlatformException catch (e) { + printError( + info: + "SpotifService------------getPlayerState ${e.code}: ${e.message}"); + } on MissingPluginException { + printError( + info: "SpotifService------------getPlayerState not implemented"); + } + return false; + } + //播放 音乐 Future play(String url) async { try { diff --git a/lib/modules/chat/controllers/chat_controller.dart b/lib/modules/chat/controllers/chat_controller.dart index 2cc4856f5..4b30db2af 100644 --- a/lib/modules/chat/controllers/chat_controller.dart +++ b/lib/modules/chat/controllers/chat_controller.dart @@ -461,6 +461,7 @@ class ChatController extends GetxController { try { await _spotifService.initialize(); _spotifService.play(card['url'] as String); + isPlay.value = true; } catch (e) { Logger.error('加载异常: $e'); } @@ -468,8 +469,12 @@ class ChatController extends GetxController { // 暂停播放音乐 void pause() async { - await player.pause(); - isPlay.value = false; + try { + _spotifService.pause(); + isPlay.value = false; + } catch (e) { + Logger.error('加载异常: $e'); + } } // 更新或添加最终响应 diff --git a/lib/modules/chat/views/chat_music_card.dart b/lib/modules/chat/views/chat_music_card.dart index 5519c7143..57ac11c18 100644 --- a/lib/modules/chat/views/chat_music_card.dart +++ b/lib/modules/chat/views/chat_music_card.dart @@ -43,7 +43,7 @@ class ChatMusicCard extends GetView { ), 10.horizontalSpace, Obx( - () => controller.audiourl == data?['url'] && controller.isPlay.value + () => controller.isPlay.value ? GestureDetector( onTap: () { controller.pause();