From 3f81ae2d7af70a644d6f9a58fa7e8d35fffb8bdb Mon Sep 17 00:00:00 2001 From: wolfplus Date: Sat, 10 May 2025 22:34:20 +0100 Subject: [PATCH] add --- .../agent_service/AgentService.kt | 55 +++++++++++++++++++ .../open_ai_service/OpenAIService.kt | 6 +- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt index b324326dc..3bea170d1 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt @@ -985,4 +985,59 @@ object AgentService : CoroutineScope { return true } + + /** + * 简单音频播放器,用于播放提示音等 + */ + class AudioPlayer(private val context: Context) { + private var mediaPlayer: MediaPlayer? = null + + /** + * 播放音频资源 + * @param resId 资源ID + */ + fun playAudio(resId: Int) { + try { + // 释放之前的资源 + release() + + // 创建播放器并设置资源 + mediaPlayer = MediaPlayer().apply { + // 设置资源 + context.resources.openRawResourceFd(resId)?.use { fd -> + setDataSource(fd.fileDescriptor, fd.startOffset, fd.length) + } + + // 播放完成后自动释放资源 + setOnCompletionListener { + release() + } + + // 准备并播放 + prepare() + start() + } + + FileLogger.d(TAG, "开始播放音频资源") + } catch (e: Exception) { + FileLogger.e(TAG, "播放音频资源异常: ${e.message}", e) + release() + } + } + + /** + * 释放资源 + */ + fun release() { + try { + mediaPlayer?.apply { + if (isPlaying) stop() + release() + } + mediaPlayer = null + } catch (e: Exception) { + // 忽略异常 + } + } + } } \ No newline at end of file diff --git a/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/OpenAIService.kt b/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/OpenAIService.kt index dbc1af023..a94f7f627 100644 --- a/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/OpenAIService.kt +++ b/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/OpenAIService.kt @@ -355,7 +355,11 @@ class OpenAIService(private val context: Context? = null) { callback.onFunctionCallResult(functionCall, resultObj) // 发送结果到OpenAI继续对话 - val resultContent = resultObj.optString("context", "工具调用失败") + val resultContent = if (resultObj.has("context") && resultObj.getString("context").isNotEmpty()) { + resultObj.getString("context") + } else { + "工具调用失败" + } sendFunctionCallResult( messages = messages, functionCall = functionCall,