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).
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.
To my knowledge I am stopping and removing all audio.
There is 1 post I found here:
iOS8 AVAudioSession setActive error
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With