Catalogue [-]
- Using Mpvolumeview
- Programming for System Volume adjustment 2
- Operating system volume via an instance of Mpvolumeslider
- There's a problem! I don't like the system eject volume prompt
- I've changed the system volume, but not through my UI.
The iOS avfoundation framework provides basic audio and video playback tools, and we can basically do most of the audio and video playback tasks by providing the classes. However, in the processing of output volume of audio playback, Apple's strategy is more conservative. Although AVPlayer
these AVPAudiolayer
classes and Zhe provide volume adjustment capabilities, these volume controls are app-level controls. The benefit is that the volume adjustment is independent of the system volume and does not affect the system volume when resized. But sometimes we might want to change the system volume so that if the system volume is too low, the app won't be able to adjust the volume significantly when the sound is tuned. In general, there are several ways to adjust the system volume:
Note: Modifying the system volume does not see the effect on the emulator, it must be debugged with a real machine to see the effect!
Use
MPVolumeView
This method is Apple's official recommendation. MPVolumeView
is a UI component in the media Player framework that directly contains control over the system volume and audio mirroring routing of Airplay devices. It contains one MPVolumeSlider
of the subview used to control the volume. This MPVolumeSlider
is a private class, and we cannot create this class manually, but this class is a UISlider
subclass. is MPVolumeView
easy to use, just add it to a parent view, give the parent view the right size, create an MPVolumeView
example, add it to the parent view, and Apple's official Document 1 has the sample code to refer to.
The disadvantages of this method are as follows:
The UI can be customized to a low degree. MPVolumeView
only a limited number of methods are available to customize the style of the slider and route button, and can only be solved by changing the image. If you want to change the slider operation to a button or other UI component, that's not possible.
There is no additional volume control API. there is no direct operating system volume available in the public API for iOS so far, so modifying the system volume can only use this UI component.
If you also want to add gestures to the UI to control the volume, this direct use MPVolumeView
is not possible, then is there any way to bypass this restriction? There is still some way.
Programming for System Volume adjustment 2
The previous section we mentioned MPVolumeView
in this component, there is a subview to control the volume, i.e. MPVolumeSlider
. We can actually iterate through the MPVolumeView
instance's subviews to get MPVolumeSlider
an instance of the operating system volume through this UI component.
Pass
MPVolumeSlider
instance to the operating system volume
We start by creating one and MPVolumeView
then traversing MPVolumeSlider
the found instance. This example provides a setValue:animated:
way to set the system volume. We can also use volumeSlider.value
this property to get the current system volume. The specific code is as follows:
?
12345678910111213 |
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
UISlider* volumeViewSlider = nil;
for
(UIView *view in [_instance.volumeView subviews]){
if
([view.
class
.description isEqualToString:@
"MPVolumeSlider"
]){
volumeViewSlider = (UISlider*)view;
break
;
}
}
// retrieve system volumefloat systemVolume = volumeViewSlider.value;
// change system volume, the value is between 0.0f and 1.0f
[volumeViewSlider setValue:1.0f animated:NO];
// send UI control event to make the change effect right now.
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
|
The above code shows how to get and modify the system volume, noting that the volume value is a floating-point number between 0 and 1.
There's a problem! I don't like the system eject volume prompt
The above programming method can be perfect to adjust the system volume, but each change will pop up the system notification box to inform:
Sometimes this kind of hint we may not need, then how to cancel out this hint? MPVolumeView
no interface is actually provided to adjust whether the system volume prompts need to be displayed. But we found that MPVolumeView
the system does not display a volume prompt when it is in the hierarchy of the current view . Well, then, let's just make sure two points are done:
MPVolumeView
The view is not visible on the screen, such as under an opaque view, or a non-viewable area of the view, a common practice is to set the frame of the view to a place outside the area, such asvolumeView.frame = CGRectMake(-1000, -100, 100, 100);
Make sure that MPVolumeView
the hidden property value for the view is NO
. Because when hidden is the YES
same, a hint pops up.
I've changed the system volume, but not through my UI.
Another possibility is that the user himself adjusts the volume through the Hardware's volume Adjustment button (located on the side of the device), which can cause problems with your business logic because you only write callbacks for your app UI, so how do you add callbacks for hardware button events? We can use Notification Center to do this.
You only need to listen to AVSystemController_SystemVolumeDidChangeNotification
events here. The specific code is as follows:
Nserror *error;
Active Audio Session before the change event for listen to the volume.
It must be called first.
The old style code equivalent to the line below is:
Audiosessioninitialize (NULL, NULL, NULL, NULL);
Audiosessionsetactive (YES);
Now the code above are deprecated in IOS 7.0 and you should use the new
code here.
[[Avaudiosession sharedinstance] Setactive:yes error:&error];
Add event handler, for this example, it's ' Volumechange: ' method
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (volumechanged:) name:@ " Avsystemcontroller_systemvolumedidchangenotification "Object:nil";
-(void) volumechanged: (nsnotification *) notification
{
Service logic here.
}
-(void) dealloc
{
[[Nsnotificationcenter Defaultcenter] removeobserver:self name:@ "Avsystemcontroller_ Systemvolumedidchangenotification "Object:nil";
}
This way, each time the user adjusts the volume using the hardware button, the logic you write is also executed.
All of the above solutions, except for the first one, are unofficial, hack-nature methods, but none of them call the private API, so there is no risk of being rejected by Apple.
Original address: http://segmentfault.com/a/1190000002401961
iOS to adjust the system volume