|
|
|
@ -21,13 +21,18 @@ class QQMusicSystemFunctionHandler(private val context: Context? = null) { |
|
|
|
// Only register QQ Music search function |
|
|
|
client.registerLocalFunction( |
|
|
|
"qqmusic_search_songs", |
|
|
|
"在QQ音乐中搜索歌曲并播放,支持歌曲名和歌手名", |
|
|
|
"在QQ音乐中搜索内容,支持歌曲、专辑、歌手等搜索类型", |
|
|
|
mapOf( |
|
|
|
"type" to "object", |
|
|
|
"properties" to mapOf( |
|
|
|
"keyword" to mapOf( |
|
|
|
"type" to "string", |
|
|
|
"description" to "搜索关键词,歌曲名或歌手名" |
|
|
|
"description" to "搜索关键词,歌曲名、专辑名或歌手名" |
|
|
|
), |
|
|
|
"searchType" to mapOf( |
|
|
|
"type" to "integer", |
|
|
|
"description" to "搜索类型:0-歌曲,1-专辑,2-歌手", |
|
|
|
"default" to 0 |
|
|
|
) |
|
|
|
), |
|
|
|
"required" to listOf("keyword") |
|
|
|
@ -36,26 +41,26 @@ class QQMusicSystemFunctionHandler(private val context: Context? = null) { |
|
|
|
) |
|
|
|
|
|
|
|
// QQ音乐播放歌曲 |
|
|
|
// client.registerLocalFunction( |
|
|
|
// "qqmusic_play_songs", |
|
|
|
// "播放QQ音乐歌曲列表", |
|
|
|
// mapOf( |
|
|
|
// "type" to "object", |
|
|
|
// "properties" to mapOf( |
|
|
|
// "songMids" to mapOf( |
|
|
|
// "type" to "array", |
|
|
|
// "description" to "歌曲ID列表", |
|
|
|
// "items" to mapOf("type" to "string") |
|
|
|
// ), |
|
|
|
// "index" to mapOf( |
|
|
|
// "type" to "integer", |
|
|
|
// "description" to "开始播放的歌曲索引,默认为0" |
|
|
|
// ) |
|
|
|
// ), |
|
|
|
// "required" to listOf("songMids") |
|
|
|
// ), |
|
|
|
// QQMusicPlaySongsHandler(context) |
|
|
|
// ) |
|
|
|
client.registerLocalFunction( |
|
|
|
"qqmusic_play_songs", |
|
|
|
"播放QQ音乐歌曲列表", |
|
|
|
mapOf( |
|
|
|
"type" to "object", |
|
|
|
"properties" to mapOf( |
|
|
|
"songMids" to mapOf( |
|
|
|
"type" to "array", |
|
|
|
"description" to "歌曲ID列表", |
|
|
|
"items" to mapOf("type" to "string") |
|
|
|
), |
|
|
|
"index" to mapOf( |
|
|
|
"type" to "integer", |
|
|
|
"description" to "开始播放的歌曲索引,默认为0" |
|
|
|
) |
|
|
|
), |
|
|
|
"required" to listOf("songMids") |
|
|
|
), |
|
|
|
QQMusicPlaySongsHandler(context) |
|
|
|
) |
|
|
|
|
|
|
|
// QQ音乐播放控制 |
|
|
|
// client.registerLocalFunction( |
|
|
|
@ -197,17 +202,29 @@ private class QQMusicSearchHandler(private val context: Context?) : FunctionHand |
|
|
|
override suspend fun handle(arguments: Map<String, Any>): String { |
|
|
|
return try { |
|
|
|
val keyword = arguments["keyword"] as? String ?: return "{\"success\": false, \"message\": \"缺少搜索关键词\"}" |
|
|
|
val searchType = arguments["searchType"] as? Int ?: 0 // 默认搜索歌曲 |
|
|
|
val qqMusicSingleton = QQMusicSingleton.getInstance() |
|
|
|
var result = "" |
|
|
|
|
|
|
|
// 直接调用搜索,内部会自动处理登录检查 |
|
|
|
qqMusicSingleton.searchSongs(keyword) { searchResult -> |
|
|
|
qqMusicSingleton.searchSongs(keyword, searchType) { searchResult -> |
|
|
|
result = if (searchResult.isSuccess) { |
|
|
|
val songs = searchResult.getOrNull() ?: emptyList() |
|
|
|
val songsJson = songs.joinToString(",") { song -> |
|
|
|
|
|
|
|
// 根据搜索类型处理结果 |
|
|
|
val processedSongs = when (searchType) { |
|
|
|
0 -> { // 歌曲搜索,只返回第一个结果 |
|
|
|
if (songs.isNotEmpty()) listOf(songs.first()) else emptyList() |
|
|
|
} |
|
|
|
else -> { // 专辑、歌手等其他类型,返回所有结果 |
|
|
|
songs |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
val songsJson = processedSongs.joinToString(",") { song -> |
|
|
|
val songMap = song as Map<String, Any?> |
|
|
|
// 添加调试日志 |
|
|
|
Log.d("QQMusicSearchHandler", "原始歌曲数据: $songMap") |
|
|
|
Log.d("QQMusicSearchHandler", "原始数据: $songMap") |
|
|
|
// 修正字段名映射 |
|
|
|
val mid = songMap["mid"] ?: "unknown" |
|
|
|
val name = songMap["name"] ?: "unknown" |
|
|
|
@ -217,7 +234,15 @@ private class QQMusicSearchHandler(private val context: Context?) : FunctionHand |
|
|
|
val duration = songMap["duration"] ?: 0 |
|
|
|
"{\"mid\": \"$mid\", \"name\": \"$name\", \"artist\": \"$artist\", \"album\": \"$album\", \"duration\":$duration,\"pic\": \"$pic\"}" |
|
|
|
} |
|
|
|
"{\"success\": true, \"message\": \"搜索成功\", \"data\": [$songsJson]}" |
|
|
|
|
|
|
|
val searchTypeText = when (searchType) { |
|
|
|
0 -> "歌曲" |
|
|
|
1 -> "专辑" |
|
|
|
2 -> "歌手" |
|
|
|
else -> "内容" |
|
|
|
} |
|
|
|
|
|
|
|
"{\"success\": true, \"message\": \"${searchTypeText}搜索成功\", \"searchType\": $searchType, \"data\": [$songsJson]}" |
|
|
|
} else { |
|
|
|
val error = searchResult.exceptionOrNull() |
|
|
|
"{\"success\": false, \"message\": \"搜索失败: ${error?.message}\"}" |
|
|
|
|