Browse Source

上传qq音乐代码

weicu
liwei1dao 1 year ago
parent
commit
8d566fd967
  1. 2
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPSubClient.kt
  2. 79
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/QQMusicSystemFunctionHandler.kt

2
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPSubClient.kt

@ -433,6 +433,7 @@ class MCPSubClient(
} }
} }
} }
/** /**
* 处理连接错误并尝试重连 * 处理连接错误并尝试重连
*/ */
@ -453,6 +454,7 @@ class MCPSubClient(
// 可以在这里添加通知或回调,告知上层连接彻底失败 // 可以在这里添加通知或回调,告知上层连接彻底失败
} }
} }
/** /**
* 关闭连接 * 关闭连接
*/ */

79
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/QQMusicSystemFunctionHandler.kt

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

Loading…
Cancel
Save