diff --git a/App.vue b/App.vue index 43adea6..412bcca 100644 --- a/App.vue +++ b/App.vue @@ -31,7 +31,7 @@ return 'XHJ' } // #endif - return 'DEV' + return 'XHJ' } }, computed: { diff --git a/pages/p2p/p2pPlayer.vue b/pages/p2p/p2pPlayer.vue index 7f287bf..4bc006d 100644 --- a/pages/p2p/p2pPlayer.vue +++ b/pages/p2p/p2pPlayer.vue @@ -250,7 +250,7 @@ return data }) - const cleanupResources = () => { + const cleanupResources = async () => { if (cleanupCalled.value) return cleanupCalled.value = true @@ -263,8 +263,8 @@ } releaseRecord() if (deviceInfo.value) { - stopSendServiceFunction(`${deviceInfo.value.productId}/${deviceInfo.value.deviceName}`) - stopServiceFunction(`${deviceInfo.value.productId}/${deviceInfo.value.deviceName}`) + await stopSendServiceFunction(`${deviceInfo.value.productId}/${deviceInfo.value.deviceName}`) + await stopServiceFunction(`${deviceInfo.value.productId}/${deviceInfo.value.deviceName}`) } // #endif // #ifdef MP-WEIXIN @@ -545,8 +545,12 @@ // #endif } - const callback = audioData => { - dataSendFunction(`${deviceInfo.value.productId}/${deviceInfo.value.deviceName}`, audioData) + const callback = async audioData => { + console.log('传输数据', audioData) + await dataSendFunction( + `${deviceInfo.value.productId}/${deviceInfo.value.deviceName}`, + audioData + ) } const handlePlaySuccess = () => { diff --git a/uni_modules/xhj-record/utssdk/app-ios/hybrid.swift b/uni_modules/xhj-record/utssdk/app-ios/hybrid.swift index c5194f2..09a47b8 100644 --- a/uni_modules/xhj-record/utssdk/app-ios/hybrid.swift +++ b/uni_modules/xhj-record/utssdk/app-ios/hybrid.swift @@ -49,62 +49,69 @@ public class AudioRecorderManager: NSObject { return } - let session = AVAudioSession.sharedInstance() - do { - try session.setCategory(.playAndRecord, mode: .default, options: .defaultToSpeaker) - try session.setPreferredSampleRate(16000.0) - try session.setPreferredInputNumberOfChannels(1) - try session.setActive(true) - } catch { - completion(nil, false, "Failed to set up audio session: \(error.localizedDescription)") - return - } + if audioEngine == nil { + let session = AVAudioSession.sharedInstance() + do { + try session.setCategory(.playAndRecord, mode: .default, options: .defaultToSpeaker) + try session.setPreferredSampleRate(16000.0) + try session.setPreferredInputNumberOfChannels(1) + try session.setActive(true) + } catch { + completion(nil, false, "Failed to set up audio session: \(error.localizedDescription)") + return + } - audioEngine = AVAudioEngine() - guard let audioEngine = audioEngine else { - completion(nil, false, "Failed to create audio engine") - return - } + audioEngine = AVAudioEngine() + guard let audioEngine = audioEngine else { + completion(nil, false, "Failed to create audio engine") + return + } - let inputNode = audioEngine.inputNode - let inputFormat = inputNode.outputFormat(forBus: 0) + let inputNode = audioEngine.inputNode + let inputFormat = inputNode.outputFormat(forBus: 0) - var outputFormatDescription = AudioStreamBasicDescription( - mSampleRate: 16000.0, - mFormatID: kAudioFormatMPEG4AAC, - mFormatFlags: 2, - mBytesPerPacket: 0, - mFramesPerPacket: 1024, - mBytesPerFrame: 0, - mChannelsPerFrame: 1, - mBitsPerChannel: 0, - mReserved: 0 - ) + var outputFormatDescription = AudioStreamBasicDescription( + mSampleRate: 16000.0, + mFormatID: kAudioFormatMPEG4AAC, + mFormatFlags: 2, + mBytesPerPacket: 0, + mFramesPerPacket: 1024, + mBytesPerFrame: 0, + mChannelsPerFrame: 1, + mBitsPerChannel: 0, + mReserved: 0 + ) - guard let outputFormat = AVAudioFormat(streamDescription: &outputFormatDescription) else { - completion(nil, false, "Failed to create output audio format") - return - } + guard let outputFormat = AVAudioFormat(streamDescription: &outputFormatDescription) else { + completion(nil, false, "Failed to create output audio format") + return + } - guard let converter = AVAudioConverter(from: inputFormat, to: outputFormat) else { - completion(nil, false, "Failed to create audio converter") - return - } - self.audioConverter = converter + guard let converter = AVAudioConverter(from: inputFormat, to: outputFormat) else { + completion(nil, false, "Failed to create audio converter") + return + } + self.audioConverter = converter - self.aacBuffer = AVAudioCompressedBuffer( - format: outputFormat, - packetCapacity: 1, - maximumPacketSize: converter.maximumOutputPacketSize - ) + self.aacBuffer = AVAudioCompressedBuffer( + format: outputFormat, + packetCapacity: 1, + maximumPacketSize: converter.maximumOutputPacketSize + ) - inputNode.installTap(onBus: 0, bufferSize: 1024, format: inputFormat) { [weak self] (pcmBuffer, when) in - guard let self = self, self.isRecording else { return } - self.convert(pcmBuffer: pcmBuffer, completion: completion) - } + inputNode.installTap(onBus: 0, bufferSize: 1024, format: inputFormat) { [weak self] (pcmBuffer, when) in + guard let self = self, self.isRecording else { return } + self.convert(pcmBuffer: pcmBuffer, completion: completion) + } - do { audioEngine.prepare() + } + + do { + guard let audioEngine = audioEngine else { + completion(nil, false, "Audio engine not initialized.") + return + } try audioEngine.start() self.isRecording = true completion(nil, true, "Recording started") @@ -193,19 +200,7 @@ public class AudioRecorderManager: NSObject { return } self.isRecording = false - audioEngine?.stop() - audioEngine?.inputNode.removeTap(onBus: 0) - audioEngine = nil - audioConverter = nil - aacBuffer = nil - - do { - try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation) - } catch { - print("Failed to deactivate audio session: \(error)") - } - completion(true, "Recording stopped", "") } diff --git a/uni_modules/xhj-tencent-xp2p/utssdk/app-ios/index.uts b/uni_modules/xhj-tencent-xp2p/utssdk/app-ios/index.uts index e4cde93..2266e08 100644 --- a/uni_modules/xhj-tencent-xp2p/utssdk/app-ios/index.uts +++ b/uni_modules/xhj-tencent-xp2p/utssdk/app-ios/index.uts @@ -132,7 +132,7 @@ export const runSendServiceFunction = async function ( crypto: boolean ): Promise { try { - const result = runSendService(id, cmd, crypto) + const result = await runSendService(id, cmd, crypto) console.log('开始发送服务', result) return { code: 0, @@ -178,8 +178,8 @@ export const dataSendFunction = async function (id: string, data: Array) } try { - const result = dataSend(id, buffer, data.length) - // console.log('发送数据结果', result) + const result = await dataSend(id, buffer, data.length) + console.log('发送数据结果', result) XP2PDataHelper.deallocateBytes(buffer)