9 changed files with 243 additions and 76 deletions
@ -1,11 +1,7 @@ |
|||||
{ |
{ |
||||
"mcpServers": { |
"mcpServers": { |
||||
|
"deepsound-mcp": { |
||||
"amap-amap-sse": { |
"url": "https://api.deapsound.com/sse" |
||||
"url": "https://mcp.amap.com/sse?key=e5fdc9605eabdeb5626f18f5721f343d" |
|
||||
}, |
|
||||
"web-search": { |
|
||||
"url": "http://mcp.ideapsound.com:8000/sse" |
|
||||
} |
} |
||||
|
|
||||
} |
} |
||||
|
|||||
@ -0,0 +1,176 @@ |
|||||
|
import 'dart:async'; |
||||
|
import 'dart:io'; |
||||
|
import 'dart:convert'; |
||||
|
import 'package:flutter_dotenv/flutter_dotenv.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; |
||||
|
|
||||
|
/// 初始化OpenAI服务 |
||||
|
Future<bool> initialize() async { |
||||
|
if (!isInitialized) { |
||||
|
getAccessToken(); |
||||
|
connectToSpotifyRemote(); |
||||
|
isInitialized = true; |
||||
|
printInfo(info: "SpotifService----------- initialize"); |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/// 获取Spotify SDK的版本信息 |
||||
|
Future<void> connectToSpotifyRemote() async { |
||||
|
try { |
||||
|
var result = await SpotifySdk.connectToSpotifyRemote( |
||||
|
clientId: dotenv.env['SPOTIFY_CLIENT_ID'].toString(), |
||||
|
redirectUrl: dotenv.env['SPOTIFY_REDIRECT_URL'].toString(), |
||||
|
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<String> getAccessToken() async { |
||||
|
try { |
||||
|
var authenticationToken = await SpotifySdk.getAccessToken( |
||||
|
clientId: dotenv.env['SPOTIFY_CLIENT_ID'].toString(), |
||||
|
redirectUrl: dotenv.env['SPOTIFY_REDIRECT_URL'].toString(), |
||||
|
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<void> 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<void> 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<void> 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<void> 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<void> 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<void> 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<void> 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<void> 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"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue