import 'package:flutter/services.dart'; class QqMusic { static final QqMusic _instance = QqMusic._internal(); /// 获取单例 factory QqMusic() => _instance; // 方法通道 final MethodChannel _methodChannel = const MethodChannel( 'com.yunqiinnovation.qq_music', ); // 内部构造函数 QqMusic._internal() {} /// 登陆qq音乐 /// 返回登录结果,包含 openId, openToken, expireTime Future> login() async { try { final result = await _methodChannel.invokeMethod('login'); return Map.from(result); } on PlatformException catch (e) { return {'success': false, 'error': e.message ?? '登录失败', 'code': e.code}; } } /// 搜索歌曲 /// [keyword] 搜索关键词 /// [searchType] 搜索类型 0-歌曲 1-专辑 2-歌手 Future> searchSongs({ required String keyword, int searchType = 7, // Data.SearchType.SEARCH_TYPE_SONG }) async { try { final result = await _methodChannel.invokeMethod('searchSongs', { 'keyword': keyword, 'searchType': searchType, }); return Map.from(result); } on PlatformException catch (e) { return {'success': false, 'error': e.message ?? '搜索失败', 'code': e.code}; } } /// 播放歌曲列表 /// [songMids] 歌曲MID列表 /// [index] 开始播放的索引,默认为0 Future> playSongs({ required List songMids, int index = 0, }) async { try { final result = await _methodChannel.invokeMethod('playSongs', { 'songMids': songMids, 'index': index, }); return Map.from(result); } on PlatformException catch (e) { return {'success': false, 'error': e.message ?? '播放失败', 'code': e.code}; } } /// 播放音乐 Future> playMusic() async { try { final result = await _methodChannel.invokeMethod('playMusic'); return Map.from(result); } on PlatformException catch (e) { return {'success': false, 'error': e.message ?? '播放失败', 'code': e.code}; } } /// 暂停音乐 Future> pauseMusic() async { try { final result = await _methodChannel.invokeMethod('pauseMusic'); return Map.from(result); } on PlatformException catch (e) { return {'success': false, 'error': e.message ?? '暂停失败', 'code': e.code}; } } /// 自动暂停音乐 Future> autopauseMusic() async { try { final result = await _methodChannel.invokeMethod('autopauseMusic'); return Map.from(result); } on PlatformException catch (e) { return {'success': false, 'error': e.message ?? '自动暂停失败', 'code': e.code}; } } /// 恢复播放 Future> resumeMusic() async { try { final result = await _methodChannel.invokeMethod('resumeMusic'); return Map.from(result); } on PlatformException catch (e) { return {'success': false, 'error': e.message ?? '恢复播放失败', 'code': e.code}; } } /// 停止音乐 Future> stopMusic() async { try { final result = await _methodChannel.invokeMethod('stopMusic'); return Map.from(result); } on PlatformException catch (e) { return {'success': false, 'error': e.message ?? '停止失败', 'code': e.code}; } } /// 下一首 Future> skipToNext() async { try { final result = await _methodChannel.invokeMethod('skipToNext'); return Map.from(result); } on PlatformException catch (e) { return { 'success': false, 'error': e.message ?? '切换下一首失败', 'code': e.code, }; } } /// 上一首 Future> skipToPrevious() async { try { final result = await _methodChannel.invokeMethod('skipToPrevious'); return Map.from(result); } on PlatformException catch (e) { return { 'success': false, 'error': e.message ?? '切换上一首失败', 'code': e.code, }; } } /// 获取播放状态 Future> getPlaybackState() async { try { final result = await _methodChannel.invokeMethod('getPlaybackState'); return Map.from(result); } on PlatformException catch (e) { return { 'success': false, 'error': e.message ?? '获取播放状态失败', 'code': e.code, }; } } /// 获取当前播放歌曲 Future> getCurrentSong() async { try { final result = await _methodChannel.invokeMethod('getCurrentSong'); return Map.from(result); } on PlatformException catch (e) { return { 'success': false, 'error': e.message ?? '获取当前歌曲失败', 'code': e.code, }; } } /// 获取播放时长 Future> getPlayTime() async { try { final result = await _methodChannel.invokeMethod('getPlayTime'); return Map.from(result); } on PlatformException catch (e) { return { 'success': false, 'error': e.message ?? '获取播放时长失败', 'code': e.code, }; } } /// 同步当前播放信息 Future> syncCurrentPlayInfo() async { try { final result = await _methodChannel.invokeMethod('syncCurrentPlayInfo'); return Map.from(result); } catch (e) { throw Exception('同步播放信息失败: $e'); } } /// 获取平台版本 Future getPlatformVersion() async { try { final version = await _methodChannel.invokeMethod( 'getPlatformVersion', ); return version; } on PlatformException catch (e) { throw Exception('获取平台版本失败: ${e.message}'); } } } /// 登录结果类 class QQMusicLoginResult { final bool success; final String? openId; final String? openToken; final String? expireTime; final String? error; final String? code; QQMusicLoginResult({ required this.success, this.openId, this.openToken, this.expireTime, this.error, this.code, }); factory QQMusicLoginResult.fromMap(Map map) { return QQMusicLoginResult( success: map['success'] ?? false, openId: map['openId'], openToken: map['openToken'], expireTime: map['expireTime'], error: map['error'], code: map['code'], ); } } /// 搜索结果类 class QQMusicSearchResult { final bool success; final List? songs; final String? error; final String? code; QQMusicSearchResult({ required this.success, this.songs, this.error, this.code, }); factory QQMusicSearchResult.fromMap(Map map) { List? songs; if (map['songs'] != null) { songs = (map['songs'] as List) .map((song) => QQMusicSong.fromMap(Map.from(song))) .toList(); } return QQMusicSearchResult( success: map['success'] ?? false, songs: songs, error: map['error'], code: map['code'], ); } } /// 歌曲信息类 class QQMusicSong { final String? mid; final String? id; final String? name; final String? singer; final String? album; final int? duration; final String? pic; QQMusicSong({ this.mid, this.id, this.name, this.singer, this.album, this.duration, this.pic, }); factory QQMusicSong.fromMap(Map map) { return QQMusicSong( mid: map['mid'], id: map['id'], name: map['name'], singer: map['singer'], album: map['album'], duration: map['duration'], pic: map['pic'], ); } Map toMap() { return { 'mid': mid, 'id': id, 'name': name, 'singer': singer, 'album': album, 'duration': duration, 'pic': pic, }; } }