Hardware Volume buttons change in app volume - iphone

So in my app i would like to change the in app volume leves for an alarm by the use of the hardware buttons but whenever i use the buttons to turn up or down the volume it ONLY changes the "ringer" volume wich does NOT effect my in app volume.
Under Settings -> Sounds the "change with buttons" switch is ON and everything works fine if i turn it off but most users will want to have it on as well. So when im in my app i want the volume buttons to change the app volume not the ringer volume.
Hope it makes sense
Thanks

By default the hardware buttons will change the alarm volume unless you have an open audio session when they use the buttons. I would recommend opening an AVAudioSession to have them change it or placing a volume slider somewhere in your app to have them change the volume.
This is a difficult problem to solve perfectly because users aren't told what is wrong and many times don't look to see that the ringer volume is what is being changed.

Use MPVolumeView!
If you add an MPVolumeView to your UIWindow (you can make it hidden), the MPVolumeView will automatically take over the hardware buttons for you. The hardware buttons will now affect your app's volume levels instead of the system's.
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero];
volumeView.showsRouteButton = NO;
volumeView.hidden = YES;
[self.window addSubview:volumeView];

Related

How to programmatically silence the ringer or change the ringer tone on iOS5

Is there a way to reduce the volume of the ringer or change the ringer tone on iOS5?
I checked out the Celestial framework, but can't instantiate AVSystemController. :-(
I don't care if this get's the app rejected in the AppStore as it will never be submitted.
Yes. Use AVSystemController. You don't instantiate it. It's a singleton.
http://code.google.com/p/iphone-dev/source/browse/trunk/include/include/Celestial/AVSystemController.h?r=136
[[AVSystemController sharedAVSystemController] setVolumeTo:10.0 forCategory:#"Ringtone"];
This might be what you are looking for: Override ringer volume in iPhone apps
Basically, you set the volume using AVAudioPlayer on the -(void)viewDidLoad method of the view you are displaying on

Change iPhone application volume without volume changed box appearing (app for Japan)

I am making an Augmented Reality application that has picture taking functionality. It uses a custom function of mine to create a UIImage to save the screen. By law in Japan, cameras must have a shutter noise, which is why the iPhone camera always plays it. So far I have found a way to play sounds even when the iPhone is muted but it still relies on the user set volume. So I found a way using MPMusicPlayerController to control the application volume. This works, but when the volume is changed a box pops up signaling that the volume was changed.
Here is my code to play sounds even when muted:
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),&sessionCategory);
I use the library Finch to play the sound (a light wrapper for openAL) and then MPMusicPlayerController to adjust the volume before play.
appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[appMusicPlayer setVolume:0.5f];
Anyone have experience with this or have made apps like this for Japan? I'm really at a loss. Thanks.
The MPVolumeView will, while visible, block the floating box, even if the user can't actually see it.
Some sample code…
// create/synthesize ivars for "MPVolumeView" and "UIView" (both are necessary)
// I called them "mpVolumeView" and "mpVolumeViewParentView" respectively
// the UIView containing the MPVolumeView can have a frame of (0,0,1,1)
// this way, the user never sees the slider, but it still works normally
- (void)viewDidLoad {
...
// with this, only the slider is visible
mpVolumeViewParentView.backgroundColor = [UIColor clearColor];
// initialize the volume slider (link the parent view in IB, or init here)
mpVolumeView = [[MPVolumeView alloc] initWithFrame:
mpVolumeViewParentView.bounds];
// since it's a programmatic init, the subview must be added like so
[mpVolumeViewParentView addSubview:mpVolumeView];
// allows the floating box to appear without destroying mpVolumeView
mpVolumeView.hidden = YES; // or [mpVolume setHidden:YES]; if you prefer
...
}
Before changing volume to force the camera to make sound…
mpVolumeView.hidden = NO; // view visible, box doesn't appear
And after sounds, so it doesn't look like you messed with anything…
mpVolumeView.hidden = YES; // view hidden, box appears
It might take some tweaking to get what you want, but it should be a good starting point.
This code is for iOS 5.1
I don't know what the compatibility is with older versions.

programmatically off the sound on tapping mute button on iphone

How to programmatically disable the sound when mute button of iphone pressed while playing audio file?
I'm using streamer for audio.
This may help you
Now, here is something from MediaPlayer framework that we are going to use if we want in most easiest way control level of volume in our application. This is very useful if you are implementing an audio player in your app.
The best thing about this small feature is easy implementation in any class. We just import MediaPlayer framework in header of our class (#import ) and add this code below in method we know that is appropriate for this feature (init method).
MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)] autorelease];
volumeView.center = CGPointMake(150,370);
[volumeView sizeToFit];
[self.view addSubview:volumeView];
This kind of volume control is connected with iPhone hardware volume buttons. You get same thing like in Music player.
You have to use the audio framework of the iOS SDK and choose the correct profile. The system automatically decides if muting is appropriate.
Apple explains it here. :-)
probably you can use ....
if you are using MPMoviewPlayerController and intend to control the volume using mpvolumeview
[[MPMusicPlayerController applicationMusicPlayer] setVolume: 0.0];

Changing the volume without a volume slider on an iphone

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

MPMoviePlayer Audio Album art

I am streaming an MP3 file using MPMoviePlayer, and I would like to have an image displayed instead of the default Quicktime logo in the background.
I found out a way to have an image overlay the player, but the problem with that, is you have to tap outside the image to get the player controls to appear. And when they do appear, they are underneath the image.
Does someone know how to do this?
Thanks,
John
backgroundColor is deprecated, and diving into private View structures is dangerous. This worked fine for me:
UIImageView *coverImageView = [[UIImageView alloc] initWithImage:coverImage];
coverImageView.contentMode = UIViewContentModeScaleAspectFit;
coverImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
coverImageView.frame = moviePlayerController.view.bounds;
[moviePlayerController.view addSubview:coverImageView];
Most every app on the iPhone is made of a hierarchy of views. You could go up to the top root node of the movie player and walk down the child views recursively and set the hidden value to YES until you find the right item, then insert your UIImageView below that item. That way, the controls stay on top and still respond to user inputs.
The risk you run is if Apple changes the MPMoviePlayer UI and shuffles the view hierarchy, but you'll probably have lots of advance notice and can patch your app to allow for the new layout.
There is a question as to whether this is kosher for appstore apps, but many current apps (especially camera/picture-taking ones) are doing it to get rid of standard window elements.
Use AVAudioPlayer and implement your own player UI.
it will work check this
MPMoviePlayerController *moviePlayerController=[[MPMoviePlayerController alloc] initWithContentURL:theURL];
moviePlayerController.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Default.png"]];