iOS - AVAudioSession后台路由更改通知 [英] iOS - AVAudioSession Route Change Notification in background

查看:169
本文介绍了iOS - AVAudioSession后台路由更改通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管该主题有很多帖子( AVPlayer& AVAudioSession 在AppDelegate.m中设置AVAudioSession类别 AvAudioSession在iOS7中不起作用?),我无法解决iOS 7中的这个特定问题:

- 我想要在后台接收AVAudioSession路线更改通知

- 我的应用程序在任何时候都没有播放任何音乐,并且不应干扰播放声音的其他应用

Despite many posts on the subject (AVPlayer & AVAudioSession, Setting the AVAudioSession category in AppDelegate.m, AvAudioSession won't work in iOS7?), I could not solve this specific issue in iOS 7:
- I want to receive AVAudioSession Route Change notifications while in the background
- my app is not playing any music at any point, and should not interfere with other apps playing sound

设置:

- 在p-list中启用音频UIBackgroundModes

- 在主控制器中:

The setup:
- audio enabled in p-list UIBackgroundModes
- in the main controller:

// 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开头的不同类别,没有运气。

This works fine in foreground, so I must be missing something simple to enable background notifications. I have tried several different categories starting with AVAudioSessionCategoryAmbient, with no luck.

播放声音时是否只接收背景通知有限制?

Is there a restriction to only receive background notifications when playing sound?

感谢您的帮助!

UPDATE
需要播放AVPlayer才能在后台接收通知。不幸的是,这是不可行的,期待通过播放一个无法接受苹果的无声音轨......

UPDATE There needs to be an AVPlayer playing to receive notifications in background. So this is unfortunately not doable, expect by playing a silent track, which is not Apple accepted...

推荐答案

如果你'在后台重新播放而不播放音乐,您的应用已暂停。您无法接收通知或其他任何内容。如果您有任何通知,则会在您的应用再次激活后发送。

If you're in the background and not playing music, your app is suspended. You can't receive notifications or anything else. If there are any notifications for you, they will be delivered when your app is activated again.

这篇关于iOS - AVAudioSession后台路由更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