我通过TCP / IP连接到服务器并接收[UInt8]。我知道这是音频。如何在iPhone上播放流?
@IBAction func connectAndListen(sender: AnyObject) {
var client:TCPClient = TCPClient(addr: "80.233.248.96", port: 6969)
var (success,errmsg)=client.connect(timeout: 1)
if success{
while (success){
var (success,errmsg)=client.send(str:"GET / HTTP/1.0\n\n" )
if success{
var data = client.read(1024*10)
if let d = data{
var endMarker = NSMutableData(bytes: d, length: d.count)
println(endMarker)
self.audioPlayer = AVAudioPlayer(data: endMarker, error: nil)
self.audioPlayer?.prepareToPlay()
self.audioPlayer?.play()
}
}else{
println(errmsg)
break
}
}
}else{
println(errmsg)
}
}
我的崩溃:
致命错误:在解包可选值时意外发现nil
on this self.audioPlayer!.prepareToPlay()Print screen my crash
答案 0 :(得分:1)
NSData
对象。AVAudioPlayer
方法创建initWithData:error:
。AVAudioPlayer
播放方法播放音频。我无法确切地告诉您如何创建NSData
对象,因为我不知道如何获取字节流,但Apple文档中详细记录了NSData
类。