Swift - 在具有多重连接的设备之间传输音频

时间:2015-10-11 15:48:37

标签: swift streaming audio-streaming multipeer-connectivity

经过长时间搜索google和stackoverflow后,我不得不辞职......

我在Swift上开发我的应用程序。

我想将音频,在智能手机(主人)上播放到另一个(从属)。

关于目标C有一个非常好的教程,但在Swift上找到相同的内容非常复杂。

我遵循本教程:http://www.ralfebert.de/tutorials/ios-swift-multipeer-connectivity/ 1

每台设备之间的连接正常。但现在,我想编写流媒体歌曲的代码。你会在下面找到我所做的。但是当我测试时,主设备上没有播放歌曲。很奇怪没有?

   func sendColor(colorName : String) {
        NSLog("%@", "sendColor: \(colorName)")

    if session.connectedPeers.count > 0 {
        var error : NSError?
        do {
            try self.session.sendData(colorName.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, toPeers: session.connectedPeers, withMode: MCSessionSendDataMode.Reliable)

            //envoie son

            let sineURL = NSBundle.mainBundle().URLForResource("tiesto", withExtension: "mp3")!
            let asset = AVAsset(URL: sineURL)
            var assetReader:AVAssetReader


            do{
                assetReader = try AVAssetReader(asset: asset)
            }catch{
                fatalError("Unable to read Asset: \(error) : \(__FUNCTION__).")
            }

            let track = asset.tracksWithMediaType(AVMediaTypeAudio).first
            let outputSettings: [String:Int] =
            [ AVFormatIDKey: Int(kAudioFormatLinearPCM),
                AVLinearPCMIsBigEndianKey: 0,
                AVLinearPCMIsFloatKey: 0,
                AVLinearPCMBitDepthKey: 16,
                AVLinearPCMIsNonInterleaved: 0]

            let trackOutput = AVAssetReaderTrackOutput(track: track!, outputSettings: outputSettings)

            assetReader.addOutput(trackOutput)
            assetReader.startReading()



            let sampleData = NSMutableData()

            while assetReader.status == AVAssetReaderStatus.Reading {
                  print("la aussi ")
                if let sampleBufferRef = trackOutput.copyNextSampleBuffer() {
                    if let blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef) {
                        let bufferLength = CMBlockBufferGetDataLength(blockBufferRef)
                        let data = NSMutableData(length: bufferLength)
                        CMBlockBufferCopyDataBytes(blockBufferRef, 0, bufferLength, data!.mutableBytes)
                        let samples = UnsafeMutablePointer<Int16>(data!.mutableBytes)
                        sampleData.appendBytes(samples, length: bufferLength)
                        CMSampleBufferInvalidate(sampleBufferRef)
                    }
                }
            }


            if assetReader.status == AVAssetReaderStatus.Completed {
                print("ok ")


             }



        } catch var error1 as NSError {
            error = error1
            NSLog("%@", "\(error)")
        }
    }

}

要获取流,我必须在以下部分编写一些代码,但我不知道如何执行该操作。任何帮助将不胜感激。

func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) {

    }

0 个答案:

没有答案