2

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

但是,我无法弄清楚如何执行以下操作;同时获取 iOS 设备和耳机的音量。现在,我只能检索当前活动硬件路由的音量。

这个问题有解决方案吗?

4

1 回答 1

0

I'm not sure if this is enough of an answer, but by the best I could manage was to do as you say and retrieve the volume of the headphones when they are the active route. I then compare this to the volume prior.

To get the headphone volume when plugged in, you can subscribe to AVAudioSessionRouteChangeNotification:

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

And then comparing the current volume in the callback to a last recorded volume.

- (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];
        }
    }
}

(This code will only work when headphones are plugged in after the app is started, but you could of course check the route and its volume before adding the notification observer.)

于 2015-03-26T03:22:45.683 回答