import 'dart:async'; import 'dart:io'; import 'dart:convert'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:get_storage/get_storage.dart'; import 'package:spotify_sdk/spotify_sdk.dart'; import 'package:get/get.dart'; import 'ai_service.dart'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; /// OpenAI服务适配器 - 连接AiService接口与OpenAIService插件 class SpotifService { bool isInitialized = false; final GetStorage _storage = GetStorage(); late String SPOTIFY_CLIENT_ID; late String SPOTIFY_REDIRECT_URL; /// 初始化OpenAI服务 Future initialize() async { if (!isInitialized) { await getAccessToken(); await connectToSpotifyRemote(); isInitialized = true; final _env = _storage.read("ENV") as Map; SPOTIFY_CLIENT_ID = _env['SPOTIFY_CLIENT_ID'] ?? ''; SPOTIFY_REDIRECT_URL = _env['SPOTIFY_REDIRECT_URL'] ?? ''; printInfo(info: "SpotifService----------- initialize"); } return true; } /// 获取Spotify SDK的版本信息 Future connectToSpotifyRemote() async { try { var result = await SpotifySdk.connectToSpotifyRemote( clientId: SPOTIFY_CLIENT_ID, redirectUrl: SPOTIFY_REDIRECT_URL, scope: 'user-read-private user-read-email streaming app-remote-control user-modify-playback-state', ); printInfo( info: "SpotifService----------- connectToSpotifyRemote $result"); } on PlatformException catch (e) { printError(info: "SpotifService------------ PlatformException$e"); } on MissingPluginException { printError(info: "SpotifService------------ MissingPluginException"); } } /// 获取Spotify的访问令牌 Future getAccessToken() async { try { var authenticationToken = await SpotifySdk.getAccessToken( clientId: SPOTIFY_CLIENT_ID, redirectUrl: SPOTIFY_REDIRECT_URL, scope: 'app-remote-control, ' 'user-modify-playback-state, ' 'playlist-read-private, ' 'playlist-modify-public,user-read-currently-playing', ); printInfo( info: "SpotifService----------- getAccessToken $authenticationToken"); return authenticationToken; } on PlatformException catch (e) { printError( info: "SpotifService------------getAccessToken ${e.code}: ${e.message}"); return Future.error('$e.code: $e.message'); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); return Future.error('not implemented'); } } //获取当前播放状态 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 { await SpotifySdk.play(spotifyUri: url); printInfo(info: "SpotifService----------- play $url"); } on PlatformException catch (e) { printError(info: "SpotifService------------play ${e.code}: ${e.message}"); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); } } //暂停 音乐 Future pause() async { try { await SpotifySdk.pause(); printInfo(info: "SpotifService----------- pause"); } on PlatformException catch (e) { printError( info: "SpotifService------------pause ${e.code}: ${e.message}"); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); } } //停止 音乐 Future resume() async { try { await SpotifySdk.resume(); printInfo(info: "SpotifService----------- resume"); } on PlatformException catch (e) { printError( info: "SpotifService------------pause ${e.code}: ${e.message}"); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); } } //下一首 音乐 Future skipNext() async { try { await SpotifySdk.skipNext(); } on PlatformException catch (e) { printError( info: "SpotifService------------pause ${e.code}: ${e.message}"); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); } } //跳过上一首 Future skipPrevious() async { try { await SpotifySdk.skipPrevious(); } on PlatformException catch (e) { printError( info: "SpotifService------------pause ${e.code}: ${e.message}"); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); } } //获取当前播放状态 Future seekTo() async { try { await SpotifySdk.seekTo(positionedMilliseconds: 20000); } on PlatformException catch (e) { printError( info: "SpotifService------------pause ${e.code}: ${e.message}"); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); } } //获取当前播放状态 Future seekToRelative() async { try { await SpotifySdk.seekToRelativePosition(relativeMilliseconds: 20000); } on PlatformException catch (e) { printError( info: "SpotifService------------pause ${e.code}: ${e.message}"); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); } } //获取当前播放状态 Future switchToLocalDevice() async { try { await SpotifySdk.switchToLocalDevice(); } on PlatformException catch (e) { printError( info: "SpotifService------------pause ${e.code}: ${e.message}"); } on MissingPluginException { printError( info: "SpotifService------------getAccessToken not implemented"); } } }