我想在我的应用程序中保存当前音量,使用[MPMusicPlayerController iPodMusicPlayer]
可以轻松访问,我可以通过订阅路线更改通知来收听不同的硬件“路线”(因此我可以检测耳机等) )。
然而,我无法弄清楚如何做以下事情;立刻获得iOS设备和耳机的音量。现在,我只能检索当前活动硬件路由的音量。
这个问题有解决方法吗?
答案 0 :(得分:0)
我不确定这是否足够的答案,但我能管理的最好的就是按照你的说法去做,并在它们是主动路线时检索耳机的音量。然后我将其与之前的音量进行比较。
要在插入时获取耳机音量,您可以订阅AVAudioSessionRouteChangeNotification
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteDidChange:) name:AVAudioSessionRouteChangeNotification object:nil];
然后将回调中的当前音量与最后录音的音量进行比较。
- (void) audioRouteDidChange:(NSNotification*)notificaiton {
AVAudioSessionRouteChangeReason routeChangeReason = [[notificaiton.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey] intValue];
if (routeChangeReason == AVAudioSessionRouteChangeReasonNewDeviceAvailable) {
AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {
if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
[self saveHeadphoneVolume];
}
}
}
(此代码仅在应用程序启动后插入耳机时才有效,但您可以在添加通知观察者之前检查路线及其音量。)