1
從發件人部分接收目標iPhone中的音頻文件我已經從發件人iPhone打開了一個輸出流,並在接收iPhone中實現了一個輸入(接收)流。我能夠使用Multipeer Connectivity在同一Wi-Fi網絡上連接兩臺設備,併發送數據(我將音頻文件轉換爲NSData格式以通過Outputstream發送)。但是,在接收時,只有部分我發送的NSData每次我重複運行代碼時,都會收到不同數量的數據。 這裏是輸出流代碼:使用Multipeer Connectivity(Swift 2)
let outputStream: NSOutputStream = try! session.startStreamWithName(name, toPeer: session.connectedPeers[0])
print("stream created")
outputStream.delegate = self
outputStream.scheduleInRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream.open()
print("Before filewritten")
outputStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)
print("filewritten")
outputStream.close()
我的輸入流的代碼是:
var bytesRead = 0
var buffer = [UInt8](count: 15000000, repeatedValue: 0)
NSLog("%@", "didReceiveStream")
stream.delegate = self
stream.scheduleInRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
stream.open()
while (stream.hasBytesAvailable){
bytesRead = stream.read(&buffer, maxLength: buffer.count)
print("data fetched"+"\(bytesRead)")
}
stream.close()
關於這部分的接收數據和代碼的後續變化的原因,制定將解決任何幫助非常感謝。