You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
5.6 KiB
176 lines
5.6 KiB
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");
|
|
}
|
|
}
|
|
}
|
|
|