我正在通过UDP套接字发送nsdata,并想在那儿播放,那么我该如何播放呢?我是否发送了正确的数据进行播放?
class ViewController: UIViewController,AVCaptureAudioDataOutputSampleBufferDelegate {
// Global Scope of Session
let session = AVCaptureSession()
@IBOutlet weak var recBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func recBtnAction(_ sender: Any) {
session.sessionPreset = .medium
let mic = AVCaptureDevice.default(for: .audio)
var mic_input: AVCaptureDeviceInput!
let audio_output = AVCaptureAudioDataOutput()
audio_output.setSampleBufferDelegate(self, queue: DispatchQueue.main)
do
{
if let temp = mic {
mic_input = try AVCaptureDeviceInput(device: temp)
}
}
catch
{
return
}
if session.inputs.isEmpty {
session.addInput(mic_input)
session.addOutput(audio_output)
}
session.startRunning()
}
@IBAction func StopBtnAction(_ sender: Any){
session.stopRunning()
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
let block = CMSampleBufferGetDataBuffer(sampleBuffer)
var length = 0
var data: UnsafeMutablePointer<Int8>? = nil
var status = CMBlockBufferGetDataPointer(block!, 0, nil, &length, &data) // TODO: check for errors
let result = NSData(bytesNoCopy: data!, length: length, freeWhenDone: false)
print("NSDATA")
print(result)
}
}
上面是我用来收听麦克风数据并转换为nsdata并发送但我无法播放nsdata的代码,所以通过套接字发送麦克风音频数据的这种正确方法是吗?