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?

User97693321
  • 3,336
  • 7
  • 45
  • 69
Tristan
  • 3,058
  • 6
  • 40
  • 68

3 Answers3

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.

Community
  • 1
  • 1
ceriseche
  • 548
  • 6
  • 14
  • 5
    Remember to add `MediaPlayer.framework` to your target's linking phase, and `#import ` in your source file. – Andreas Ley May 27 '13 at 08:53
  • 6
    it can be important to know the `volume` property is **deprecated** in iOS7. (further info here: https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMusicPlayerController_ClassReference/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instp/MPMusicPlayerController/volume) – holex Jan 30 '14 at 17:25
23

You can use a little trick:

  MPMusicPlayerController* musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
  musicPlayer.volume = 1; // device volume will be changed to maximum value
JohnK
  • 6,865
  • 8
  • 49
  • 75
  • Use systemMusicPlayer instead of iPodMusicPlayer for IOS8+ – Kevin Oct 30 '15 at 09:15
  • The `volume` property is deprecated starting with iOS 7.0. You can check that [here](https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMusicPlayerController_ClassReference/#//apple_ref/occ/instp/MPMusicPlayerController/volume). – Alex Feb 29 '16 at 16:36
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

Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
  • 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. – Aurum Aquila Feb 01 '11 at 06:32
  • 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), http://itunes.apple.com/us/app/msiradio/id399408266?mt=8 – Waqas Raja Feb 01 '11 at 06:45
  • 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. – Tristan Feb 01 '11 at 15:23
  • I currently use the AVAudioPlayer volume property, tied to a slider value. Works perfectly. – seeafish Aug 12 '11 at 08:14
  • 1
    @seeafish, I am not sure until you do +1 :) – Waqas Raja Aug 12 '11 at 09:12