Reputation: 395
I am learning Swift as my first programming language.
I've struggled for many hours to resume background audio playback after interruption (eg call)
What should happen:
Really appreciate any help! Thanks
Notes:
Code:
import UIKit
import AVFoundation
var player: AVQueuePlayer!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true, withOptions: .NotifyOthersOnDeactivation)
} catch { }
let songNames = ["music"]
let songs = songNames.map { AVPlayerItem(URL:
NSBundle.mainBundle().URLForResource($0, withExtension: "mp3")!) }
player = AVQueuePlayer(items: songs)
let theSession = AVAudioSession.sharedInstance()
NSNotificationCenter.defaultCenter().addObserver(self,
selector:"playInterrupt:",
name:AVAudioSessionInterruptionNotification,
object: theSession)
player.play()
}
func playInterrupt(notification: NSNotification) {
if notification.name == AVAudioSessionInterruptionNotification
&& notification.userInfo != nil {
var info = notification.userInfo!
var intValue: UInt = 0
(info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)
if let type = AVAudioSessionInterruptionType(rawValue: intValue) {
switch type {
case .Began:
print("aaaaarrrrgggg you stole me")
player.pause()
case .Ended:
let timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "resumeNow:", userInfo: nil, repeats: false)
}
}
}
}
func resumeNow(timer : NSTimer) {
player.play()
print("attempted restart")
}
}
Upvotes: 12
Views: 8542
Reputation: 443
Simon, absolutely confirm it on my end. I spend 2 days looking for it! If you use just:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
then does not matter what you do your player will not resume playing audio if you use instead:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
then it works like perfect and will resume after receiving phone call while the app is in the background.
Thanks!
Upvotes: -1
Reputation: 3401
For me it was adding a delay that fixed the problem:
@objc func handleInterruption(_ notification: Notification) {
// audio interuption - restart audio player for any type (began or ended) to make sure it keeps going
guard let type = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt,
let interruption = AVAudioSession.InterruptionType(rawValue: type) else {
NSLog("*** Incorrect notification format")
return
}
if interruption == .began {
NSLog("audio interruption: began")
} else if interruption == .ended {
NSLog("audio interruption: ended")
if isRunning {
// Sadly this lengthy delay hack is necessary, otherwise sounds won't play :(
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
self.start() // sets up the audio session, connects nodes, starts the engine, plays the player, and sets isRunning to true
}
}
}
}
And the delay really does need to be this long. 2s didn't do the trick.
Upvotes: 0
Reputation: 395
Finally got it!
Solution: added the mixable option by changing the setCategory line to be:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,
withOptions: .mixWithOthers )
Upvotes: 6
Reputation: 9354
For me reload player after end interruption resolve the problem:
func interruptionNotification(_ notification: Notification) {
guard let type = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt,
let interruption = AVAudioSessionInterruptionType(rawValue: type) else {
return
}
if interruption == .ended && playerWasPlayingBeforeInterruption {
player.replaceCurrentItem(with: AVPlayerItem(url: radioStation.url))
play()
}
}
Upvotes: 0
Reputation: 2011
I wrote a code like this and successfully resumed the audio from the phone call ended. I was build on Xcode 6.4 and ran the app on my iPhone 4S.
var player: AVQueuePlayer! = nil
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(true)
var song = AVPlayerItem(URL: NSBundle.mainBundle().URLForResource("AlmostAYearAgo", withExtension: "mp3")!)
player = AVQueuePlayer(items: [song])
let theSession = AVAudioSession.sharedInstance()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playInterrupt:", name: AVAudioSessionInterruptionNotification, object: theSession)
player.play()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playInterrupt(notification: NSNotification)
{
if notification.name == AVAudioSessionInterruptionNotification && notification.userInfo != nil
{
var info = notification.userInfo!
var intValue: UInt = 0
(info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)
if let type = AVAudioSessionInterruptionType(rawValue: intValue)
{
switch type
{
case .Began:
print("aaaaarrrrgggg you stole me")
player.pause()
case .Ended:
let timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "resumeNow:", userInfo: nil, repeats: false)
}
}
}
}
func resumeNow(timer : NSTimer)
{
player.play()
print("attempted restart")
}
Upvotes: 1