iOS:拍照时启用快门声

时间:2014-04-16 08:23:07

标签: ios audio

我使用以下代码:https://stackoverflow.com/a/14063081/1011125

现在我需要启用拍摄照片时播放的声音。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

有两种方法可以做到:

1)使用AVAudioPlayer

myPic.m添加:

//at the top
#import <AudioToolbox/AudioToolbox.h>
#import <MediaPlayer/MediaPlayer.h>

@implementation myPic {

    AVAudioPlayer *mySound;

 }

//then use this function where ever you want to play sound

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"mySound" ofType: @"mp3"]; //wav, aiff, ogg, ext
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    loopUno = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    loopUno.numberOfLoops = 0; // here loop setup infinite is -1
    [mySound play];

2)我更喜欢,没有循环但更稳定的是SystemSoundID soundID

    //at the top
    #import <AudioToolbox/AudioToolbox.h>
    #import <MediaPlayer/MediaPlayer.h>

    @implementation myPic {

         SystemSoundID soundID;

     }

//then use this function where ever you want to play sound
        AudioServicesDisposeSystemSoundID(soundID);
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =  CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"mySound" ,CFSTR ("mp3") , NULL); //wav, aiff, ogg, ext
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);

覆盖发言人:

- (void)viewDidLoad {

    [super viewDidLoad];

    AVAudioSession* session = [AVAudioSession sharedInstance];
    BOOL success;
    NSError* error;
    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    success = [session setActive:YES error:&error];
}

或者如果您在应用中使用更多音频,请在AppDelegate.m下添加application didFinishLaunchingWithOptions:内的功能并将您的代表导入到您需要的地方

希望这能帮到你;)

答案 1 :(得分:0)

将此代码放在快门声中,

AudioServicesPlaySystemSound(1108);

注意http://iphonedevwiki.net/index.php/AudioServices