当我点击一个按钮时,如何从iPhone播放快门声?
AudioToolbox
和纯神秘数字 1108
:
Swift:
import AudioToolbox
AudioServicesPlaySystemSound(1108)
Objective-C:
#import <AudioToolbox/AudioToolbox.h>
AudioServicesPlaySystemSound(1108);
这里是所有这些纯魔法ID的完整列表。
1) 在网上搜索相机快门声音文件。如果它有点长,您可以使用.wav编辑器进行裁剪。 http://www.freesound.org/people/Nathan_Lomeli/sounds/79190/
2) 将其命名为shutter.wav并将其与其他资源一起放入您的开发目录中。
3) 在您的视图的.h文件中定义该声音。
@interface myViewController : UIViewController {
SystemSoundID SoundID;
}
4) 在您的视图的 "viewDidLoad" 事件中加载文件:
//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) 在您的button_click事件中:
//play then shutter.wav sound
AudioServicesPlaySystemSound(SoundID);
您没有指定编程语言,但我们现在假设是ObjC而不是Monotouch或任何其他第三方框架。
AV框架中的AVAudioPlayer是您的好朋友。
请参阅苹果公司文档。
或在SO上搜索AVAudioPlayer,您将找到数十个示例。不过,您仍需要从某个地方获取快门声音文件,但您可以在Google上找到任何公共可用的快门WAV。
如果您真的想使用相机,则在拍照时会自动播放快门声音,但我怀疑这不是您想要的。
一些声音可以选择到这些位置:/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds /System/Library/Sounds