gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:54:14 37 4
gpt4 key购买 nike

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

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

但是,如果我启动应用程序并首先播放声音(使用我的第二个 View Controller ),然后转到记录 View Controller 并尝试记录一些东西,我会收到此崩溃消息:Terminating app due to未捕获的异常'com.apple.coreaudio.avfaudio',原因:'[[busArray objectAtIndexedSubscript:(NSUInteger)element] setFormat:format error:&nsErr]: returned false, 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 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 5s、iOS12 以外的所有 iPhone 上。需要帮助。

最佳答案

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

关于swift - 在 AKPlayer 中播放后尝试录制时 AudioKit 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52730057/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com