【问题标题】:iOS - AVAudioSession Route Change Notification in backgroundiOS - 后台的 AVAudioSession 路由更改通知
【发布时间】:2014-01-21 00:23:16
【问题描述】:

尽管有很多关于该主题的帖子(AVPlayer & AVAudioSessionSetting the AVAudioSession category in AppDelegate.mAvAudioSession won't work in iOS7?),但我无法在 iOS 7 中解决这个特定问题:
- 我想在后台接收 AVAudioSession 路由更改通知
- 我的应用在任何时候都不会播放任何音乐,并且不应干扰其他应用播放声音

设置:
- 在 p-list UIBackgroundModes 中启用音频
- 在主控制器中:

// AUDIO
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback  withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[audioSession setActive:YES error:nil];

// NOTIFICATIONS
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(audioSessionRouteChanged:)
                                                 name:AVAudioSessionRouteChangeNotification
                                               object:nil];

这在前台工作得很好,所以我必须缺少一些简单的东西来启用后台通知。我从 AVAudioSessionCategoryAmbient 开始尝试了几个不同的类别,但都没有成功。

播放声音时是否只接收后台通知?

感谢您的帮助!

更新 需要播放 AVPlayer 才能在后台接收通知。所以不幸的是,这是不可行的,期待通过播放无声的曲目,这不是 Apple 接受的......

【问题讨论】:

    标签: ios avaudiosession


    【解决方案1】:

    如果您在后台且未播放音乐,您的应用将暂停。您无法接收通知或其他任何内容。如果有任何通知给您,将在您的应用再次激活时发送。

    【讨论】:

    • 2021 年仍然如此吗?我不能在后台收到通知吗?
    • @Keselme 所述的答案仍然正确。您可以在后台收到信号,但通知中心会发出通知。
    • 你的意思是but not notifications from the Notification Center. 吗?
    • @Keselme 是的,对不起。
    猜你喜欢
    • 1970-01-01
    • 2011-04-28
    • 2013-11-28
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    相关资源
    最近更新 更多