简体   繁体   English

单击按钮时播放iPhone相机快门声

[英]Play iPhone camera shutter sound when I click a button

单击按钮时如何从iPhone播放快门声?

You can use AudioToolbox and pure magical number 1108 like this: 您可以像这样使用AudioToolbox和纯魔法数字1108

Swift: 迅速:

import AudioToolbox
AudioServicesPlaySystemSound(1108)

Objective-C: Objective-C的:

#import <AudioToolbox/AudioToolbox.h>
AudioServicesPlaySystemSound(1108);

Here is full list of these pure magical ids . 这里是这些纯魔法ids的完整列表

1) Search online for a camera shutter wave file. 1)在线搜索相机快门波形文件。 You can trim this one if it is a bit long using a .wav editor http://www.freesound.org/people/Nathan_Lomeli/sounds/79190/ 如果使用.wav编辑器有点长,你可以修剪一下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. 2)将其命名为shutter.wav并将其与您的其他资源一起放入您的开发目录。

3) Define the sound in your view's .h file 3)在视图的.h文件中定义声音

@interface  myViewController : UIViewController {
    SystemSoundID SoundID;
}

4) Load the file in your View's "viewDidLoad" event: 4)在View的“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) In your button_click event: 5)在你的button_click事件中:

   //play then shutter.wav sound
AudioServicesPlaySystemSound(SoundID);

You don't specify a programming language, but let's assume ObjC for now and not Monotouch or any other 3rd party framework. 您没有指定编程语言,但我们现在假设ObjC而不是Monotouch或任何其他第三方框架。

AVAudioPlayer from the AV framework is your friend. AV框架的AVAudioPlayer是你的朋友。

Please see the documentation here at Apple 请参阅Apple的文档

Or do a search on AVAudioPlayer here on SO and you will find dozens of examples. 或者在这里搜索AVAudioPlayer,你会发现很多例子。 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. 不过,您必须从某个地方获取快门声音文件,但您可以使用在Google上找到的任何公共快门WAV。

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. 如果您真的想使用相机,拍摄照片时会自动播放快门声,但我怀疑这是您想要的。

一些声音可以选择到这些位置:/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds / System / Library / Sounds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM