Objective-C Troublesome MPVolumeView
April 19, 2009 § 1 Comment
This is probably the only way to control the system volume. However MPVolumeView is poorly documented with little flexibility.
MPVolumeView is part of MediaPlayer. If you use the MediaPlayer to play the audio, it calls up a stand-alone screen.
Below is a work around to use MPVolumeView to control the system volume for the app, not specific to play a single audio file.
Add framework MediaPlayer.framework to your project.
Add this line to .h header file:
Add code to .m source file:
volumeView = [[[MPVolumeView alloc] initWithFrame:CGRectMake(200,100,100,200)] autorelease];
Basically when the users change volume, the system volume will change immediately. It provides you the UISlider so you don’t have to create UISlider object.
However the application is very limited. First it won’t work at all for landscape orientation. No matter how you customize it with UISlider properties and rotate it, it won’t work. Secondly, avoid to put it to – (void)viewDidLoad method on the MainViewController, it would crash.
Putting the MPVolumeView on the main app is not a good idea. Many apps leave the volume control on views other than the MainView.
From researching around, I’ve found a Notification class you can use for volume change. I should alert that it is not tested.
[[NSNotificationCenter defaultCenter] addObserver:self
– (void) volumeChanged:(NSNotification *)notify