Browse Source

上传音频卡片播放

newdev_shunjiawei
liwei1dao 2 years ago
parent
commit
aa0054d62f
  1. 26
      lib/data/services/spotify_service.dart
  2. 9
      lib/modules/chat/controllers/chat_controller.dart
  3. 2
      lib/modules/chat/views/chat_music_card.dart

26
lib/data/services/spotify_service.dart

@ -15,8 +15,8 @@ class SpotifService {
/// 初始化OpenAI服务
Future<bool> initialize() async {
if (!isInitialized) {
getAccessToken();
connectToSpotifyRemote();
await getAccessToken();
await connectToSpotifyRemote();
isInitialized = true;
printInfo(info: "SpotifService----------- initialize");
}
@ -68,6 +68,28 @@ class SpotifService {
}
}
//获取当前播放状态
Future<bool> 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<void> play(String url) async {
try {

9
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');
}
}
// 更新或添加最终响应

2
lib/modules/chat/views/chat_music_card.dart

@ -43,7 +43,7 @@ class ChatMusicCard extends GetView<ChatController> {
),
10.horizontalSpace,
Obx(
() => controller.audiourl == data?['url'] && controller.isPlay.value
() => controller.isPlay.value
? GestureDetector(
onTap: () {
controller.pause();

Loading…
Cancel
Save