AVAudioSession never stopped

£可爱£侵袭症+ 提交于 2019-12-05 18:20:11

A part from documentation of AVAudioSession:

Discussion

If another active audio session has higher priority than yours (for example, a phone call), and neither audio session allows mixing, attempting to activate your audio session fails. Deactivating your session will fail if any associated audio objects (such as queues, converters, players, or recorders) are currently running.

My guess is that the failure to deactivate the session is the running process(es) of your queue as I highlighted in the document quote.

Probably you should make the deactivation process synchronous instead of asynchronous OR make sure that all the running actions under your queue has been processed.

Give this a try:

func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance:AVSpeechUtterance) {
        print("Speaker has finished to talk")

        queue.sync { // <---- `async` changed to `sync`
               do {
                   try self.audioSession.setActive(false, with: AVAudioSessionSetActiveOptions.notifyOthersOnDeactivation)
               }
               catch {}
           }
       }
    }

I would suggest using an AvAudioPlayer. They have very easy start and stop commands.

first declare the audio player as a variable

var SoundEffect: AVAudioPlayer!

then select the file you need

    let path = Bundle.main.path(forResource: "Untitled2.wav", ofType:nil)!
                    let url = URL(fileURLWithPath: path)
    let sound = try AVAudioPlayer(contentsOf: url)
                        SoundEffect = sound
                        sound.numberOfLoops = -1
                        sound.play()

and to stop the audio player

if SoundEffect != nil {
            SoundEffect.stop()
            SoundEffect = nil
        }

You cannot stop or deactive AudioSession, your app gets it upon launching. Documentation:

An audio session is the intermediary between your app and iOS used to configure your app’s audio behavior. Upon launch, your app automatically gets a singleton audio session.

So method -setActive: does not make your AudioSession "active", it just puts its category and mode configuration into action. For getting back to the "normal state", you could set default settings or just call setActive(false, with:.notifyOthersOnDeactivation), that will be enough.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!