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 m开发者_运维百科uted:
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.
精彩评论