我正在学习 Swift 作为我的第一门编程语言。
我花了好几个小时才在中断(例如通话)后恢复背景音频播放
应该发生什么:
备注:
代码:
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")
}
}
解决方案:通过将 setCategory 行更改为:
添加可混合选项
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,
withOptions: .mixWithOthers )
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")
}
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 并没有达到目的。
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
那么无论你做什么,如果你使用:,你的播放器都不会恢复播放音频:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
然后它就完美地工作了,并且在应用程序处于后台时接到电话后将恢复。
谢谢!