简体   繁体   中英

How to make a slider volume controller on iOS? Objective-C, iOS 7 (or 8), xcode 5 (or 6)

I am learning to develop iphone apps and I have a simple question.

I am making a really simple app which allows the user to set the volume of the device on the settings screen using a slider instead of the volume buttons.

It may be quite obvious for experienced developers but I just can't find the answer I am looking for.

My question is:

How can I make this slider?

Basically I want to do this (image: https://www.dropbox.com/s/b81bzdwyrab0ha7/slider.png?dl=0 ):

I used to think that I could use a regular UISlider but some documentation says that I need to use something called a MPVolumeView. The problem is that I can´t find a good tutorial that tells how to use this explicity.

I found this link on the apple documentation but I dont understand how this works. http://developer.apple.com/iphone/library/documentation/MediaPlayer/Reference/MPVolumeView_Class/Reference/Reference.html And the description says: Use a volume view to present the user with a slider control for setting the system audio output volume, and a button for choosing the audio output route. When first displayed, the slider's position reflects the current system audio output volume. As the user drags the slider, the changes update the volume. If the user presses the device volume buttons while sound is playing, the slider moves to reflect the new volume.

This is exactly what I want but I dont understand the example it gives. If you could please tell me how t make this slider work, even if it is on a new one-view project, I would really appreciate it.

Also I am aware that this method also creates an airPlay button next to the volume slider. Is there a way to delete this button? (My app is a simple productivity app so I only want the slider, not the airplay button).

I also understand that this will not work on the simulator. How am I supposed to know that this will work without having to test it on an actual device (I am not registered as a developer yet)?

Plus, I only know objective-c, not swift . If you could please restrain from using swift in your answers I would apreaciate.

Thank you so much for your help.

PD: English is not my first language. Sorry for any mistakes I made.

The MPVolumeView class is designed to let you do exactly this. It's in MediaPlayer.framework, so add that to your app to make things build correctly.

You create it and make it visible the way you instantiate any other subclass of UIView, which you probably know by now.

You disable the routing button by setting the "showsRoutingButton" property to false.

"How am I supposed to know that this will work without having to test it on an actual device?"

By seeing that it's been there since iOS 2.0, and is used in countless apps?

The process to writing such a slider is incredibly simple, look into UISlider ( https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISlider_Class/ ) and then use the float value from the slider to set the volume.

If you do not want to write your own slider, look on GitHub( github.com ) for controls that do this for you. After doing a quick search, I found this UISlider subclass that adjusts volume .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM