Set device volume on iPhone - iphone

I'm writing an app that uses AVAudioPlayers to play sounds and I need the sounds to play at absolute MAX volume. Yes you can set the relative volume for an AVAudioPlayer, but if the user has the device volume turned down, the sound will still play quietly.
I know that Apple says that the device volume cannot be overridden, but yet the Alarm Clock apps out there somehow are able to do this - so there must be a way.
I believe that this can be done using Audio Queues using the call:
AudioQueueSetParameter(aq, kAudioQueueParam_Volume, 1.0);
But my code needs to use the AVAudioPlayer to play the sounds so I don't think this approach will be of any help.
There is also a way to use an undocumented function of MPVolumeView to accomplish this, but apparently someone already had an app rejected for using this method.
Any other ideas?

The comment from the anonymous for this site:
float volume;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:volume];
does actually work.

I came across this:
MPVolumeView *volumeView1 = [[MPVolumeView alloc] initWithFrame:CGRectMake(4, 316, sk.size.width, sk.size.height)];
[volumeView1 sizeToFit];
[self.view addSubview:volumeView1];
[volumeView1 release];
I'm not entirely sure if it will work, but I believe it should. It seems you might have to modify the height and width

Related

Programmatically make the volume full from mute

I am working on iphone app. I want to tap a button for playing an .wav file. then I want to check if the volume of iphone is mute then make it louder and play the .wav file. Is it possible programmatically?
I think you can do this....
http://iphoneincubator.com/blog/tutorial/how-to-play-audio-with-the-iphone-sdk
this link for audio play..
when you are able to play sound,then for mute the sound just write soundname.volume = 0;and when you want to up the volume then just write soundname.volume=5
I believe not. iOS only allows the "user" to change settings like these.
Even if it was possible, I would highly doubt if it would pass the App Store terms of publication.
You can try to check the volume by,
[[MPMusicPlayerController iPodMusicPlayer] volume];
set the volume by,
[[MPMusicPlayerController iPodMusicPlayer] setVolume: value];

How to tell in code if the user has "Locked the Volume" in their settings menu

