简体   繁体   English

AVAudioSession不尊重mixWithOthers

[英]AVAudioSession doesn't respect mixWithOthers

I'm trying to build an alarm app. 我正在尝试构建一个警报应用程序。 To play alarms when the app is in the background, my app plays a completely silence sound (however I'm hearing a quiet noise, although if I play the same sound file on my computer there is definitely nothing to hear) in the background until the alarm time comes and the AVPlayer plays the alarm sound. 要在应用程序处于后台时播放警报,我的应用程序会在后台播放完全静音的声音(但是我听到的是安静的声音,尽管如果我在计算机上播放相同的声音文件,肯定听不到声音)警报时间到了,AVPlayer播放警报声音。 It works, but when my app plays the silent sound in the background, some other apps (especially some games) don't play their sound. 它可以工作,但是当我的应用程序在后台播放无声声音时,其他一些应用程序(尤其是某些游戏)将无法播放声音。 This is kind of weird, because there is the app 'Alarmy' in the AppStore, which definitely uses the same system with a silent sound, but when I set an alarm in this app, it doesn't silence any other sound. 这有点奇怪,因为AppStore中有一个应用程序“ Alarmy”,该应用程序肯定使用具有静音声音的同一系统,但是当我在此应用程序中设置警报时,它不会使任何其他声音静音。

How can I achieve this? 我该如何实现? Currently I'm setting my AVAudioSession like this (I'm setting it in the initializer of my alarm player class, of which i have an instance in my AppDelegate): 目前,我正在像这样设置我的AVAudioSession(我在我的警报播放器类的初始化程序中设置了它,我的AppDelegate中有一个实例):

let session = AVAudioSession.sharedInstance()
do {
    try session.setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
    try session.setActive(true)
} catch {
    print("Failed to set audio session category.  Error: \(error)")
}

EDIT: For more explanation about the app Alarmy, look also at App "Alarmy" is able to play iTunes song from background state... How? 编辑:有关应用程序Alarmy的更多说明,另请参阅应用程序“ Alarmy”能够从背景状态播放iTunes歌曲...怎么样?

"To play alarms when the app is in the background, my app plays a completely silence sound." “要在应用程序处于后台运行时发出警报,我的应用程序会播放完全静音的声音。” Do you mean you're trying to use playing silence to keep your app alive? 您是说要尝试使用静音来保持您的应用程序正常运行吗? This isn't allowed. 这是不允许的。 If you've found other apps that do this (how do you know they're doing it that way?), that doesn't mean it's allowed. 如果您发现其他执行此操作的应用程序(您怎么知道他们正在这样做?),这并不意味着它被允许。 The fact that someone has gotten away with something doesn't change AppStore rules (and doesn't change the fact that silent audio is a major battery drain). 某人已经放弃某些东西这一事实不会改变AppStore规则(也不会改变无声音频会消耗大量电池这一事实)。

The correct way to build an app that launches at a specific time to play an alarm is with a UNNotificationRequest . 构建在特定时间启动以播放警报的应用的正确方法是使用UNNotificationRequest You can register the notification to wake you up at a given date and time and you can perform your alarm then, or (often better) just have the notification take care of the alarm without waking you up until the user taps on the message. 您可以注册通知以在给定的日期和时间将您唤醒,然后可以执行警报,或者(通常更好)只是让通知来处理警报,而无需唤醒用户,直到用户点击消息为止。

See the Local and Remote Notification Programming Guide for full details. 有关完整的详细信息,请参见《 本地和远程通知编程指南》

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM