Browse Source

上传聊天语音优化

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
c80a7c8c5b
  1. 19
      android/app/src/main/kotlin/com/yunqiinnovation/deepsound/VoiceInteractionHandler.kt
  2. 7
      lib/modules/chat/controllers/chat_controller.dart
  3. 51
      local_plugins/spotify_sdk/android/src/main/kotlin/de/minimalme/spotify_sdk/SpotifySdkHelper.kt

19
android/app/src/main/kotlin/com/yunqiinnovation/deepsound/VoiceInteractionHandler.kt

@ -12,6 +12,7 @@ import com.yunqiinnovation.azure_speech.AzureAsrHelper
import com.yunqiinnovation.azure_speech.AzureTtsHelper import com.yunqiinnovation.azure_speech.AzureTtsHelper
import com.yunqiinnovation.open_ai_service.OpenAIService import com.yunqiinnovation.open_ai_service.OpenAIService
import com.yunqiinnovation.open_ai_service.SystemFunctionHandler import com.yunqiinnovation.open_ai_service.SystemFunctionHandler
import de.minimalme.spotify_sdk.SpotifySdkHelper
/** /**
@ -38,7 +39,9 @@ class VoiceInteractionHandler(
// OpenAI服务 // OpenAI服务
private val openAIService = OpenAIService(context.applicationContext) private val openAIService = OpenAIService(context.applicationContext)
private var spotifyHelper: SpotifySdkHelper? = null
// 当前用户输入 // 当前用户输入
private var currentUserInput = "" private var currentUserInput = ""
@ -314,7 +317,19 @@ class VoiceInteractionHandler(
notifyStateChanged() notifyStateChanged()
} }
} }
/*
* 播放音乐。Spotify
* */
fun playSpotifyMusic(url:String){
if (spotifyHelper == null){
spotifyHelper = SpotifySdkHelper(context).apply {
initialize(spotifyclientid,spotifyRedirectUrl)
}
}
spotifyHelper?.play(url)
}
/** /**
* 播放AI回复 * 播放AI回复
*/ */

7
lib/modules/chat/controllers/chat_controller.dart

