views:

496

answers:

3

Hello guys

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

+2  A: 

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.

jrtc27
You might also want to tell them how to change the volume for a complete answer :)
iWasRobbed
okayasu
Yeah sure - I skimmed through your question and thought I had answered it, but hadn't quite! Answer has been updated though ;)
jrtc27
I would be careful using MPVolumeView... you cannot change the *volume* programmatically without calling `_commitVolumeChange` which is private API.
iWasRobbed
Apparently it automatically updates since iOS 3.0 - http://www.fraoula.com/?p=124
jrtc27
And you can also use `[MPMusicPlayerController setVolume:]`
jrtc27
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 :)
iWasRobbed
+1  A: 

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

}
iWasRobbed
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 http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html
okayasu
See my updated answer
iWasRobbed
Thank you very much rob, I will try it :D I'm quite new to iphone programming :D thanks :D
okayasu
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.
iWasRobbed
it was just perfect iWasRobbed, thank you very much :D
okayasu
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.
okayasu
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
iWasRobbed
okay rob :D could you help me please with this? :s I created a new post http://stackoverflow.com/questions/3501227/record-simultaneously-while-streamer-is-playing
okayasu
+3  A: 

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.

Eddie Marks
Simple and clear~~ :) Thanks a lot. Even if I don't use MPMusicPlayerController (e.g. using MPMoviePlayerController) I can use it. Due to AVAudioSession. :)
alones