gpt4 book ai didi

ios - AVAudioSession中断和路由变化

转载 作者:可可西里 更新时间:2023-11-01 06:13:23 25 4
gpt4 key购买 nike

我正在开发一款包含在后台运行的音频多轨播放器的游戏。在处理暂停所有轨道然后在按下暂停按钮时恢复时,我发现没有太大的困难:

class AudioMultiTrack {

var tracks : [AudioTrack]
var mixer : AVAudioMixerNode
var pauseTime : AVAudioTime?

var engine = AVAudioEngine()
var volumes = [Float]()

init(audioTracks: [AudioTrack]){
tracks = audioTracks
mixer = engine.mainMixerNode
}

func initialize() {
for track in tracks {
track.setUpTrack()
engine.attach(track.audioPlayer)
engine.connect(track.audioPlayer, to: mixer, format: track.format)
track.scheduleFile()
}

do {
try engine.start()
} catch {
fatalError("error starting engine")
}
}

func play() {
for track in tracks {
track.audioPlayer.play(at: startTime())
}
}

func pause() {
pauseTime = tracks[0].audioPlayer.lastRenderTime

for track in tracks {
track.audioPlayer.pause()
}

}

func unPause() {

for track in tracks {
track.scheduleFile()
track.audioPlayer.play(at: pauseTime)
}
}

func stop() {
pauseTime = tracks[0].audioPlayer.lastRenderTime
engine.stop()

for track in tracks {
engine.detach(track.audioPlayer)
}

engine.reset()
}

func startTime() ->AVAudioTime {
let sampleRate = tracks[0].format.sampleRate
let sampleTime = AVAudioFramePosition(sampleRate)

return AVAudioTime(sampleTime: sampleTime, atRate: sampleRate)
}

在 GameViewController 中按下暂停键时:

func pausePressed() {
audio.pause()
}

func continuePressed() {
audio.unPause()
}

但是,当以这种方式处理中断或路线变更时:

    func handleInterruption(notification: NSNotification) {
guard let info = notification.userInfo else { return }

switch info[AVAudioSessionInterruptionTypeKey] as! Int {
case 0:
audio.unPause()
case 1:
audio.pause()
default:
break;
}
}

程序崩溃,我收到:

*** 由于未捕获的异常“com.apple.coreaudio.avfaudio”而终止应用程序,原因:“所需条件为假:_engine->IsRunning()”

所以看起来音频引擎正在停止,我不想重新初始化音频播放器,原因有很多,包括设置时间和游戏的连续性...

我对问题的评估是否正确?如果是这样,我怎样才能让音频引擎在中断或路由更改时保持运行?

最佳答案

我认为这是不可能的,因为你为每个声音创建了一个音频播放器,它需要准备、设置硬件等。当声音完成后,该设置将被删除并需要重新完成。 IE。保持音频引擎“运行”可能是不可能的。

关于ios - AVAudioSession中断和路由变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42236753/

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