@ -414,7 +414,7 @@ class ChatController extends GetxController {
if (chunk['card_spotify'] != null) { if (chunk['card_spotify'] != null) {
play(chunk['card_spotify']); play(chunk['card_spotify']);
} }
} else { } else if (chunk is String) {
fullResponse += chunk; fullResponse += chunk;
// 更新中间状态的消息内容 // 更新中间状态的消息内容
@ -422,7 +422,8 @@ class ChatController extends GetxController {
// 如果TTS启用,使用流式处理发送到TTS // 如果TTS启用,使用流式处理发送到TTS
if (isTtsEnabled.value) { if (isTtsEnabled.value) {
_ttsService.speakStream(chunk); String str = chunk.replaceAll('*', '');
_ttsService.speakStream(str);
} }
} }
}, },
@ -456,7 +457,7 @@ class ChatController extends GetxController {
final List<Map<String, String>> messageHistory = []; final List<Map<String, String>> messageHistory = [];
// 只取最近的10条消息作为上下文 // 只取最近的10条消息作为上下文
final recentMessages = messages.where((m) => !m.isIntermediate).toList(); final recentMessages = messages.where((m) => m.role == "user").toList();
final startIndex = final startIndex =
recentMessages.length > 10 ? recentMessages.length - 10 : 0; recentMessages.length > 10 ? recentMessages.length - 10 : 0;

51
local_plugins/spotify_sdk/android/src/main/kotlin/de/minimalme/spotify_sdk/SpotifySdkService.kt → local_plugins/spotify_sdk/android/src/main/kotlin/de/minimalme/spotify_sdk/SpotifySdkHelper.kt

@ -2,7 +2,6 @@ package de.minimalme.spotify_sdk
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent
import android.util.Log import android.util.Log
import com.spotify.android.appremote.api.ConnectionParams import com.spotify.android.appremote.api.ConnectionParams
import com.spotify.android.appremote.api.Connector import com.spotify.android.appremote.api.Connector
@ -20,45 +19,33 @@ import com.spotify.android.appremote.api.error.UserNotAuthorizedException
import com.spotify.sdk.android.auth.AuthorizationClient import com.spotify.sdk.android.auth.AuthorizationClient
import com.spotify.sdk.android.auth.AuthorizationRequest import com.spotify.sdk.android.auth.AuthorizationRequest
import com.spotify.sdk.android.auth.AuthorizationResponse import com.spotify.sdk.android.auth.AuthorizationResponse
import de.minimalme.spotify_sdk.subscriptions.CapabilitiesChannel
import de.minimalme.spotify_sdk.subscriptions.ConnectionStatusChannel class SpotifySdkHelper(private val context: Context? = null) {
import de.minimalme.spotify_sdk.subscriptions.PlayerContextChannel
import de.minimalme.spotify_sdk.subscriptions.PlayerStateChannel
import de.minimalme.spotify_sdk.subscriptions.UserStatusChannel
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import kotlinx.event.SetEvent
import kotlinx.event.event
class SpotifySdkService(private val context: Context? = null) {
// application context // application context
private var applicationContext : Context? = null private var applicationContext : Context? = null
private var applicationActivity : Activity? = null private var applicationActivity : Activity? = null
// method channel // method channel
private lateinit var methodChannel : MethodChannel
private val channelName = "spotify_sdk"
private val loggingTag = "spotify_sdk"
private val loggingTag = "spotify_sdk"
private val errorConnecting = "errorConnecting"
private val errorDisconnecting = "errorDisconnecting"
private val errorConnection = "errorConnection" private val errorConnection = "errorConnection"
private val errorAuthenticationToken = "authenticationTokenError"
private val requestCodeAuthentication = 1337 private val requestCodeAuthentication = 1337
private var spotifyAppRemote: SpotifyAppRemote? = null private var spotifyAppRemote: SpotifyAppRemote? = null
private var playerApi: PlayerApi? = null private var playerApi: PlayerApi? = null
public fun connectToSpotify(clientId: String?, redirectUrl: String?, result: MethodChannel.Result) { fun initialize(clientId: String, redirectUrl: String): Boolean {
getAccessToken(clientId,redirectUrl,"");
connectToSpotify(clientId,redirectUrl)
return true;
}
public fun connectToSpotify(clientId: String?, redirectUrl: String?) {
if (clientId.isNullOrBlank() || redirectUrl.isNullOrBlank()) { if (clientId.isNullOrBlank() || redirectUrl.isNullOrBlank()) {
result.error(errorConnecting, "client id or redirectUrl are not set or have invalid format", "") // result.error(errorConnecting, "client id or redirectUrl are not set or have invalid format", "")
} else { } else {
val connectionParams = ConnectionParams.Builder(clientId) val connectionParams = ConnectionParams.Builder(clientId)
.setRedirectUri(redirectUrl) .setRedirectUri(redirectUrl)
@ -74,7 +61,7 @@ class SpotifySdkService(private val context: Context? = null) {
initiallyConnected = true initiallyConnected = true
Log.i(loggingTag, "App Remote successfully connected") Log.i(loggingTag, "App Remote successfully connected")
result.success(true) // result.success(true)
} }
override fun onFailure(throwable: Throwable) { override fun onFailure(throwable: Throwable) {
@ -154,20 +141,20 @@ class SpotifySdkService(private val context: Context? = null) {
// ) // )
} else { } else {
// throw exception as the connect method // throw exception as the connect method
result.error(errorCode, errorMessage, errorDetails) // result.error(errorCode, errorMessage, errorDetails)
} }
} }
}) })
} }
} }
public fun getAccessToken(clientId: String?, redirectUrl: String?, scope: String?, result: MethodChannel.Result) { public fun getAccessToken(clientId: String?, redirectUrl: String?, scope: String?) {
if (applicationActivity == null) { if (applicationActivity == null) {
throw IllegalStateException("getAccessToken needs a foreground activity") throw IllegalStateException("getAccessToken needs a foreground activity")
} }
if (clientId.isNullOrBlank() || redirectUrl.isNullOrBlank()) { if (clientId.isNullOrBlank() || redirectUrl.isNullOrBlank()) {
result.error(errorConnecting, "client id or redirectUrl are not set or have invalid format", "") // result.error(errorConnecting, "client id or redirectUrl are not set or have invalid format", "")
} else { } else {
//Convert String? scope to Array. Delimiter set as comma "," //Convert String? scope to Array. Delimiter set as comma ","
val scopeArray = scope?.split(",")?.toTypedArray() val scopeArray = scope?.split(",")?.toTypedArray()
@ -187,14 +174,14 @@ class SpotifySdkService(private val context: Context? = null) {
} }
} }
public fun disconnectFromSpotify(result: MethodChannel.Result) { public fun disconnectFromSpotify() {
if (spotifyAppRemote != null && spotifyAppRemote!!.isConnected) { if (spotifyAppRemote != null && spotifyAppRemote!!.isConnected) {
SpotifyAppRemote.disconnect(spotifyAppRemote) SpotifyAppRemote.disconnect(spotifyAppRemote)
result.success(true) // result.success(true)
} else if (!spotifyAppRemote!!.isConnected) { } else if (!spotifyAppRemote!!.isConnected) {
result.error(errorDisconnecting, "could not disconnect spotify remote", "you are not connected, no need to disconnect") // result.error(errorDisconnecting, "could not disconnect spotify remote", "you are not connected, no need to disconnect")
} else { } else {
result.error(errorDisconnecting, "could not disconnect spotify remote", "spotifyAppRemote is not set") // result.error(errorDisconnecting, "could not disconnect spotify remote", "spotifyAppRemote is not set")
} }
} }
Loading…
Cancel
Save