I am trying to send and receive an audio data(saved by .caf extension device) using Network.framework.
This is my plan.
- convert file into Data type using below code:
guard let data = try? Data(contentsOf: recordedDocumentURL) else {
print("recorded file to data conversion failed in touchUpCallButton Method")
return
}
- send Data type data using NWConnection.send, UDP.
- receive Data type data using NWConnection.receiveMessage
- convert received Data type data into AVAudioFile type data and play it using AVAudioEngine
Right now my problem is that size of data converted from audio file is too big to fit in maximumDatagramSize to send.
So in my understanding, I need to split Data type into many small bytes of data and send it one by one.
But in this case, I need to collect received data to make complete audio file again, so received device can play complete audio file.
And.. I'm stuck at this step.
I can't find right solution to divide Data type into small pieces to send by datagram using UDP.
What I have in my mind is to use 'subdata(in: Range<Data.Index>) -> Data' function and 'append(Data)' function to divide and sum up data.
Is this right approach to solve my problem?
Little advice would be very appreciated!