如何在 Swift 2 / AVPlayer 中恢复背景音频?

How to resume background audio in Swift 2 / AVPlayer?

我正在学习 Swift 作为我的第一门编程语言。

我已经挣扎了好几个小时才能在中断(例如通话)后恢复后台音频播放

应该发生什么:

  1. 当应用进入后台时音频继续播放(有效)
  2. 被来电打断时,获取打断开始通知 (作品)
  3. 通话结束时,获取中断通知 结束(作品)
  4. 继续播放音频(不起作用 - 什么也听不到

非常感谢任何帮助!谢谢

备注:

  1. 该应用已注册背景音频并且在中断前播放正常
  2. 我试过有和没有时间延迟来恢复播放 - 都没有用

代码:

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([=11=], 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")
    }
}

我写了这样的代码,并成功地从 phone 通话结束后恢复了音频。我是在 Xcode 6.4 和 运行 我的 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")
}

终于明白了!

解决方案:通过将 setCategory 行更改为添加了可混合选项:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,
                                            withOptions: .mixWithOthers )

西蒙,我这边绝对确认。我花了2天时间寻找它!如果您只使用:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 那么不管你做什么你的播放器都不会恢​​复播放音频,如果你使用:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)

然后它会完美运行,并且会在应用程序处于后台时收到 phone 调用后恢复。

谢谢!

对于我来说结束中断后重新加载播放器解决问题:

    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()
    }
  }

对我来说,是通过添加延迟来解决问题:

@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
            }
        }
    }
}

而且延迟确实需要这么久。 2s 没有成功。