在AKPlayer.buffer上播放

时间:2018-09-05 14:44:00

标签: ios swift audio audiokit

您好,我正在使用Audiokit AKNodeRecorder录制声音,然后将这些数据传输到播放器,对其进行一些处理,现在尝试再次播放。但是我无法使其正常工作。

我尝试通过AKPlayer播放缓冲区,或创建没有骰子的新AKAudioFile。

这是我目前正在使用的代码:

tape = recorder.audioFile!
player.load(audioFile: tape)
player.buffering = .always
recorder.stop()
var globalAudioFile: AKAudioFile?

if let avAudioPCMBuffer = player.buffer {

    //do processing on avAudioPCMBuffer here

    do {
        globalAudioFile = try AKAudioFile()
    } catch let error as NSError {
        print("Recorder Error: Unable to open AudioFile: \(error.localizedDescription)")
    }

    do {
        globalAudioFile = try AKAudioFile(forWriting: (globalAudioFile?.url)!,
                                          settings: (globalAudioFile?.processingFormat.settings)!)
    } catch let error as NSError {
        print("Recorder Error: cannot write to \(globalAudioFile?.fileNamePlusExtension)")
        print(error.localizedDescription)
    }

    do {
        try globalAudioFile?.write(from: avAudioPCMBuffer)
    } catch let error as NSError {
        print("Recoder Error: Unable to write buffer to AudioFile: \(error.localizedDescription)")
    }

    if let audioFile = globalAudioFile {
        let bufferedPlayer = audioFile.player
        bufferedPlayer.play()
    }
}

//later when I wish to play:

if let audioFile = globalAudioFile {
    let bufferedPlayer = audioFile.player
    bufferedPlayer.play()
}

我尝试玩游戏时会遇到异常。

如果有人要测试驱动器,我在这里创建了一个示例项目:https://gitlab.com/Donwangugi/AVAudioPCMBufferExample

我非常感谢您的帮助:)

0 个答案:

没有答案