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.
211 lines
6.8 KiB
211 lines
6.8 KiB
/* 暂时关闭:spotify_sdk plugin 已在 pubspec.yaml 注释掉,恢复时把本文件首尾的 block comment 解开即可。
|
|
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'dart:convert';
|
|
import '../../data/models/appconfig.dart';
|
|
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<bool> initialize() async {
|
|
if (!isInitialized) {
|
|
// final _env = _storage.read("ENV") as Map<String, String>;
|
|
SPOTIFY_CLIENT_ID = AppConfig.env('SPOTIFY_CLIENT_ID') ?? '';
|
|
SPOTIFY_REDIRECT_URL = AppConfig.env('SPOTIFY_REDIRECT_URL') ?? '';
|
|
printInfo(
|
|
info:
|
|
"SpotifService----------- initialize SPOTIFY_CLIENT_ID:${SPOTIFY_CLIENT_ID} SPOTIFY_REDIRECT_URL:${SPOTIFY_REDIRECT_URL}");
|
|
|
|
await getAccessToken();
|
|
await connectToSpotifyRemote();
|
|
isInitialized = true;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// 获取Spotify SDK的版本信息
|
|
Future<void> 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<String> 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<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 {
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
|