Disable in App sounds - iphone

I have a question regarding sounds in my app. I need the user to be able to mute all sounds coming from my game, for example if they just want to listen to the ipod while playing. There is a similar question here Disable all program sounds but there doesnt seem to be an answer. At the moment I have an AVAudioSession set to AVAudioSessionCategoryAmbient which allows the ipod to play but also will allow my app to play game sounds. Is the best way to achieve my aim to just set a boolean bhen a mute button is clicked and check this each time a sound should be played? This seems kinda awkward although it would work... any ideas please?
Many thanks
Jules

If the iPod is playing when the game begins, you can mute the sounds automatically.
e.g.,
UInt32 userPlayback;
UInt32 propertySize = sizeof(userPlayback);
AudioSessionGetProperty(
kAudioSessionProperty_OtherAudioIsPlaying,
&propertySize, &userPlayback);
BOOL userMusicPlaying = (userPlayback != 0);
Then if userMusicPlaying is YES, don't let the sounds play. (Consider also adding a switch that lets the sounds play even with the iPod playing.) Otherwise, I don't think there's a way to disable the sounds without you or the user disabling them.

Put isMuted in the user defaults
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"isMuted"]) {
playSound()....
}
to mute:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"isMuted"];
If you call the following it will write the settings to disk and remember what the user did last time:
[[NSUserDefaults standardUserDefaults] synchronize];

Related

How to mute a video on iOS while iPod still playing audio?

I'm developing a teaching app playing video animations with no sound.
I would like to prevent the movie player from stopping other audio sessions (such as the iPod) when the video is launched.
Is there any way to indicate the iOS the video has no audio or play in mute mode?
Thanks in advance for your help!
You can always detect if the music is playing and and resume it right way after the movie started playing.
Detecting player state
[MPMusicPlayerController iPodMusicPlayer] playbackState]
Resume
[[MPMusicPlayerController iPodMusicPlayer] play];
I think maybe something like this
UInt32 allowMixing = YES;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
AudioSessionSetActive(YES);
should do the the trick.
Keep in mind, you'll need to add the AudioToolbox library to your application, and add:
#import <AudioToolbox/AudioToolbox.h>
to your classes imports in order to use the above code.
It should allow your app's sounds to mix with the other app's sounds in the background. In this case, since your app is silent, it will just allow background sounds to keep playing.
Hope it helps!

How to show a video when the app loads first time?

How can I make an app play a video walkthrough (only) the first time it is loaded and then close itself and not play the next time user uses the app?
I found some instructions on showing a pop-up message but this didn't help really :/
On launch read a setting from NSUserDefaults. If not present play the video. When the video finishes write that same setting to NSUserDefaults. In the next launch the setting will be found and you can skip the video part.
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"firstLaunch"];) {
// play the video
// when done:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"firstLaunch"]
}
Not sure if you're asking how to play the video too. If that's the case, you may want to split the question in two and be more specific.

AVAudioSession category not working as documentation dictates

I have an iOS app that has some audio feedback in certain places, but I want any other music the user has playing in the background to be allowed to play over this. In addition, I want the audio in my app to respect the mute switch. According to the developer documentation, this functionality should all be enabled by the AVAudioSession ambient category. This is the code I'm using:
if (!hasInitialisedAudioSession) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:NULL];
[session setActive:YES error:NULL];
hasInitialisedAudioSession = YES;
}
The code is executing just fine, and it does indeed let the app sounds play over iPod music. What it doesn't do, however, is respect the mute switch. I've tried swapping this code out for similar C audio calls (stuff like AudioSessionSetProperty) instead of the Objective-C calls, but I get the same result - the ambient session category simply doesn't want to respect the mute switch, despite what the documentation says it should be doing.
Any ideas? Thanks for the help :)
I think I managed to work it out - turns out that it has nothing to do with my app at all, but rather the iPod app. My app obeys the mute switch as it should when the iPod isn't playing, and then allows the iPod to play over it - all behaviour I wanted. However, when the iPod is playing, the app stops responding to the mute switch, so I think it's just something the iPod does to the device audio settings. I could probably work a way around it if I really wanted to spend the time on it, but as long as it obeys the mute switch when the iPod isn't playing that's good enough for me.
EDIT: to work around this, just use this function to determine whether or not the mute switch is on manually, and don't play your sounds if the result is YES. Could be a bit of a pain if you don't have a central audio manager class, though. It would be nice if Apple could publish this behaviour in their documentation.
- (BOOL)deviceIsSilenced
{
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) <= 0);
}

MPMusicPlayerController doesn't respect device mute switch?

I'm using the MPMusicPlayerController application music player, created like:
appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
The issue is that it will play music no matter which setting the device mute switch is in, it doesn't seem to care either way. Is there some audio session mode I need to be in to have it respect the mute switch?
Yes. You want to use one of the following constants for your Audio Session Category:
kAudioSessionCategory_AmbientSound
kAudioSessionCategory_SoloAmbientSound
These are the ones that go silent when the Ring/Silent switch is set to silent.
Fwiw I'm seeing the same problem. I suspect that, like the iPod app itself, it will never respect the mute switch.
Try using iPodMusicPlayer instead of applicationMusicPlayer.

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.