ios - 使用Multipeer Connectivity(Swift 2)在目标iPhone中从发送者部分接收了音频文件

标签 ios audio-streaming multipeer-connectivity nsinputstream nsoutputstream

我已经打开了发送方iPhone的输出流,并且还在接收方iPhone中实现了输入(接收)流。我能够使用Multipeer Connectivity在同一Wi-Fi网络上连接两个设备并发送数据(我将音频文件转换为NSData格式,以通过Outputstream发送)。但是在接收时,仅发送了一部分NSData每次我重复运行代码时,都会收到不同数量的数据。
这是输出流代码:

     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()

我们将非常感谢您提供有关部分接收数据以及随后更改代码以制定解决方案的原因的任何帮助。

最佳答案

NSOutputStream的文档指示返回的代码为
write(_:maxLength :)应使用以下命令进行检查:

let result = outputStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)

返回值:
实际写入的字节数;如果发生错误,则为-1。可以通过streamError获得有关该错误的更多信息。如果接收器是固定长度的流并且已达到其容量,则返回0。

我建议您检查此返回码,并按片获取数据。

关于ios - 使用Multipeer Connectivity(Swift 2)在目标iPhone中从发送者部分接收了音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35722134/

相关文章:

ios - 在一个 UITableView 中添加两个标题 View ?

iphone - 与 insertSubView 的索引值混淆

iOS Alamofire 停止所有请求

FFMPEG 音量命令参数限制

ios - 使用 Apple 音频文件流服务、iOS 7 无法解析 M4A、AAC 文件

iOS - 无法将图像对象添加到 NSMutableArray

python - 拆分/加入 mp3 时 pydub 音频故障

ios - 如何获取媒体播放器的时间位置

ios - 接受邀请多点连接

ios - 检测附近的用户并通过推送通知 iOS 进行通知