【问题标题】:How to resume background audio in Swift 2 / AVPlayer?如何在 Swift 2 / AVPlayer 中恢复背景音频?
【发布时间】:2015-12-09 11:26:51
【问题描述】:

我正在学习 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($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")
    }
}

【问题讨论】:

  • 方法是否被正确调用?尤其是 resumeNow 方法。
  • 是的 - 我在通话终止 3 秒后看到日志显示“尝试重新启动”。

标签: ios swift audio avfoundation avplayer


【解决方案1】:

终于明白了!

解决方案:通过将 setCategory 行更改为添加 mixable 选项:

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

【讨论】:

  • 我们的应用音乐和iPhone音乐都播放可以吗?
【解决方案2】:

我写了这样的代码并成功恢复了电话结束时的音频。我基于 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")
}

【讨论】:

  • 试过了,但似乎什么也没做。恢复时仍无法播放音频。
  • 在这两种情况下:case .Ended 和 resumeNow 就在 player.play() 之前你设法让它工作了吗?
  • 嗨,Jason,谢谢,但这似乎无助于解决问题(而且是很久以前的问题了 - 我可以看看它在哪里被修复或记录了吗?)
  • 感谢 Jason,之前通读但有点困惑,因为在该页面上的链接文档中:developer.apple.com/library/ios/documentation/AVFoundation/… 它说处理音频部分已弃用。你有什么建议吗?顺便说一句,作为初学者,我非常感谢像你这样有经验的人的帮助!
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    对我来说,它增加了解决问题的延迟:

    @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 没用。

    【讨论】:

      【解决方案5】:

      Simon,请绝对确认。我花了2天时间寻找它!如果你只使用:

      AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 那么不管你做什么,如果你使用它,你的播放器将不会继续播放音频:

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

      然后它就像完美一样工作,并且在应用程序处于后台时接到电话后会恢复。

      谢谢!

      【讨论】:

      • Simon,不确定您是否注意到,但这不是有效的解决方案。我认为该解决方案有效,但是当您使用: AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers 然后您的播放器将与其他播放器混合。尝试同时盯着音乐应用程序。您将获得 2 种声音同时播放和混音。它适用于在后台通话后恢复,但它会扰乱你的声音的整个播放。有人知道吗?
      • 我们缺少的东西是 self.becomeFirstResponder() 一旦添加了整个事情就开始为我工作了。
      • 很高兴知道,谢谢。我不需要它,因为我的应用是一个无声冥想应用,它们不会自己混合音频,所以没有测试过这个选项。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2012-08-26
      • 2011-11-15
      • 1970-01-01
      • 2016-12-12
      相关资源
      最近更新 更多