Swift 中断后如何恢复音频?

How to resume audio after interruption in Swift?

我正在按照说明进行操作 ,我已经将这个测试项目放在一起来处理音频播放的中断。具体来说,我使用默认 iphone 时钟应用程序的闹钟作为中断。似乎正在调用中断处理程序,但没有通过 let = interruptionType 行,因为“错误类型”出现了两次。

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player = AVAudioPlayer()

    let audioPath = NSBundle.mainBundle().pathForResource("rachmaninov-romance-sixhands-alianello", ofType: "mp3")!

    func handleInterruption(notification: NSNotification) {

        guard let interruptionType = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? AVAudioSessionInterruptionType else { print("wrong type"); return }

        switch interruptionType {

        case .Began:
            print("began")
            // player is paused and session is inactive. need to update UI)
            player.pause()
            print("audio paused")

        default:
            print("ended")
            /**/
            if let option = notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? AVAudioSessionInterruptionOptions where option == .ShouldResume {
                // ok to resume playing, re activate session and resume playing
                // need to update UI
                player.play()
                print("audio resumed")
            }
            /**/
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        do {
            try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))
            player.numberOfLoops = -1 // play indefinitely
            player.prepareToPlay()
            //player.delegate = player

        } catch {
            // process error here
        }

        // enable play in background  but this audio still gets interrupted by alerts
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            print("AVAudioSession Category Playback OK")
            do {
                try AVAudioSession.sharedInstance().setActive(true)
                print("AVAudioSession is Active")
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }

        // add observer to handle audio interruptions
        // using 'object: nil' does not have a noticeable effect
        let theSession = AVAudioSession.sharedInstance()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleInterruption(_:)), name: AVAudioSessionInterruptionNotification, object: theSession)

        // start playing audio
        player.play()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

此外,根据一个想法here,我将处理程序修改为

func handleInterruption(notification: NSNotification) {

        //guard let interruptionType = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? AVAudioSessionInterruptionType else { print("wrong type"); return }

        if notification.name != AVAudioSessionInterruptionNotification
            || notification.userInfo == nil{
            return
        }

        var info = notification.userInfo!
        var intValue: UInt = 0
        (info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)
        if let interruptionType = AVAudioSessionInterruptionType(rawValue: intValue) {

            switch interruptionType {

            case .Began:
                print("began")
                // player is paused and session is inactive. need to update UI)
                player.pause()
                print("audio paused")

            default:
                print("ended")
                /** /
                if let option = notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? AVAudioSessionInterruptionOptions where option == .ShouldResume {
                    // ok to resume playing, re activate session and resume playing
                    // need to update UI
                    player.play()
                    print("audio resumed")
                }
                / **/
                player.play()
                print("audio resumed")
            }
        }
    }

结果是 "began"、"audio paused"、"ended" 和 "audio resumed" 全部显示在控制台中,但实际上并未恢复音频播放。

注意:我将 player.play() 移到注释掉的 where option == .ShouldResume if 语句之外,因为当 .Ended 中断发生时 if 条件不成立。

(代表问题作者发表,在问题发表后).

已找到解决方案!在讨论 之后,将其插入 viewDidLoad()

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
} catch {        
}

点击"ok"报警中断后,音频继续播放。与之前提到的不同,该解决方案不需要中断处理程序(@Leo Dabus 已将其删除)。

但是,如果您使用的是中断处理程序,则不得在 handleInterruption() 中调用 .play(),因为这样做并不能保证继续播放并且似乎会阻止调用 audioPlayerEndInterruption() (参见 docs)。相反,必须在 audioPlayerEndInterruption()(它的 3 个版本中的任何一个)中调用 .play() 以保证恢复。

此外,如果您希望您的应用程序在后台运行时中断后继续播放,则必须给 AVAudioSession 选项 .MixWithOthers 由@Simon Newstead 指出。看起来,如果用户希望应用程序在进入后台时继续播放,那么可以合理地假设用户也希望应用程序在应用程序处于后台时中断后继续播放。事实上,这就是 Apple Music 应用程序所表现出的行为。

@rockhammers 的建议对我有用。 Here

之前 class

let theSession = AVAudioSession.sharedInstance()

在 viewDidLoad 中

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.handleInterruption(notification:)), name: NSNotification.Name.AVAudioSessionInterruption, object: theSession)

然后是函数

func handleInterruption(notification: NSNotification) {
    print("handleInterruption")
    guard let value = (notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber)?.uintValue,
        let interruptionType =  AVAudioSessionInterruptionType(rawValue: value)
        else {
            print("notification.userInfo?[AVAudioSessionInterruptionTypeKey]", notification.userInfo?[AVAudioSessionInterruptionTypeKey])
            return }
    switch interruptionType {
    case .began:
        print("began")
        vox.pause()
        music.pause()
        print("audioPlayer.playing", vox.isPlaying)
        /**/
        do {
            try theSession.setActive(false)
            print("AVAudioSession is inactive")
        } catch let error as NSError {
            print(error.localizedDescription)
        }
        pause()
    default :
        print("ended")
        if let optionValue = (notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? NSNumber)?.uintValue, AVAudioSessionInterruptionOptions(rawValue: optionValue) == .shouldResume {
            print("should resume")
            // ok to resume playing, re activate session and resume playing
            /**/
            do {
                try theSession.setActive(true)
                print("AVAudioSession is Active again")
                vox.play()
                music.play()
            } catch let error as NSError {
                print(error.localizedDescription)
            }
            play()
        }
    }
}