11

I want the user to be able to change the system volume with a slider, and I realized the only way to do this is with an MPVolumeView.

But I can't find any example code for it, and every method I try to implement won't show up.

So what is the easiest and correct, working way of implementing a MPVolumeView?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Emil
  • 7,220
  • 17
  • 76
  • 135

3 Answers3

11

Place it as a generic UIView, then use the inspector to set the class to MPVolumeView (ensuring that you also link the MediaPlayer framework). It'll still be shown as a regular slider in IB, but at runtime, it will be an instance of MPVolumeView and will have the necessary styles and behavior. Note that this may not work as expected in the iOS Simulator, which doesn't permit volume control.

warrenm
  • 31,094
  • 6
  • 92
  • 116
  • Thank you, it works now, I only need help placing it in a UIAlertView now.. http://stackoverflow.com/questions/2829234/mpvolumeview-in-a-uialertview – Emil May 15 '10 at 19:31
  • It works fine by changing a UISlider just make sure to add the MediaPlayer.framework or else it will just show as a UISlider – valexa Apr 04 '12 at 13:35
  • 1
    Thanks for the help. Its really annoying that it does not work on the simulator. If you want to always hide the button at the right (route button), add also myVolumeView.showsRouteButton = NO; – AlvaroSantisteban Mar 12 '14 at 15:49
3

Use this it will automatically get it

mpVolumeViewParentView.backgroundColor = [UIColor clearColor];
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview: myVolumeView];
[myVolumeView release];
Emil
  • 7,220
  • 17
  • 76
  • 135
mondal
  • 91
  • 6
  • I dont see much help on copying the code from apple´s documentation when someone is asking for an example... https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPVolumeView_Class/Reference/Reference.html#//apple_ref/occ/instp/MPVolumeView/showsVolumeSlider – AlvaroSantisteban Mar 12 '14 at 15:38
  • 2
    It's very helpful, since you shouldn't post links but solutions. This code will still be here in ten years time (maybe), the link could be invalid next week. – gnasher729 May 11 '15 at 18:52
  • Wait. But by that logic, what if this link is invalid in ten years? :O. THE INTERNET IS BROKEN! :O. FYI 5 years later and the link is just fine, except that unlike this example, it's been updated and maintained. – KthProg Apr 02 '19 at 19:35
  • ^ There's no guarantee that a 3rd party link will remain valid. – jcpennypincher Nov 20 '20 at 21:08
2

In iOS 13 this has changed. Adding a slider in IB with its class set to MPVolumeView doesn't work anymore. So the accepted answer no longer works. The right way, as outlined in the Apple docs, is to use a UIView in IB and then in code add the MPVolumeView as a subview. Here's how in Swift:

// myVolumeViewParentView is the UIView you put in IB
let myVolumeView = MPVolumeView(frame: myVolumeViewParentView.bounds)
myVolumeViewParentView.addSubview(myVolumeView)

This method works in iOS 12 too.