From: liangzhenying <867174124@qq.com>
Date: Fri, 14 Mar 2025 10:03:44 +0000 (+0800)
Subject: 改了voicechat连接流程,每次对话前都去判断是否已经连接,如果没有连上,则先去连接
X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.js?a=commitdiff_plain;h=a05a5446781ef4f7e00df5166584f453effe4ba8;p=mirrors%2FIOTBluetooth-iOS.git
改了voicechat连接流程,每次对话前都去判断是否已经连接,如果没有连上,则先去连接
---
diff --git a/IOTBluetooth/IOTBluetooth/Example/ChatViewController.swift b/IOTBluetooth/IOTBluetooth/Example/ChatViewController.swift
index ab24436..166739b 100644
--- a/IOTBluetooth/IOTBluetooth/Example/ChatViewController.swift
+++ b/IOTBluetooth/IOTBluetooth/Example/ChatViewController.swift
@@ -65,6 +65,7 @@ class ChatViewController: UIViewController{
}
IBAiManager.shared.aiResponse = {[weak self] response in
guard let self = self else {return}
+ print("=== \(response.description)")
if response.type == 1 || response.type == 2{
if let model = list.last,model.reponseId == response.reponseId {
list.removeLast()
diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate
index 0e38908..5f7b5ff 100644
Binary files a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate and b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAiManager.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAiManager.swift
index 94d589c..e45b9bf 100644
--- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAiManager.swift
+++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAiManager.swift
@@ -83,6 +83,13 @@ public class IBAiManager{
sendAiState(isValid: true, gptKey: nil)
}
}
+ if platform == .chatGPT {
+ MixSdk.shared.chatGpt.changeModel(model:gptModel)
+ MixSdk.shared.setPlatform(platform,key: nil)
+ }
+ else{
+ MixSdk.shared.setPlatform(platform,key: nil)
+ }
}
public func checkChatptValid(_ key:String ,completeHandler: @escaping MSChatGptCheckKeyCallBack){
@@ -128,7 +135,7 @@ public class IBAiManager{
MixSdk.shared.setPlatform(platform,key: nil)
}
responseId = "\(Date().timeIntervalSince1970)"
-// MixSdk.shared.voiceChat.start()
+ MixSdk.shared.voiceChat.requestToSpeak()
}
func stopRecord(_ data:Data){
Logger.log("ç»æå½é³ï¼")
diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist
index 7b7c173..8d6b401 100644
--- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist
+++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -7,7 +7,7 @@
Example.xcscheme_^#shared#^_
orderHint
- 7
+ 2
diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example/ViewController.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example/ViewController.swift
index c822f19..4cf4e04 100644
--- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example/ViewController.swift
+++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example/ViewController.swift
@@ -19,7 +19,7 @@ class ViewController: UIViewController{
("aigc","aigcAction"),
("大模å对è¯","chatAction"),
("ChatGPT","chatGptAction"),
- // ("VoiceChat","voiceChatAction"),
+ ("VoiceChat","voiceChatAction"),
("é¿ææ¬åæè¯é³","longTtsAction"),
("ææ¬åæè¯é³","ttsAction"),
@@ -179,10 +179,10 @@ extension ViewController{
let vc = ChatGptViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
- // @objc func voiceChatAction() {
- // let vc = VoiceChatViewController()
- // self.navigationController?.pushViewController(vc, animated: true)
- // }
+ @objc func voiceChatAction() {
+ let vc = VoiceChatViewController()
+ self.navigationController?.pushViewController(vc, animated: true)
+ }
@objc func imageAction() {
let vc = ImgCompressViewController()
self.navigationController?.pushViewController(vc, animated: true)
diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example/VoiceChatViewController.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example/VoiceChatViewController.swift
index a72fa93..0385f1b 100644
--- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example/VoiceChatViewController.swift
+++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Example/Example/VoiceChatViewController.swift
@@ -1,124 +1,173 @@
-////
-//// VoiceChatViewController.swift
-//// Example
-////
-//// Created by incar on 2024/7/29.
-////
//
-//import UIKit
-//import MixSdk
-//import SnapKit
-//import Toast_Swift
-//class VoiceChatViewController: UIViewController{
-// var voiceData:NSMutableData = NSMutableData()
-// let textView = UITextView()
-//
-// override func viewDidLoad() {
-// super.viewDidLoad()
-// // Do any additional setup after loading the view.
-//
-// setupViews()
-// setupCallBack()
-// }
-//
-// func setupViews(){
-// self.title = "VoiceChat"
-// self.view.backgroundColor = .white
-//
-// let toolBar = SendRecordToolBar()
-// toolBar.textField.text = "æ·±å³ä»å¤©å¤©æ°æä¹æ ·"
-// toolBar.delegate = self
-// view.addSubview(toolBar)
-// toolBar.snp.makeConstraints { make in
-// make.width.centerX.equalToSuperview()
-// make.height.equalTo(150)
-// make.bottom.equalToSuperview().inset(0)
-// }
-//
-// textView.isEditable = false
-// view.addSubview(textView)
-// textView.snp.makeConstraints { make in
-// make.top.equalToSuperview().inset(80)
-// make.left.right.equalToSuperview()
-// make.bottom.equalTo(toolBar.snp.top)
-// }
-// }
-// func setupCallBack(){
-// MixSdk.shared.voiceChat.eventCallBack = { [weak self] model in
-// guard let self = self else {
-// return
-// }
-// if model.header.name == "RespondingEnded"{
-// self.saveToFile()
-// self.voiceData = NSMutableData()
-// }
-// }
-// MixSdk.shared.voiceChat.replyCallBack = {[weak self] data in
-//// print(data);
-// self?.voiceData.append(data)
-// }
-// }
-//
-// func saveToFile(){
-// let pcmPath = NSHomeDirectory() + "/Documents/1.pcm"
-// let mp3Path = NSHomeDirectory() + "/Documents/1.mp3"
-//
-// let url = URL(fileURLWithPath: pcmPath)
-// do {
-// try self.voiceData.write(to: url, options: .atomic)
-// print(pcmPath)
-// let result = MSAudioConvert.pcmToMp3(pcmPath: pcmPath, pcmSampleRate: 24000, mp3Path: mp3Path)
-// if result {
-// PlayerManager.shared.play(filePath: mp3Path)
-// }
-// }
-// catch{
-// print(error)
-// }
-// }
-//}
-//extension VoiceChatViewController:SendRecordToolBarDelegate{
+// ChatViewController.swift
+// Example
//
-// func sendRecordToolBarStartRecord(_ toolBar: SendRecordToolBar) {
-// view.makeToastActivity(.center)
-// //1ãå¼å§
-// MixSdk.shared.voiceChat.willSpeedch()
-// }
-//
-// func sendRecordToolBar(_ toolBar: SendRecordToolBar, record data: Data) {
-// //2ãåéçè¯é³æµ
-// //注ædemoä¸çææºå½é³çè¯é³ç¼ç æ¯pcm,妿èç设å¤ä¼ è¿æ¥çè¯é³æ°æ®æ¯sbcï¼å使ç¨ä¸é¢çæ¹æ³
-// MixSdk.shared.voiceChat.sendVoice(pcmData: data)
-//// MixSdk.shared.chatGptTrans.sendVoice(sbcData: data)
-// }
-//
-// func sendRecordToolBarStopRecord(_ toolBar: SendRecordToolBar) {
-// self.view.hideToastActivity()
-// //3ã忢ï¼
-//// MixSdk.shared.chatGptTrans.stop {[weak self] text, error in
-//// guard let self = self else {return}
-//// self.view.hideToastActivity()
-//// toolBar.textField.text = text ?? ""
-//// }
-// }
+// Created by incar on 2024/6/17.
//
-// func sendRecordToolBar(_ toolBar: SendRecordToolBar, _ text: String) {
-//// textView.text += "[send] " + text + "\n"
-//// let range = NSMakeRange(textView.text.count - 1, 1)
-//// textView.scrollRangeToVisible(range)
-////
-//// view.makeToastActivity(.center)
-//// MixSdk.shared.chatGpt.send(content: text) {[weak self] response, error in
-//// guard let self = self else {return}
-//// self.view.hideToastActivity()
-//// if let model = response,let choices = model.choices.first {
-//// textView.text += "[receive] " + choices.message.content + "\n"
-//// let range = NSMakeRange(textView.text.count - 1, 1)
-//// textView.scrollRangeToVisible(range)
-//// }
-//// else if let err = error{
-//// self.view.makeToast(err.message)
-//// }
-//// }
-// }
-//}
+
+import UIKit
+import MixSdk
+import SnapKit
+import Toast_Swift
+class VoiceChatViewController: UIViewController{
+
+
+ let textView = UITextView()
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+ // Do any additional setup after loading the view.
+
+ setupViews()
+ setupChatCallBack()
+ //设置é»è®¤å¹³å°
+ self.title = MSPlatform.deepseek.title
+ MixSdk.shared.setPlatform(.deepseek)
+ }
+
+
+ func setupViews(){
+ self.title = "大模å对è¯"
+ self.view.backgroundColor = .white
+ self.navigationItem.rightBarButtonItems = [UIBarButtonItem(title: "忢平å°", style: .done, target: self, action: #selector(changePlatform)),UIBarButtonItem(title: "clean", style: .done, target: self, action: #selector(cleanAction))]
+
+ let toolBar = SendToolBar()
+ toolBar.textField.text = "åä¸ä¸ª800åç使"
+ toolBar.textField.text = "ä½ æ¯è°"
+
+ toolBar.delegate = self
+ view.addSubview(toolBar)
+ toolBar.snp.makeConstraints { make in
+ make.width.centerX.equalToSuperview()
+ make.height.equalTo(60)
+ make.bottom.equalToSuperview().inset(20)
+ }
+
+ textView.isEditable = false
+ view.addSubview(textView)
+ textView.snp.makeConstraints { make in
+ make.top.equalToSuperview().inset(80)
+ make.left.right.equalToSuperview()
+ make.bottom.equalTo(toolBar.snp.top)
+ }
+ }
+
+ func setupChatCallBack(){
+ let chat = MixSdk.shared.voiceChat
+ chat.connectStateCallBack = {[weak self] state in
+ guard let self = self else {return}
+ if state == .disConnected{
+ textView.text += Date().format(.full) + " æå¼è¿æ¥ããã" + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ }
+ else{
+ textView.text += Date().format(.full) + " å·²è¿æ¥ããã" + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ }
+ }
+
+ chat.callBack = {[weak self] model in
+ guard let self = self else {return}
+
+ if model.type == "start" {
+ textView.text += Date().format(.full) + "[receive] "
+ }
+ else if model.type == "end" {
+ textView.text += "\n"
+ }
+ else if model.type == "middle"{
+ textView.text += model.content ?? ""
+ }
+ let range = NSMakeRange(self.textView.text.count - 1, 1)
+ self.textView.scrollRangeToVisible(range)
+ }
+ }
+
+
+ @objc func changePlatform(){
+ let vc = UIAlertController(title: nil, message: "è¯·éæ©", preferredStyle: .actionSheet)
+ vc.addAction( UIAlertAction(title: MSPlatform.deepseek.title, style: .default) { action in
+ self.title = MSPlatform.deepseek.title
+ MixSdk.shared.setPlatform(.deepseek)
+ })
+ vc.addAction( UIAlertAction(title: MSPlatform.doubao.title, style: .default) { action in
+ self.title = MSPlatform.doubao.title
+ MixSdk.shared.setPlatform(.doubao)
+ })
+ vc.addAction(UIAlertAction(title:MSPlatform.yiyan.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.yiyan)
+ self.title = MSPlatform.yiyan.title
+ })
+ vc.addAction(UIAlertAction(title: MSPlatform.kimi.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.kimi)
+ self.title = MSPlatform.kimi.title
+ })
+ vc.addAction(UIAlertAction(title: MSPlatform.xunfei.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.xunfei)
+ self.title = MSPlatform.xunfei.title
+ })
+ vc.addAction(UIAlertAction(title: MSPlatform.aliyun.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.aliyun)
+ self.title = MSPlatform.aliyun.title
+ })
+ vc.addAction(UIAlertAction(title: MSPlatform.chatGPT.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.chatGPT, key: gptKey)
+ MixSdk.shared.chatGpt.changeModel(model: gptModel)
+ self.title = MSPlatform.chatGPT.title
+ })
+
+ vc.addAction(UIAlertAction(title: "åæ¶", style: .cancel) { action in
+ })
+
+ self.present(vc, animated: true)
+ }
+
+ @objc func cleanAction(){
+ MixSdk.shared.voiceChat.reset()
+ }
+}
+extension VoiceChatViewController:SendToolBarDelegate{
+ func sendToolBar(_ toolBar: SendToolBar, _ text: String) {
+ if(MixSdk.shared.aiPlatform == .chatGPT){
+ textView.text += Date().format(.full) + "[send] " + text + "\n"
+
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ let content = text
+ view.makeToastActivity(.center)
+ // åéchatgpt
+ MixSdk.shared.chatGpt.send(content: content) {[weak self] response, error in
+ guard let self = self else {return}
+ self.view.hideToastActivity()
+ if let model = response,let choices = model.choices.first {
+ textView.text += Date().format(.full) + "[receive] " + choices.message.content + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ }
+ else if let err = error{
+ self.view.makeToast(err.message)
+ }
+ }
+ }
+ else{
+ // åéå
¶ä»å¹³å°
+ if MixSdk.shared.voiceChat.connectState == .connected {
+ textView.text += Date().format(.full) + "[send] " + text + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+
+ MixSdk.shared.voiceChat.requestToSpeak()
+ MixSdk.shared.voiceChat.send(content: text)
+
+ }
+ else{
+ //å·²æå¼è¿æ¥
+ textView.text += Date().format(.full) + "æå¼è¿æ¥ããã" + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ }
+ }
+ }
+}
diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/MixSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/MixSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate
index 0367225..706014f 100644
Binary files a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/MixSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate and b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/MixSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/MixSdk/MixSdk/MSVoiceChat.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/MixSdk/MixSdk/MSVoiceChat.swift
index 4c753e7..1b490cd 100644
--- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/MixSdk/MixSdk/MSVoiceChat.swift
+++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/MixSdk/MixSdk/MSVoiceChat.swift
@@ -53,7 +53,7 @@ public class MSVoiceChat {
}
/// å¼å§ä»»å¡
- func start(){
+ func start(){
openWebSocket()
setupHeartBeat()
}
@@ -63,7 +63,7 @@ public class MSVoiceChat {
timer = Timer.scheduledTimer(withTimeInterval: MSConfig.wsHeartBeatTime, repeats: true) { [weak self] _ in
guard let self = self else {return}
if socket?.readyState == .open{
- self.socket?.ping()
+// self.socket?.ping()
}
else{
self.openWebSocket()
@@ -119,12 +119,6 @@ public class MSVoiceChat {
let cmd = params.jsonString() ?? ""
Logger.log("send" ,cmd)
socket?.send(text: cmd)
-
- let tmpModel = MSChatModel()
- tmpModel.type = "start"
- tmpModel.content = nil
- lastChatModel = tmpModel
- callBack?(tmpModel)
}
/// ç»æè¯´è¯
@@ -167,6 +161,19 @@ public class MSVoiceChat {
/// ææï¼è¯·æ±è¯´è¯
public func requestToSpeak(){
+ if socket?.readyState == .open{
+ //self.socket?.ping()
+ sendSpeakCMD()
+ }
+ else{
+ self.openWebSocket()
+ DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
+ self.sendSpeakCMD()
+ }
+ }
+ }
+
+ func sendSpeakCMD(){
Logger.log("å¼å§è¯·æ±è¯´è¯")
let params:[String:Any] =
[
@@ -183,7 +190,11 @@ public class MSVoiceChat {
Logger.log("send" ,cmd)
socket?.send(text: cmd)
-
+ let tmpModel = MSChatModel()
+ tmpModel.type = "start"
+ tmpModel.content = nil
+ lastChatModel = tmpModel
+ callBack?(tmpModel)
}
/// æ¬å°å¼å§ææ¾
public func startPlay(){
@@ -228,8 +239,9 @@ extension MSVoiceChat{
currentEvent = nil;
// let url = MSConfig.webSocketUrl(api: "voice-chat/ws",addPlatform: false)
-// let url = "ws://118.145.158.207:22082/voice-chat/ws?platform=raisound&bot_type=doubao&is_auth=0&dt=\(MixSdk.shared.auth.deviceToken)"
let url = "ws://118.145.158.207:22080/voice-chat/v2.1/ws?platform=raisound&bot_type=\(MixSdk.shared.aiPlatform.voiceChatValue)&is_auth=0&dt=\(MixSdk.shared.auth.deviceToken)"
+// let url = MSConfig.wsBaseURL + "voice-chat/v2.1/ws?platform=raisound&bot_type=\(MixSdk.shared.aiPlatform.voiceChatValue)&is_auth=1&dt=\(MixSdk.shared.auth.deviceToken)"
+
Logger.log(url)
socket = WebSocket(url)
socket?.delegate = self
diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Pods/Pods.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Pods/Pods.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist
index cdc0448..eaee3d7 100644
--- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Pods/Pods.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist
+++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/MixSdk/Pods/Pods.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -7,32 +7,32 @@
IQKeyboardManagerSwift-IQKeyboardManagerSwift.xcscheme_^#shared#^_
orderHint
- 4
+ 6
IQKeyboardManagerSwift.xcscheme_^#shared#^_
orderHint
- 6
+ 4
Pods-Example.xcscheme_^#shared#^_
orderHint
- 5
+ 3
SnapKit-SnapKit_Privacy.xcscheme_^#shared#^_
orderHint
- 3
+ 7
SnapKit.xcscheme_^#shared#^_
orderHint
- 1
+ 5
Toast-Swift.xcscheme_^#shared#^_
orderHint
- 2
+ 1