我正在播放几种声音,每种声音都会使背景音频变暗.完成后,我恢复背景音频.每当其中一个音频文件播放时,背景会变暗(根据需要).当最后一个音频完成播放时,恢复背景音频(也是期望的).然而,大约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.
据我所知,我正在停止并删除所有音频.
我在这里找到1篇帖子:
iOS8 AVAudioSession setActive error
但解决方案对我不起作用.这是我的音频播放器课程.如果你可以告诉我什么可能,我会很感激.
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") } }
重要更新
所以我发现了我认为是问题的粗略原因.我们的应用程序基于Cordova构建.所以我们有很多Safari(浏览器)调用.每当我们播放视频(通过Safari播放)时,就会发生此错误.似乎Safari以某种方式调暗音频并保持运行的I / O线程.