Currently, I'm setting the volume to max, and then checking if the volume is at max, or a lower value. If it's at a lower value, then the user must have a Volume Lock on their system.
This works fine, but I'm wondering if there is some method to call, or property to check which tells me this in code for free? I've been looking online as to how to do this, but I can't seem to find anything. Thanks in advance!
Edit: It turns out that my previous method of setting the max volume and then checking if it's lower to see if there is a Volume lock does not work on the device. It seems as though the volume is scaled with the Volume lock, instead of just being cut off.
Now I'm completely stuck on this. Is there even any private methods or properties that I can use in order to detect this?
I think what you are asking for is to find out if the iphone has a cap on the volume limit. I have looked for the answer but could not find one. Here is a way just to check the volume level, hope this helps.
In your XIB you can add a slider to check what the volume level is at, so basically you can tell if it is silent, and know the level of the volume. For more understanding of this class, here's the link http://blog.stormyprods.com/2008/09/proper-usage-of-mpvolumeview-class.html but try this first:
The following code will create something like a volume bar.
- (void)viewDidLoad {
// create a frame for MPVolumeView image
CGRect frame = volumeViewHolder.bounds; // CGRectMake(0, 5, 180, 0);
volumeView = [[[MPVolumeView alloc] initWithFrame:frame] autorelease];
[volumeView sizeToFit];
[volumeViewHolder addSubview:volumeView];
for (UIView *view in [volumeView subviews]){
if ([[[view class] description] isEqualToString:#"MPVolumeSlider"]) {
volumeViewSlider = view;
}
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(volumeChanged:)
name:#"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
}
- (void) volumeChanged:(NSNotification *)notify
{
[volumeViewSlider setValue:[[[notify userInfo] objectForKey:#"AVSystemController_AudioVolumeNotificationParameter"] floatValue]];
}
I heard that for some reason apple doesn't allow you to sell an app if you use a certain class (the one in my example) but I'm not too sure about this, I would double-check and make sure that you are 'allowed' to use it. But the code should work.
There are many answers to this problem but they all seem to have fallen foul of Apple's guidelines or App Store curators at some point as Apple don't provide an official way to do this.
However one feature you can utilise to find this out is that when a device is muted then the OS will not play the sound at all - so if you play a 800ms sound file and you time how long it takes to play if it is less than 800ms then you can infer that the mute switch is on.
I would recommend to use a silent 800ms sound file to use this trick so that the user is not suddenly confronted with an unexpected sound.
See here for the article that inspired me: http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/
In addition you might want to use the Audio Toolbox to do this simply and guarantee that the mute switch is being used.
Consider setting the volume to max, then recording the output channel, and playing a short tone - then compare the peak of that recorded sample to a known sample where the volume limiter is not present. This will work if the iPad scales the output volume before recording gets access to it.
I wish I had more info on how to record the output, but I don't have much personal experience with it. Somebody who is versed in recording input should be able to point you the right way.

How to disable iOS System Sounds

I am working on an iPad app that connects with an accessory that plays sound. When the iPad is connected to the accessory, I would like to mute all system sounds but allow other sounds (iPod).
Part of the reason for this is that the accessory is such that it is intended to be used during a live performance. Clearly it would be annoying to have e-mail, alert, or any other system sound running through and amplified (crazy loud).
I have looked at using AVAudioSession (read Audio Sessions to learn more) and tried all of the AudioSessionCategories. None of these categories will mute the system sound, instead it will only allow you to mute application sounds (iPod) - not useful for my purposes.
I also found docs on "System Sound Services", but this only allows you to play system sounds. There is no api here to disable system sounds while your app is running.
A final note, we have made it easy to adjust the iPad level (volume) by including the MPVolumeView, but we expect the user to want to play iPod music. If while playing iPod music (or music from another app) and an e-mail comes through, you'd be amazed how LOUD / ANNOYING that e-mail suddenly becomes when going through our accessory. It's even possible it could damage equipment. :D
It is possible to change the system sounds, which turns out to be the ringer btw, using the AVSystemController. However, AVSystemController exists in the private Celestial framework. Since this framework is referenced by UIKit, it is still possible to use this class without directly referencing it.
Apple prohibits using private API's, so that alone makes this a bad idea. Given my circumstance, I think they may make an exception, BUT I will likely abandon this course since after taking it I realized that it didn't fix my problem. It does indeed mute the sounds, but as soon as I plug in to my accessory, the system sounds come out at max volume even though the ringer volume is set to 0. This leads me to believe the answer to solving my problem is in the MFI documentation.
Anyhow, here is how to change the ringer using private framework / api (which will get your app rejected without some kind of special permission).
short answer:
[[AVSystemController sharedAVSystemController] setVolumeTo:0 forCategory:#"Ringtone"];
answer without having to directly reference Celestial frameork / AVSystemController.h:
- (void) setSystemVolumeLevelTo:(float)newVolumeLevel
{
Class avSystemControllerClass = NSClassFromString(#"AVSystemController");
id avSystemControllerInstance = [avSystemControllerClass performSelector:#selector(sharedAVSystemController)];
NSString *soundCategory = #"Ringtone";
NSInvocation *volumeInvocation = [NSInvocation invocationWithMethodSignature:
[avSystemControllerClass instanceMethodSignatureForSelector:
#selector(setVolumeTo:forCategory:)]];
[volumeInvocation setTarget:avSystemControllerInstance];
[volumeInvocation setSelector:#selector(setVolumeTo:forCategory:)];
[volumeInvocation setArgument:&newVolumeLevel atIndex:2];
[volumeInvocation setArgument:&soundCategory atIndex:3];
[volumeInvocation invoke];
}
Using MediaPlayer framework, we can set the level of SYSTEM sound
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0];
Best you can do is encourage your users to go into airplane mode.

Programmatically powering off an iPhone?

Is it possible to programmatically power off an iPhone or does Apple disallow this?
If Apple disallows this, is it possible to programmatically mute the sound on an iPhone?
The iPhone applications you create with the official SDK are sandboxes in and of themselves. Walled off sandboxes with barbed wire.
You won't be able to turn off the power. And muting sounds other than your own applications' sounds amounts to being able to turn off the iPod playback.
I don't have any evidence for that, but this would involve modifying the "UserExperience" - which is something that Apple never would allow (and why still many people jailbreak their phones).
And this involves "power off" as well as "mute sound" - because both could destroy the UX (you wait for an important call, but application X broke the sound).
Is it possible to programmatically power off apple iPhone or does apple dissalow this. If apple disallow this is it possible to programmatically mute the sound on iPhone?
Apple prevents you from affecting the functionality of other apps and the core phone functions. When in doubt, if you want to do something phone-wide, you can't.
Plus, to mute the phone, you'd also have to figure out some way of making the physical mute switch on the side of the phone match the phone's mute setting. That's not going to happen with software!
I'm not sure how powering down the device and muting the device are reasonable alternatives in your app, but the bottom line is that you can't power down the device. However, you can mute the sound of your own app or the iPod app using the MPMusicPlayerController class.
The code looks like this for your app:
MPMusicPlayerController *player = [MPMusicPlayerController applicationMusicPlayer];
player.volume = 0.0f;
And, this for the iPod:
MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
player.volume = 0.0f;
Anything you do that affects anything external to your application wont make it through the approval process (besides push notifications). You can certainly mute the sound in your app by simply pausing, stopping, or setting the volume to zero for all sounds you are playing. If you mean make the phone be mute globally, no.
You can't turn the device off through software. You can set the music playback volume with the MPMusicPlayerController class, the docs suggest you can't change the volume of the
iPod player though.

Is it OK to mix AudioServicesPlaySystemSound and AVAudioPlayer?

We have a game that is very close to being released, but we have one extremely annoying problem. One on of our Beta testers phones, he can't hear any of the in game sound effects. He can, however, hear the background music and the title screen music just fine.
The background and title music are both being played via AVAudioPlayer (they are longer, we need looping and volume control, etc). The sound effects are simply being played with AudioServicesPlaySystemSound (they are very short, we don't need precise control or to know when they end, etc). This works on most iPhones, but not on this one. All of this is being played with an audio session of AVAudioSessionCategorySoloAmbient.
So I have two questions:
- First, is this an acceptable implementation? i.e. is there something I missed that says you can't mix these two frameworks, or a reason why its a bad idea to mix them?
- Second, has anyone seen something like this before? If so, did you find a way around it?
Additional background note: I can pretty conclusively say that on his phone, it is the mixing of the two frameworks. He was able to hear sounds until roughly the same build where we added the title screen music. Also, if I change one of the sounds to work through an AVAudioPlayer, he's able to hear it. Unfortunately, I can't simply move the sounds into AVAudioPlayers because it just doesn't perform well at all, and I need better synchronization.
EDIT 2:
I've determined the cause of the error. The two different audio frameworks play on two different volume settings. There is the main volume (that's what shows up once you are in the app, and has no title on it) and the ringer volume (that's what shows up when you're on the home screen without any app loaded). The AVAudioPlayer calls play with the main volume setting. As I have set the category to AVAudioSessionCategorySoloAmbient (see code below), this is the volume control that will be adjusted if you use the volume rocker inside the app. The SystemSounds, however, are played at the Ringer Volume. This will NOT come up inside the app, and as such does not allow the user to adjust their settings during the game.
It's easy to see the possible confusion for the user: Let's say they have their ringer set to low, or even off. They start playing the game. They hear the title screen music (AVAudioPlayer) and are able to turn the volume up or down and it responds normally. Then they start the game and the sound effects play (SystemSounds), and they hear nothing because the ringer volume is low/off. In an effort to hear the sound effects, they bump up the volume and the background music responds accordingly. So from this point of view, it definitely looks like the sound effects just aren't playing.
If you have this similar situation, have your user make sure that their ringer volume is up before playing the game and their mute switch is on (i.e. not muted). You can also verify all of this by doing the test yourself - drop your ringer volume and bump your in-app volume up. Your SystemSounds should drop out of the mix.
Original answer:
In re-digging through all of the docs, I found the answer. I wasn't properly activating the audio session. Previously I only had this code to set the category:
NSError *setCategoryError = nil;
BOOL categoryWasSet = [[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategorySoloAmbient
error: &setCategoryError];
But I needed to also add the following code to explicitly activate the audio session:
NSError *activationError = nil;
BOOL activationSuccess = [[AVAudioSession sharedInstance]
setActive: YES
error: &activationError];
I've re-tested with this user, and it has completely fixed the issue. Hope that helps anyone who might have a similar problem.
EDIT: This doesn't seem to have resolved the issue after all. I got a false positive from my tester, and when examined more in detail it seems that he didn't actually hear the SystemSound generated sounds.
I have two apps on the app store that mix those two frameworks. AVAudioPlayer for sounds that require start and stop and volume control and AudioServices toolbox for short little beeps and clicks. The only think I can think of, off the top of my head, is that the volume of AVAudioPlayer seems to be controlled separately from the AudioServices sounds. So check to make sure that the tester doesn't have sounds turned off (with the vibrate mode switch) or the volume turned way down.
If this doesn't help, tell me more about your sounds/code and I can add some code to my app to see if I can get the same behavior. Of course, sounds like most your testers don't have this problem, and I probably won't see it either.