Please note:
## Change the system volume can not be seen on the emulator, you must use the real machine debugging to see the effect!
Before use, please import libraries # import < > MediaPlayer/MediaPlayer. H
1. Obtain the system volume control view
First declare the _volumeViewSlider property
-(void)getVolumeVolue { MPVolumeView *volumeView = [[MPVolumeView alloc] init]; _volumeViewSlider = nil; for (UIView *view in [volumeView subviews]){ if ([view.class.description isEqualToString:@"MPVolumeSlider"]){ _volumeViewSlider = (UISlider *)view; break; }}}Copy the code
2. Change the system volume
- (void) verticalMoved: (CGFloat) value {/ / change the volume of the system self. VolumeViewSlider. Value = value; // The smaller the smaller the smaller the value between 0 and 1}Copy the code
Other requirements:
After obtaining the slider to control the volume through the above method, when we change the value of the slider, the system’s volume prompt box will be displayed. Maybe some apps need to define the prompt box to change the volume, so we need to hide the system’s volume prompt box
#### implementation: the above to obtain the system volume slider, and not if the slider into our view, if it into our view, you will find that the system volume prompt box will not be displayed, you can know: VolumeViewSlider is not displayed in the view hierarchy, and the system’s volume prompt is displayed when the volume slider is not in the view hierarchy. Adjust the volumeViewSlider’s coordinates to the lowest level, or set its coordinates to negative values such as (-1000,-1000,100,4). However, it should be noted that the hidden value cannot be set to YES. If it is set to YES, It detects that the volumeViewSlider is not in the view hierarchy, and the system’s volume prompt is displayed