Changing the volume without a volume slider on an iphone

12,257

Solution 1

I would be careful calling setValue on an MPVolumeView since it probably won't do anything other than update the appearance of the slider, but not the actual device volume level. You would instead have to call _commitVolumeChange which is private API and will likely get your app rejected.

A short answer to how to control volume: It really depends on what you're trying to control the volume of.

If you want a "controls every sound within the app" sort of control then you can use an MPVolumeView but you cannot change it's value programmatically. You would then only be able to change the volume by either moving the slider with a touch or using the volume buttons on the side of the device. The best thing to do is create a global object that stores the volume level which any of your objects can read before they play their sound.

If it's an AVAudioPlayer object, you'd create the object and use [theAudioPlayerObject setVolume: someFloat]; where someFloat is a value between 0.0 and 1.0.

If it's a SystemSound object, you can't control volume.

If it's an AudioQueue you'd change it via AudioQueueSetParameter

Like I said.. it all depends on how you are playing the sound.

Update based on comment

For that particular example, you would set the volume like this:

Add to the AudioStreamer.h file

- (void)setVolume:(float)Level;

Add to the AudioStreamer.m file

- (void)setVolume:(float)Level
{

    OSStatus errorMsg = AudioQueueSetParameter(audioQueue, kAudioQueueParam_Volume, Level);

    if (errorMsg) {
        NSLog(@"AudioQueueSetParameter returned %d when setting the volume.", errorMsg);
    }

}

Add to the view controller for where the volume knob will be (this goes in the .m file.. i just did this as a couple UIButtons real quick, you'll have to do your own) and set up an IBAction to change the volume for a given value (you can pass in 0.0 thru 1.0 as a float)

- (IBAction)volumeUp:(id)sender
{

    [streamer setVolume:1.0];

}

- (IBAction)volumeDown:(id)sender
{

    [streamer setVolume:0.0];

}

Solution 2

I view this as a bug in Apple's code and have reported it to them both with Bug Reports and in person, but since they insist its a feature, you might as well benefit from it.

Use the following code to change your application's volume:

[[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolume];

This will only work after you have established your audio session, either by playing a sound or by setting it active as such:

[[AVAudioSession sharedInstance] setActive:YES error:NULL];

Note as that you'll need MediaPlayer.framework and AVFoundation.framework and that the volume is between 0.0 and 1.0.

Solution 3

Well, take the min Rotation (R1) and max Rotation (R2). Then do rotation / (R2 - R1) to get a % like a slider does.

EDIT:
To commit the volume change, add the following:

MPVolumeView *systemVolumeSlider = [[MPVolumeView alloc] initWithFrame: self.view.bounds];
[systemVolumeSlider setHidden:YES];
[systemVolumeSlider setUserInteractionEnabled:NO];
[self.view addSubview:systemVolumeSlider];

(Make sure to release systemVolumeSlider in dealloc)

Then, when the volume is changed, use setValue to set its value. You will also need to handle what happens when your user presses volume +/- buttons on the device.

Share:
12,257
awlcs
Author by

awlcs

Updated on June 03, 2022

Comments

  • awlcs
    awlcs almost 2 years

    I need your help. How should I proceed to change the sound volume in my app. I don't want to use a volume slider. Instead I have an UIImageView which is a volume knob, in which I rotate clockwise to increase, and anti clockwise to decrease the sound volume. The rotation is just an animation and I've already done that part.

    I need your help and advice on how to increase/decrease the volume. Thanks

  • iwasrobbed
    iwasrobbed over 13 years
    You might also want to tell them how to change the volume for a complete answer :)
  • awlcs
    awlcs over 13 years
    thanks jrtc & iWasRobbed :D jrtc: lol my question was, after having the %, what's next, I'm stuck here xD
  • jrtc27
    jrtc27 over 13 years
    Yeah sure - I skimmed through your question and thought I had answered it, but hadn't quite! Answer has been updated though ;)
  • iwasrobbed
    iwasrobbed over 13 years
    I would be careful using MPVolumeView... you cannot change the volume programmatically without calling _commitVolumeChange which is private API.
  • jrtc27
    jrtc27 over 13 years
    Apparently it automatically updates since iOS 3.0 - fraoula.com/?p=124
  • jrtc27
    jrtc27 over 13 years
    And you can also use [MPMusicPlayerController setVolume:]
  • iwasrobbed
    iwasrobbed over 13 years
    Right, but keep in mind that's only if you use the volume control buttons on the side of the actual device. Lemme put it this way: if it's not buried in the class reference, you probably can't do it using public api :)
  • awlcs
    awlcs over 13 years
    I want to control the volume of my app. I'm trying to make an app using matt gallagher classes on how to stream an mp3 source cocoawithlove.com/2008/09/…
  • awlcs
    awlcs over 13 years
    Thank you very much rob, I will try it :D I'm quite new to iphone programming :D thanks :D
  • iwasrobbed
    iwasrobbed over 13 years
    Not a problem, if you have issues implementing this, let me know and I'll post a sample project. Try it on your own first, you'll learn more that way.
  • awlcs
    awlcs over 13 years
    hey rob, is there a simple way to record what has been streamed with matt gallagher audio classes. I mean i want to record while playing simultaneously.
  • iwasrobbed
    iwasrobbed over 13 years
    Sorry awics, you'd have to start a new question for that. Stack Overflow is a one-question-per-post site so things stay organized for later readers looking for answers
  • awlcs
    awlcs over 13 years
    okay rob :D could you help me please with this? :s I created a new post stackoverflow.com/questions/3501227/…
  • alones
    alones over 13 years
    Simple and clear~~ :) Thanks a lot. Even if I don't use MPMusicPlayerController (e.g. using MPMoviePlayerController) I can use it. Due to AVAudioSession. :)
  • JohnK
    JohnK almost 11 years
    But what if you're using AudioUnit instead?
  • Ashton Honnecke
    Ashton Honnecke about 10 years
    'setVolume:' is deprecated in iOS 7.0