What's the best way to use a UISlider to change the volume of the iPhone in an app?
Use an MPVolumeView to display a slider that allows the user to modify the system volume.
I've tried using that Bill (http://stackoverflow.com/questions/463034/iphone-sdk-uislider-to-change-volume#463970)
But nothing appears on the screen using the code:
MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:
CGRectMake(0, 0, 215, 22)] autorelease];
volumeView.center = CGPointMake(150,375);
[volumeView sizeToFit];
[self.view addSubview:volumeView];
If you want to be able to change the iPhone volume, MPVolumeView is the only option. Try the example on this link.
http://www.stormyprods.com/blogger/2008/09/proper-usage-of-mpvolumeview-class.html
http://stackoverflow.com/questions/463034/iphone-sdk-uislider-to-change-volume#465649
^ I tried that lostintransit, however this is not changing the ringer volume. It comes up on the iphone screen, but doesn't change anything. Even using the demo project provided.
I've just found a very easy way. Instead of coding, just place it in your XIB.
- Open the XIB where you want to place to slider into
- Add a UIView to your view
- Change the class identity from UIView to MPVolumeView
- Change backgroundColor to clear
Voila!
PS: Tested it on a device with iPhone OS 3.0. As lostInTransit stated before, it won't work on the simulator.
I am using this same technique, however I am noticing the following behavior:
On an iPod touch G2: The MPVolumeView slider works and fully responds to the rocker switch from app startup time.
On an iPhone Gen 1 and 3G: The MPVolumeView slider doesn't start changing the volume until media playback has occurred via the AudioQueue APIs, and will stop changing the volume when AudioSessionSetActive(false) is called. Even more bizarre: if you hit the rocker switch on the iPhone while using the AudioQueue APIs during playback then the MPVolumeView slider will work for the rest of the lifetime of the app.
This code keeps everything in sync, and tends to help with ensuring that the rocker switch and the slider stay in sync, but it doesn't eliminate the issues I am having with MPVolumeView on an iPhone.
All of my experience here is under iPhone OS 3.0 with the iPhone 3.0 SDK.
I hope this helps narrow down your problem somewhat.
My next tactic was to start doing things with kAudioSessionProperty_AudioRoute and try alternate values for kAudioSessionProperty_AudioCategory to see what happens.
Side note: Thanks to everyone on the IB techniques for creation and managing the MPVolumeView. I was using code to create and place it, and I keep forgetting about the technique where you can set the type of the UIView in IB.
Side note 2: In working with the MPVolumeView I discovered that if you set the audio category to anything other than kAudioSessionCategory_MediaPlayback that the MPVolumeView will display the text "iPhone..." when the iPhone is set to silent mode.