Hi, I'm trying to build a Walki-talkie app using Swift.
My Idea is...
- record user's voice by AVAudioEngine in device1
- convert recorded file into Data type data
- send data from device1 to device2 using NWConnection.send
- receive data using NWConnection.receiveMessage
- play received data in device2
I am implementing this app using P2P option in Network.framework, so each device has both browser and listener.
And I have to make each device to keep receiving incoming data, and to send recorded voices.
At first I thought that if receiveMessage method was executed, it would wait for other device's send method to send data and receive it.
But while debugging, program didn't stopped at receiveMessage method, it just went through and executed next line.
I must be missing something, but I'm not sure what it is.
Below is send and receive part of code I tried.
func sendRecordedAudio(data: Data) {
guard let connection = connection else {
print("connection optional unwrap failed: sendRecordedAudio")
return
}
connection.send(content: data, completion: .contentProcessed({ (error) in
if let error = error {
print("Send error: \(error)")
}
}))
}
func receiveRecordedAudio() {
guard let connection = connection else {
print("connection optional unwrap failed: receiveRecordedAudio")
return
}
connection.receiveMessage{ (data, context, isComplete, error) in
if let error = error {
print("\(error) occurred in receiveRecordedAudio")
}
if let data = data {
self.delegate?.receivedAudio(data: data)
}
}
}
App is calling sendRecordAudio when recording audio is ended, and calling receiveRecordeAudio when user pressed receive button.
Any help would be greatly appreciated!