Question

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

Was it helpful?

Solution

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];

}

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top