gpt4 book ai didi

ios - iOS:从其他 'routes'检索音量

转载 作者:行者123 更新时间:2023-12-01 16:56:56 27 4
gpt4 key购买 nike

我想将当前音量保存在我的应用程序中,可以使用[MPMusicPlayerController iPodMusicPlayer]轻松访问该音量,并且我可以通过订阅路由更改通知来收听不同的硬件“路由”(这样我就可以检测到耳机等)。

但是,我不知道如何执行以下操作;同时获取iOS设备和耳机的音量。现在,我只能检索当前 Activity 硬件路由的数量。

有解决这个问题的方法吗?

最佳答案

我不确定这是否足以解决问题,但最好的办法是按照您所说的去做,并在处于 Activity 状态时恢复耳机的音量。然后,我将其与之前的数量进行比较。

要获得插入耳机的音量,可以订阅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];
}
}
}

(此代码仅在启动应用程序后插入耳机时才有效,但是您当然可以在添加通知观察器之前检查路线及其音量。)

关于ios - iOS:从其他 'routes'检索音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10882165/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com