AVAudioSession 错误:停用正在运行 I/O 的音频会话

问题描述 投票:0回答:4
2017-02-24 14:56:44.280 PropertyManager[10172:5336578] 14:56:44.280 ERROR:    [0x1a0a24000] AVAudioSession.mm:692: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

2017-02-24 14:56:44.281 PropertyManager[10172:5336578] error === Error Domain=NSOSStatusErrorDomain Code=560030580 "(null)"
PropertyManager was compiled with optimization - stepping may behave oddly; variables may not be available.
ios avaudiosession
4个回答
20
投票

您的错误日志非常简洁地自我表达:

Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session

它告诉您问题以及解决方案。

现在你正在做这样的事情:

[[AVAudioSession sharedInstance] setActive:NO
               withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
                                             error:nil];

但是,您应该首先停止音频播放器实例,然后将激活状态设置为“是”或“否”。

[yourAudioPlayer stop];
[[AVAudioSession sharedInstance] setActive:NO
                   withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
                                                 error:nil];

请参阅 Apple 文档以查看 enum AudioSessionSetActiveOption 的值。

另请参阅:有关 setActive:withOptions 方法的 Apple 文档

至于你的第二个错误

PropertyManager was compiled with optimization - stepping may behave oddly; variables may not be available.

请参阅这个出色的答案


1
投票

@NSNoob 100%正确。播放器(或其他东西)仍然处于活动状态。

更多来自dani-mp。他说:

我想说暂停播放器不是同步操作,所以我们 在知道玩家之前不应该停用会话 已暂停(请参阅此线程中的响应)。

这个问题的解决方案可能是我们听取更改 timeControlStatus 并在播放器完成后停用音频会话 真的暂停了。

线程中的答案说

此错误表明您的应用程序中的某些内容仍在使用音频 调用 AVAudioSession setActive:NO 时的 I/O。 如果没有更多信息就不可能说出哪个物体,但是 有时这是调用异步停止方法的结果 某种玩家并且不等待通知 播放器在停用音频会话之前已停止。


0
投票

在使录音会话为假之前让音频播放器停止..如果你想让音频播放器为零..删除该行..它对我有用..


0
投票

我开发了一个解决方案,其中使用 AVAudioEngine、AVAudioPlayerNode 并使用 QUEUE 的概念。正如您提到的问题是在音频会话中断时发生的。

这是我的实现:

func playAudio(soundFile: String, alertTextmessage: String) {
    
    if let waveFileURL = Bundle.main.url(forResource: soundFile, withExtension: "wav") {
        do {
            // Create an AVAudioFile from the wave file URL
            let audioFile = try AVAudioFile(forReading: waveFileURL)
            
            // Calculate the duration of the audio file based on its length and format
            let audioFormat = audioFile.processingFormat
            let audioFrameCount = UInt64(audioFile.length)
            let audioDuration = Double(audioFrameCount) / audioFormat.sampleRate
            
            // Create an AVAudioPlayerNode and attach it to the audio engine
            let playerNode = AVAudioPlayerNode()
            audioEngine.attach(playerNode)
            
            // Connect the player node to the main mixer node
            audioEngine.connect(playerNode, to: audioEngine.mainMixerNode, format: audioFormat)
            
            // Schedule the audio file for playback on the player node
            playerNode.scheduleFile(audioFile, at: nil)
            
            // Start the audio engine
            try audioEngine.start()
            
            // Start playback of the wave file
            playerNode.play()
            
            // After the wave file finishes playing, speak the text
            DispatchQueue.main.asyncAfter(deadline: .now() + audioDuration) {
                self.speakPhrase(phrase: alertTextmessage)
            }
        } catch {
            print("Error playing wave file: \(error.localizedDescription)")
        }
    } else {
        print("Wav file not found.")
    }
}

这里我正在播放以语音文本音频开头的 wav 文件。 您可以在我的 Github 上访问完整的文件和使用 https://github.com/iAmita/MultipleAudioPlay

© www.soinside.com 2019 - 2024. All rights reserved.