.duckOther在SMS后无法正常工作。本地或推送通知

时间:2019-01-30 19:23:31

标签: ios swift iphone avaudiosession background-music

我遇到了一个有趣的行为。

我有一个AudioSession带有.duckOthers选项的应用程序,但是在SMS,Push或Local通知(取消通知徽章后)之后,背景音乐会响亮。

如何防止这种行为?

我有一个相关的问题:

是否可以从SMS,推送或本地通知中捕获声音中断?

我尝试捕获“ AVAudioSession.interruptionNotification”,但是没有运气。我还尝试捕获更多通知,但也没有运气。

代码:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    private var audioPlaying = AVAudioPlayer()

    func playSound() {
         let alertSound = URL(fileURLWithPath:Bundle.main.path(forResource: "someSound", ofType: "mp3")!)
         try! audioPlaying = AVAudioPlayer(contentsOf: alertSound)
         audioPlaying.prepareToPlay()
         audioPlaying.play()
     }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    @objc func interruptionTriggered(notification: Notification) {
        print("Interruption triggered \(notification.debugDescription)")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default, options: [.duckOthers])
        try? AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
       NotificationCenter.default.addObserver(self, selector: #selector(interruptionTriggered), name:AVAudioSession.interruptionNotification /*nil in case to get more notifications*/,object:nil)
       playSound()
    }
}

我发现了非常相似的问题,但没有任何答案 duckOthers Audio Session Interrupted by Remote Push Notification

0 个答案:

没有答案