Troubleshooting Volume Control in iOS

There are actually a lot of tips for volume control in iOS. This little episode will help you solve your confusion. There are two main places to control the volume in iOS, one is the system volume, the user actively presses the volume key to adjust the volume, this method will display the system volume prompt box; the other is the volume of the player, such as adjusting the volume through AVAudioPlayer, this The system prompt volume box will not be displayed.

[1] How to not display the system volume prompt box when adjusting the volume The
main principle is to get the system volume View and make it invisible to the user. But note that you can't set the hidden property of MPVolumeView to YES, the result is that the system volume prompt box will still be displayed when the user adjusts the volume. [Figure 1]

[2] Get system volume
Method 1: Get system volume through
self.volumeSlider Very tangled, the result is that the obtained system volume is inaccurate. This is because volumeSlider.value has not been assigned a value in the initial MPVolumeView. Through [Figure 2], it can be found that the volume is later updated through [MPVolumeController updateVolumeValue]. So we can get the system volume by listening to the events when the value of self.volumeSlide changes.

[self.volumeSlider addTarget:self action: @selector (sliderValueDidChange:) forControlEvents:UIControlEventValueChanged];

Method 2: Get it through AVAudioSession
This method is straightforward.
[[AVAudioSession sharedInstance] outputVolume];

[3] Custom volume control
If you want to customize the volume control, you can monitor the volume change and hide the system volume prompt box through the first method. By monitoring notifications, the effect of monitoring volume changes is achieved.

[4] Monitor volume changes
Monitor volume changes, notify AVSystemController_SystemVolumeDidChangeNotification by monitoring

[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector (volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];

The final result AVSystemController_AudioVolumeNotificationParameter represents the value of the volume, here It should be noted that "AVSystemController_AudioVolumeChangeReasonNotificationParameter" = ExplicitVolumeChange; this value, which indicates the reason for the volume change. It should be noted that in some cases it does not modify the system volume. It will have different values ​​due to different scenarios. ExplicitVolumeChange is when the user clicks the volume button, CategoryChange is when the user presses the home button to call up Siri, and RouteChange is a route modification (not very clear, under what circumstances).

AVSystemController_SystemVolumeDidChangeNotification; object = <AVSystemController: 0x1c4001dc0>; userInfo = {
"AVSystemController_AudioCategoryNotificationParameter" = "Audio/Video";
"AVSystemController_AudioVolumeChangeReasonNotificationParameter" = ExplicitVolumeChange;
"AVSystemController_AudioVolumeNotificationParameter" = "0.5625";
"
AVSystemController_UserVolumeAboveEUVolume

"
If the value of self.volumeSlide is modified through code, the system volume box will be displayed. If you find that a system volume box suddenly pops up on a page, most of the reasons are that you have modified this value.

For more knowledge collection content, please check: web link
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939596&siteId=291194637