使用 AVAudioEngine 从 AVAudioPCMBuffer 播放音频

Play audio from AVAudioPCMBuffer with AVAudioEngine

我有两个 class,MicrophoneHandlerAudioPlayer。我已经设法使用 AVCaptureSession 使用已批准的答案 来窃听麦克风数据,并使用此功能将 CMSampleBuffer 转换为 NSData

func sendDataToDelegate(buffer: CMSampleBuffer!)
{
    let block = CMSampleBufferGetDataBuffer(buffer)
    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)

    self.delegate.handleBuffer(result)
}

我现在想通过将上面生成的 NSData 转换为 AVAudioPCMBuffer 并使用 AVAudioEngine 播放它来通过扬声器播放音频。我的AudioPlayerclass如下:

var engine: AVAudioEngine!
var playerNode: AVAudioPlayerNode!
var mixer: AVAudioMixerNode!

override init()
{
    super.init()

    self.setup()
    self.start()
}

func handleBuffer(data: NSData)
{
    let newBuffer = self.toPCMBuffer(data)
    print(newBuffer)

    self.playerNode.scheduleBuffer(newBuffer, completionHandler: nil)
}

func setup()
{
    self.engine = AVAudioEngine()
    self.playerNode = AVAudioPlayerNode()

    self.engine.attachNode(self.playerNode)
    self.mixer = engine.mainMixerNode

    engine.connect(self.playerNode, to: self.mixer, format: self.mixer.outputFormatForBus(0))
}

func start()
{
    do {
        try self.engine.start()
    }
    catch {
        print("error couldn't start engine")
    }

    self.playerNode.play()
}

func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer
{
    let audioFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatFloat32, sampleRate: 8000, channels: 2, interleaved: false)  // given NSData audio format
    let PCMBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: UInt32(data.length) / audioFormat.streamDescription.memory.mBytesPerFrame)

    PCMBuffer.frameLength = PCMBuffer.frameCapacity

    let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: Int(PCMBuffer.format.channelCount))

    data.getBytes(UnsafeMutablePointer<Void>(channels[0]) , length: data.length)

    return PCMBuffer
}

当在上面的第一个代码段中调用 self.delegate.handleBuffer(result) 时,缓冲区到达 handleBuffer:buffer 函数。

我能够 print(newBuffer),并查看转换后缓冲区的内存位置,但扬声器没有任何声音。我只能想象与 NSData 之间的转换不一致。有任何想法吗?提前致谢。

跳过原始 NSData 格式

为什么不一路使用AVAudioPlayer呢?如果您确实需要 NSData,您可以随时从下面的 soundURL 加载此类数据。在此示例中,磁盘缓冲区类似于:

let soundURL = documentDirectory.URLByAppendingPathComponent("sound.m4a")

为了优化内存和资源管理,直接记录到文件是有意义的。您可以通过这种方式从录音中获得 NSData

let data = NSFileManager.defaultManager().contentsAtPath(soundURL.path())

下面的代码就是您所需要的:

记录

if !audioRecorder.recording {
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setActive(true)
        audioRecorder.record()
    } catch {}
}

播放

if (!audioRecorder.recording){
    do {
        try audioPlayer = AVAudioPlayer(contentsOfURL: audioRecorder.url)
        audioPlayer.play()
    } catch {}
}

设置

let audioSession = AVAudioSession.sharedInstance()
do {
    try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
    try audioRecorder = AVAudioRecorder(URL: self.directoryURL()!,
        settings: recordSettings)
    audioRecorder.prepareToRecord()
} catch {}

设置

let recordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)),
    AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)),
    AVNumberOfChannelsKey : NSNumber(int: 1),
    AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue))]

下载Xcode项目:

你可以找到这个例子 here. Download the full project, which records and plays on both simulator and device, from Swift Recipes