xcode AVAudioSession 音频调光错误:停用正在运行 I/O 的音频会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31866639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AVAudioSession Audio Dimming Error: Deactivating an audio session that has running I/O
提问by Aggressor
I am playing several sounds, which each dim the background audio. When they are done, I restore background audio. What happens is every time one of the audio files play, the background dims (as desired). When the last audio finishes playing the background audio is restored (also desired). However after about 5 seconds, it throws this error and dims the audio again (not what I want since all sounds are now finished).
我正在播放几种声音,每个声音都会使背景音频变暗。完成后,我恢复背景音频。每次播放其中一个音频文件时,背景都会变暗(根据需要)。当最后一个音频播放完毕后,背景音频将恢复(也需要)。然而,大约 5 秒后,它会抛出此错误并再次使音频变暗(不是我想要的,因为所有声音现在都已完成)。
ERROR: [0x19c9af310] AVAudioSession.mm:646: -[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.
错误:[0x19c9af310] AVAudioSession.mm:646:-[AVAudioSession setActive:withOptions:error:]:停用正在运行 I/O 的音频会话。在停用音频会话之前,应停止或暂停所有 I/O。
To my knowledge I am stopping and removing all audio.
据我所知,我正在停止并删除所有音频。
There is 1 post I found here:
我在这里找到了 1 个帖子:
iOS8 AVAudioSession setActive error
iOS8 AVAudioSession setActive 错误
But the solution does not work for me. Here is my audio player class. If you can advise what might be up I'd appreciate it.
但该解决方案对我不起作用。这是我的音频播放器类。如果你能告诉我可能会发生什么,我会很感激。
import Foundation
import AVFoundation
private var _singleton:O_Audio? = O_Audio()
private var _avAudioPlayers:Array<AVAudioPlayer> = []
//Manages dimming and resuming background audio
class O_Audio:NSObject, AVAudioPlayerDelegate
{
class var SINGLETON:O_Audio
{
if(_singleton == nil)
{
_singleton = O_Audio()
}
return _singleton!
}
class func dimBackgroundAudio()
{
AVAudioSession.sharedInstance().setActive(true, error: nil)
}
class func restoreBackgroundAudio()
{
AVAudioSession.sharedInstance().setActive(false, error: nil)
}
class func playSound(path:String)
{
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(path, ofType: "m4a")!)
var audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
_avAudioPlayers.append(audioPlayer)
audioPlayer.delegate = O_Audio.SINGLETON
audioPlayer.prepareToPlay()
audioPlayer.play()
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool)
{
//this was from the 1 stack post I found but these
//two lines do not solve my problem
player.stop()
player.prepareToPlay()
var index:Int!
for i in 0..._avAudioPlayers.count - 1
{
if(_avAudioPlayers[i] == player)
{
index = i
break
}
}
_avAudioPlayers.removeAtIndex(index)
if(_avAudioPlayers.count == 0)
{
O_Audio.restoreBackgroundAudio()
}
}
func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer!, error: NSError!)
{
println("error")
}
}
Important Update
重要更新
So I've found what I think is a rough cause of the issue. Our App is built on Cordova. So we have a lot of Safari (browser) calls. And this bug is occurring whenever we play a video (which is played via Safari). It seems like Safari is somehow dimming the audio and keeping a running I/O thread.
所以我找到了我认为是问题的粗略原因。我们的应用程序建立在 Cordova 之上。所以我们有很多 Safari(浏览器)调用。每当我们播放视频(通过 Safari 播放)时,就会发生此错误。似乎 Safari 以某种方式使音频变暗并保持运行的 I/O 线程。
采纳答案by Aggressor
The issue is the fact that an MPMoviePlayerController object is playing. In fact Any AVPlayerItem causes this. If you play a movie, and try to dim audio you will get this error.
问题是 MPMoviePlayerController 对象正在播放。事实上,任何 AVPlayerItem 都会导致这种情况。如果您播放电影并尝试调暗音频,则会出现此错误。
At present, any movie played on iOS has an un-mutable audio track (even if there is no audio on the movie file). That permanently causes the duck issue (its a bug in the source code). I tried many workarounds and nothing worked. I am certain this is an Xcode source bug.
目前,任何在 iOS 上播放的电影都有一个不可改变的音轨(即使电影文件上没有音频)。这会永久导致鸭子问题(它是源代码中的一个错误)。我尝试了很多解决方法,但没有任何效果。我确定这是一个 Xcode 源错误。
回答by saurabh2810
@Aggressor: you can change the audio category to multi route so that the audio plays through speaker and headphones(if plugged in) at the same time.
@Aggressor:您可以将音频类别更改为多路由,以便同时通过扬声器和耳机(如果已插入)播放音频。
By this way you won't get the dimmed audio.
通过这种方式,您将不会获得变暗的音频。
回答by user501836
I have the same issue. You must call AVAudioSession.sharedInstance().setActive(true, error: nil); before AVAudioSession.sharedInstance().setActive(false, error: nil);
我有同样的问题。你必须调用 AVAudioSession.sharedInstance().setActive(true, error: nil); 之前 AVAudioSession.sharedInstance().setActive(false, error: nil);
And they are appear in pairs.
而且它们是成对出现的。