Play iPhone camera shutter sound when I click a button
How do I play the shutter sound from iPhone 开发者_如何学编程when I click a button?
You can use AudioToolbox
and pure magical number 1108
like this:
Swift:
import AudioToolbox
AudioServicesPlaySystemSound(1108)
Objective-C:
#import <AudioToolbox/AudioToolbox.h>
AudioServicesPlaySystemSound(1108);
Here is full list of these pure magical ids.
1) Search online for a camera shutter wave file. You can trim this one if it is a bit long using a .wav editor http://www.freesound.org/people/Nathan_Lomeli/sounds/79190/
2) Name it shutter.wav and put it into your development directory along with your other resources.
3) Define the sound in your view's .h file
@interface myViewController : UIViewController {
SystemSoundID SoundID;
}
4) Load the file in your View's "viewDidLoad" event:
//load a sound wav file to use for the shutter
NSURL *buttonURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"shutter" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
5) In your button_click event:
//play then shutter.wav sound
AudioServicesPlaySystemSound(SoundID);
some sounds to pick to those location : /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds /System/Library/Sounds
You don't specify a programming language, but let's assume ObjC for now and not Monotouch or any other 3rd party framework.
AVAudioPlayer from the AV framework is your friend.
Please see the documentation here at Apple
Or do a search on AVAudioPlayer here on SO and you will find dozens of examples. Still, you will have to grab the shutter sound file from somewhere but you can take any public available shutter WAV you find on Google.
If you really want to use the camera, the shutter sound will be played automatically when a picture is taken but I doubt that is what you want.
精彩评论