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/?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