Как возобновить звук после перерыва в 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 https://stackru.com/a/30280699/1827488 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.
    }
}

Кроме того, следуя этой идее, я изменил обработчик на

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

В результате все "начало", "приостановка звука", "завершение" и "возобновление звука" отображаются в консоли, но воспроизведение звука фактически не возобновляется.

Примечание: я переместил player.play() вне закомментированного where option == .ShouldResume если заявление, потому что это if условие не верно, когда .Ended прерывание происходит.

3 ответа

Решение

(Опубликовано от имени ОП).

Решение найдено! После обсуждения здесь, вставил это в viewDidLoad()

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

После нажатия кнопки "ОК" при прерывании будильника воспроизведение звука продолжилось. В отличие от ранее отмеченного, решение НЕ требует обработчика прерываний (который с тех пор @Leo Dabus удалил).

Однако, если вы используете обработчик прерываний, .play() НЕ должен вызываться в handleInterruption() так как это НЕ гарантирует, что игра возобновится и, кажется, мешает audioPlayerEndInterruption() быть вызванным (см. документы). Вместо .play() должны быть вызваны в течение audioPlayerEndInterruption() (любой из 3-х его версий), чтобы гарантировать возобновление.

Более того, AVAudioSession надо дать вариант .MixWithOthers замечено @Simon Newstead, если вы хотите, чтобы ваше приложение возобновляло воспроизведение после прерывания, когда ваше приложение находится в фоновом режиме. Кажется, что если пользователь хочет, чтобы приложение продолжало играть, когда оно переходит в фоновый режим, логично предположить, что пользователь также хочет, чтобы приложение возобновило воспроизведение после прерывания, пока приложение находится в фоновом режиме. Действительно, именно такое поведение демонстрирует приложение Apple Music.

Предложение @rockhammers сработало для меня. Вот

перед уроком

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

по некоторым причинам interruptionNotification не работает правильно на iOS 12.x, поэтому я добавил молчаниеSecondaryAudioHintNotification.

      @objc func handleSecondaryAudioSilence(notification: NSNotification) {
    guard let userInfo = notification.userInfo,
          let typeValue = userInfo[AVAudioSessionSilenceSecondaryAudioHintTypeKey] as? UInt,
          let type = AVAudioSession.SilenceSecondaryAudioHintType(rawValue: typeValue) else {
        return
    }
    
    if type == .end {
        // Other app audio stopped playing - restart secondary audio.
        reconnectAVPlayer()
    }
}
Другие вопросы по тегам