iOS: Change Device Volume
Asked Answered
T

3

24

Is there a way to change the volume of the device? I've seen several apps do it.

I have a desktop version of the iOS app and the device will be able to be controlled to some extent over the network. One of the things I want to allow the user to do is change the device volume and then play a sound. This can help if you loose your iPhone in a crack in your couch again, but can't find it.

Is there any way that you can do this without Apple getting angry?

Tran answered 1/2, 2011 at 5:53 Comment(0)
O
32

Using iPodMusicPlayer would affect the actual iPod volume setting as well. If you want to avoid that, use this:

#import <MediaPlayer/MediaPlayer.h>
// ...
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = 1.0f; 

As the user holex correctly mentioned the property volume in MPMusicPlayerController is deprecated in iOS 7.

Outwardly answered 3/5, 2012 at 23:59 Comment(2)
Remember to add MediaPlayer.framework to your target's linking phase, and #import <MediaPlayer/MediaPlayer.h> in your source file.Catamount
it can be important to know the volume property is deprecated in iOS7. (further info here: developer.apple.com/library/ios/documentation/MediaPlayer/…)Fda
S
23

You can use a little trick:

  MPMusicPlayerController* musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
  musicPlayer.volume = 1; // device volume will be changed to maximum value
Satyr answered 25/8, 2011 at 11:32 Comment(2)
Use systemMusicPlayer instead of iPodMusicPlayer for IOS8+Vinny
The volume property is deprecated starting with iOS 7.0. You can check that here.Polypropylene
D
15

You cannot change device volume programatically, however MPVolumeView (volume slider) is there to change device volume but only through user interaction.

MPVolumeView is a control in toolbox, you need to add MediaPlayer.framework in your project then MPVolumeView will be displayed in toolbox in interface builder.

Edit 1: MPVolumeView uses the device volume which is also used for ringing volume. AVAudioPlayer is there if you want application level volume. In this case you can use volume property to set your application volume (not device volume) programatically. However, you can use UISlider control to get volume input from user and set to your AVAudioPlayer

Dyslexia answered 1/2, 2011 at 6:29 Comment(5)
Also note that it's only available in iOS 4.2, so you'll have to include version/class availability checking before you use it.Barrage
Nopes, a new property added in it to visible/hide it, the new property is only available in iOS 4.2, however I have uses it in iOS3.2, here I used it (iOS 3.2 or later), itunes.apple.com/us/app/msiradio/id399408266?mt=8Dyslexia
Hmm, that's a sort of setback. Would I be able to play a sound at a different volume than the ringer or system volume? I've seen quite a few apps do it already.Tran
I currently use the AVAudioPlayer volume property, tied to a slider value. Works perfectly.Suborder
@seeafish, I am not sure until you do +1 :)Dyslexia

© 2022 - 2024 — McMap. All rights reserved.