在 AKPlayer 中播放后尝试录制时 AudioKit 崩溃

AudioKit crashes when try record after playing in AKPlayer

这是我遇到的问题。我有一个应用程序可以从网络加载声音然后播放。同时,该应用程序具有录制和播放语音的功能。我为这两个功能使用了两个不同的视图控制器。对于这些任务,我创建了一个单例。

当我启动应用程序并首先直接转到 Record View Controller 时 - 一切正常(我可以录制声音,播放它,然后我可以使用我的声音转到我的第二个 View Controller 并可以使用也没有问题。

但是,如果我启动应用程序并首先播放声音(使用我的第二个视图控制器),然后转到记录视图控制器并尝试录制一些内容,我会收到此崩溃消息:Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: '[[busArray objectAtIndexedSubscript:(NSUInteger)element] setFormat:format error:&nsErr]: returned false, error Error Domain=NSOSStatusErrorDomain Code=-10865 "(null)"'

当我录制 recordButton 时发生此崩溃。它在线发生 - 试试 recorder.record():

func startRecord() {
    if AKSettings.headPhonesPlugged {
        micBooster.gain = 1
    }
    micBooster.gain = 0
    do {
        try recorder.record()
    } catch {
        print("Can't record because: \(error)")
    }
}

如果我设置类别 .playAndRecord

,就会发生此崩溃
try audioSession.setCategory(.playAndRecord, mode: .default, options: .mixWithOthers)

我的单身狗class:

class AudioKitSingleton {
var mic: AKMicrophone!
var micMixer: AKMixer!
var recorder: AKNodeRecorder!
var tape: AKAudioFile!
var player: AKPlayer!
var micBooster: AKBooster!
var mainMixer: AKMixer!
var url: URL?
var name: String?
var artist: String?
var uploader: String?
var performer: String?
var load: Bool = false
var isPlay: Bool = false
var categoryID: Int? = nil

static let shared = AudioKitSingleton()

func initMic() {
    stopAudioKitEngine()
    AKAudioFile.cleanTempDirectory()
    AKSettings.bufferLength = .medium
    AKSettings.defaultToSpeaker = true
    setupRecordSession()
    mic = AKMicrophone()
    micMixer = AKMixer(mic)
    micBooster = AKBooster(micMixer)
    micBooster.gain = 0
    recorder = try? AKNodeRecorder(node: micMixer)
    if let file = recorder.audioFile {
        player = AKPlayer(audioFile: file)
    }
    player.isLooping = false
    mainMixer = AKMixer(player, micBooster)
    AudioKit.output = mainMixer
    startAudioKitEngine()
}

func deinitMic() {
    stopAudioKitEngine()
    mic = nil
    micMixer = nil
    recorder = nil
    tape = nil
    player = nil
    micBooster = nil
    mainMixer = nil
}

func startRecord() {
    if AKSettings.headPhonesPlugged {
        micBooster.gain = 1
    }
    micBooster.gain = 0
    do {
        try recorder.record()
    } catch {
        print("Can't record because: \(error)")
    }
}

func stopRecord() {
    micBooster.gain = 0
    tape = recorder.audioFile!
    player.load(audioFile: tape)
    player.prepare()
    if let _ = player.audioFile?.duration {
        recorder.stop()
    }
}

func resetRecorder() {
    do {
        try recorder.reset()
    } catch {
        print("Can't reset recorder because: \(error)")
    }
}

func playerPlayRecord() {
    player.play()
}

func playerStopRecord() {
    player.stop()
}

func setupRecordSession() {
    do {
        try audioSession.setCategory(.record, mode: .default, options: .mixWithOthers)
    } catch {
        print(error)
    }
}

func startAudioKitEngine() {
    do {
        try AudioKit.start()
    } catch {
        AKLog("AudioKit did not start because: \(error)")
    }
}

func stopAudioKitEngine() {
    if AudioKit.engine.isRunning {
        do {
            try AudioKit.stop()
        } catch {
            AKLog("AudioKit did not start because: \(error)")
        }
    }
}

func setupPlayer(url: URL) {
    if AudioKit.engine.isRunning {
    stopAudioKitEngine()
    }
    player = try AKPlayer(url: url)
    AudioKit.output = player
    startAudioKitEngine()
}

func setupPlayer(mixloop: AVAudioFile) {
    if AudioKit.engine.isRunning {
    stopAudioKitEngine()
    }
    player = try AKPlayer(audioFile: mixloop)
    AudioKit.output = player
    startAudioKitEngine()
}

func play() {
    try player?.play()
}

func resume() {
    try player?.resume()
}

func stop() {
    player?.stop()
}

func pause() {
    player?.pause()
}

func remove() {
    if AudioKit.engine.isRunning {
        try? AudioKit.stop()
        player?.stop()
        player = nil
        recorder = nil
        AudioKit.output = nil
        url = nil
        name = nil
        artist = nil
        uploader = nil
        load = false
        isPlay = false
    }
}

}

此崩溃发生在所有 iPhone 秒,除了 iPhone 5 秒,iOS12。需要帮助。

我很确定这会在今天的 AudioKit 4.5.2 版本中得到解决,现在正在上传。