last day (28 days later) » 

9:46 PM
0
A: Swift: how to resume audio after interruption?

Leo DabusXcode 7.3.1 • Swift 2.3.1 let audioSession = AVAudioSession.sharedInstance() do { try audioSession.setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers) print("AVAudioSession Category Playback OK") do { try audioSession.setActive(true) print("AVAudi...

 
Thx. I have replaced the interruption handler with the new one here above AND commented out the .MixWithOthers code, but audio would not resume despite these showing in console: AVAudioSession Category Playback OK AVAudioSession is Active handleInterruption began handleInterruption ended. My iphone is running 9.3.3 (13G34)
 
@rockhammer I have update the answer with .MixWithOthers as you already notice it throws an error when you try to set the session active again so .MixWithOthers is definetly needed there to resume playing.
@rockhammer I have created a small sample with the new code, added routeChanged and I thought you might be interested dropbox.com/s/ksjvfba2cz05msp/AVAudioPlayer.zip?dl=1
 
thx for continued efforts. Apologies could not get back to it until now. Before trying your solution in dropbox, I have not yet found a way to make play resume after alarm WITHOUT .MixWithOthers. In fact, this is the only way I have found to make it work gist.github.com/plaudev/692a2dfa44ab66fe155dcf4d24947f13. I have tried 4 other variants that all fail (please see Gist in next comment). All these Gists are versions of ViewController.swift; just substitute into github provided earlier
 
@rockhammer no worries the sample i posted works flawlessly using MixWithOthers. I couldn't re activate the session without that option either
Have you tried the sample i posted?
 
@Leo Dabus, thx for the code. You write neat code & like you said it works smoothly
I will incorporate routeChange into my code. Thanks for pointing that out. Learning lots new everyday
 
9:49 PM
You are welcome. I also learn a lot answering. I will post the Swift 3 version also later
 
In your callbacks / event handlers you put the code to update button label in dispatch_async(). I suppose that is not to hold up the main queue for non-critical things? And what are those two test files? They look like auto generated templates
 
It is needed as the alarm starts it gets the main queue so you need to update the UI from the main queue otherwise you might get a delay or even an error in this case
Test files are generated if you choose it when creating your project.
 
thx for all your efforts. Have a good rest of weekend. I'm sure we will run into each other on SO again
 
Dont forfet to vote up the other so question
The mixWithOther
 

  last day (28 days later) »