How do you implement an MPVolumeView?

16,804

Solution 1

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.

Solution 2

Use this it will automatically get it

mpVolumeViewParentView.backgroundColor = [UIColor clearColor];
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview: myVolumeView];
[myVolumeView release];

Solution 3

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.

Share:
16,804

Related videos on Youtube

Emil
Author by

Emil

Web developer. Currently working in Angular.

Updated on April 21, 2022

Comments

  • Emil
    Emil about 2 years

    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?

    • Joost
      Joost about 14 years
      Just add it is as a subview of some onscreen view? Should do the job I suppose.
  • Emil
    Emil about 14 years
    Thank you, it works now, I only need help placing it in a UIAlertView now.. stackoverflow.com/questions/2829234/…
  • valexa
    valexa about 12 years
    It works fine by changing a UISlider just make sure to add the MediaPlayer.framework or else it will just show as a UISlider
  • AlvaroSantisteban
    AlvaroSantisteban about 10 years
    I dont see much help on copying the code from apple´s documentation when someone is asking for an example... developer.apple.com/library/ios/documentation/MediaPlayer/…
  • AlvaroSantisteban
    AlvaroSantisteban about 10 years
    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;
  • gnasher729
    gnasher729 about 9 years
    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.
  • KthProg
    KthProg about 5 years
    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.
  • jcpennypincher
    jcpennypincher over 3 years
    ^ There's no guarantee that a 3rd party link will remain valid.