【发布时间】:2015-03-20 04:54:16
【问题描述】:
我有一个使用AVPlayer(实际上是AVQueuePlayer)播放音频的应用程序。它设置MPNowPlayingInfoCenter 信息,使用MPRemoteCommandCenter 管理远程命令,并允许背景音频。所有这些功能都可以通过控制中心和锁定屏幕正常工作。当我将手机带入车内时,它也可以与车内的蓝牙接口正常工作。但是,如果我通过 iPhone 6 闪电端口将手机直接插入汽车的立体声音响,它就会严重损坏。它确实会播放音频,但是...
(1) 我的汽车仪表盘上不会显示任何正在播放的信息 (2) 每当我尝试做任何事情时,我的汽车音响都会抛出一堆连接错误。
我手机上的其他应用没有这个问题,所以我认为这不是我的音响的问题。我用的是苹果官方的闪电线,所以也没有关系。
我通过我的 Apple TV 尝试了我的带有 AirPlay 的应用程序,它同样不稳定。我注意到AVPlayer 有一个名为allowsExternalPlayback 的重要属性,如果您只播放音频,则需要将其设置为NO。将该属性设置为NO 后,通过Apple TV 的AirPlay 可以正常工作。我认为这与影响我的汽车的问题相同,但事实并非如此。即使将allowsExternalPlayback 设置为NO,通过我的汽车上的USB 连接播放音频仍然搞砸了。
似乎 (a) AVAudioSession 或 (b) AVPlayer 的配置方式有问题。
我的音频播放器配置非常简单...
self.audioQueue = [AVQueuePlayer queuePlayerWithItems:nil];
self.audioQueue.allowsExternalPlayback = NO;
[self.audioQueue addObserver:self forKeyPath:@"status" options:0 context:kAVPlayerStatusContext];
[self.audioQueue addObserver:self forKeyPath:@"rate" options:0 context:kAVPlayerRateContext];
[self.audioQueue addObserver:self forKeyPath:@"error" options:0 context:kAVPlayerErrorContext];
[self.audioQueue addObserver:self forKeyPath:@"currentItem" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:kAVPlayerCurrentItemContext];
...就像在我的音频会话中...
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error;
[audioSession setActive:YES error:&error];
// Error handling code
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
// Error handling code
// Audio session notifications
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(audioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:audioSession];
[defaultCenter addObserver:self selector:@selector(audioSessionRouteChangeNotification:) name:AVAudioSessionRouteChangeNotification object:audioSession];
[defaultCenter addObserver:self selector:@selector(audioSessionMediaServicesWereResetNotification:) name:AVAudioSessionMediaServicesWereResetNotification object:audioSession];
是否需要设置其他属性来处理此用例?我能做些什么来调试这里发生的事情?这个是我的。
【问题讨论】:
标签: objective-c avplayer