需要访问 iPhone 使用的默认相机快门声音

20

目前当用户按下按钮时,我会播放自定义相机快门声。

但是,如果可能的话,我更愿意使用在使用iPhone拍照时默认播放的相机快门声。

我能否访问并使用默认的iPhone相机快门声?如果可以,那么它实际上位于何处?

我需要确定它的文件路径,以便我可以将其与下面的代码一起使用,并只更改pathForResource的输入:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"shutter" ofType: @"wav"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];

    self.myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

    [self.myAudioPlayer play];

感谢您的帮助。

2个回答

39

1
从iOS9开始,更推荐使用AudioServicesPlaySystemSoundWithCompletion - AudioServicesPlaySystemSound的头文件注释表明它将被弃用。 - user2067021
请注意,您需要导入AVKit才能访问这些方法。 - CoolPineapple

6

看了stevesliva的答案后,我受益匪浅。然而,我最终得到了这个:

Unexpected type name 'SystemSoundID': expected expression

使用以下代码行来在HTML中嵌入图像:

AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(1108), nil);

因此,使用这段代码可以让我的应用程序播放相机快门声:
SystemSoundID soundID = 1108;

AudioServicesPlaySystemSoundWithCompletion(soundID, ^{
    AudioServicesDisposeSystemSoundID(soundID);
});

希望这能帮助到某些人。

这是因为你引用的代码是Swift的。你有看过ObjC/C API吗? 应该是AudioServicesPlaySystemSoundWithCompletion((SystemSoundID)1108, nil); - Léo Natan
OP的问题是Obj-C相关的。被采纳的答案是Swift的,我觉得这很奇怪。我添加了我的评论,希望我的实际Obj-C响应能够帮助某人。 - Carl Hine
答案在 Obj C 中,并且已经使用 Swift 进行了编辑。 - Léo Natan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接