Sync Volume rocker with a UISlider

2019-06-13 03:29发布

问题:

I am trying to get a UISlider to sync with the volume rocker. I have successfully used the slider to change the system/ringer volume, but I need the slider to move when the rocker moves. any suggestions on how to get the current volume value? I am using the code below to monnitor the volume change:

- (void)volumeChanged:(NSNotification *)notification{

       AVSystemController* avc = SharedAVSystemController;
       //   my sliders name is customVolume, and this is how I 
       //   am trying to get the volume:
       customVolume.value = [avc getVolume:(float *) forCategory:(id)];

}

Also, what are the AVSystemController categories other than "Ringtone"?

回答1:

There is a class available in the SDK which handles the system volume all by itself, MPVolumeView. It's not a subclass of UISlider therefore you can't change it's appearance (Well, it would be hard hacking it together all by yourself) but that's probably not an issue. (edit: looking at class dumps, the view contains a normal UISlider view so it may be easier than I expected)

I'm not sure whether it handles everything you need and if it will fit in your context, but you can also take a look at it and try it.



回答2:

This will update theUISliderwith the volume rocker:

- (void)volumeChanged:(NSNotification *)notification
{
    AVSystemController *avs = [ notification object ];
    NSString *audioDeviceName;
    [ avs getActiveCategoryVolume:&_volume andName:&audioDeviceName ];
}