Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement an MPVolumeView?

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?

like image 419
Emil Avatar asked May 08 '10 20:05

Emil


3 Answers

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.

like image 119
warrenm Avatar answered Oct 20 '22 18:10

warrenm


Use this it will automatically get it

mpVolumeViewParentView.backgroundColor = [UIColor clearColor];
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview: myVolumeView];
[myVolumeView release];
like image 31
mondal Avatar answered Oct 20 '22 17:10

mondal


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.

like image 38
Angel Olvera Avatar answered Oct 20 '22 17:10

Angel Olvera