Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resume background audio in Swift 2 / AVPlayer?

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:

  1. Audio keeps playing when app goes to background (works)
  2. When interrupted by a call, get the notification for interruption began (works)
  3. When call ends, get the notification for interruption ends (works)
  4. Resume playing the audio (does NOT work - hear nothing)

Really appreciate any help! Thanks

Notes:

  1. The app is registered for background audio and plays fine before interruption
  2. I have tried with and without the time delay to resume playing - neither work

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")
    }
}
like image 779
Simon Newstead Avatar asked Sep 13 '15 09:09

Simon Newstead


2 Answers

Finally got it!

Solution: added the mixable option by changing the setCategory line to be:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,
                                            withOptions: .mixWithOthers )
like image 168
Simon Newstead Avatar answered Nov 01 '22 20:11

Simon Newstead


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")
}
like image 35
Jason Nam Avatar answered Nov 01 '22 21:11

Jason Nam