【问题标题】:Need to access the default camera shutter sound used by the iPhone需要访问 iPhone 使用的默认相机快门声音
【发布时间】:2014-04-03 22:13:41
【问题描述】:

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

但是,如果可能的话,我宁愿只使用您使用 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];

感谢您的帮助。

【问题讨论】:

    标签: ios objective-c iphone-5 iphone-4 audiotoolbox


    【解决方案1】:
    AudioServicesPlaySystemSound(1108);
    

    基于: Are there any other iOS system sounds available other than 'tock'?http://iphonedevwiki.net/index.php/AudioServices

    2016 年 ....

        if #available(iOS 9.0, *) {
            AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(1108), nil)
        } else {
            AudioServicesPlaySystemSound(1108)
        }
    

    【讨论】:

    • 从 iOS9 开始,AudioServicesPlaySystemSoundWithCompletion 是首选 - AudioServicesPlaySystemSound 的标头 cmets 表明它将被弃用。
    • 请注意,您需要导入 AVKit 才能访问这些方法
    【解决方案2】:

    查看 stevesliva 的回答对我帮助很大。但是,我最终得到了这个:

    Unexpected type name 'SystemSoundID': expected expression
    

    ...使用这条线...

    AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(1108), nil);
    

    所以,使用这段代码让我的应用播放了相机快门噪音:

    SystemSoundID soundID = 1108;
    
    AudioServicesPlaySystemSoundWithCompletion(soundID, ^{
        AudioServicesDisposeSystemSoundID(soundID);
    });
    

    希望这对某人有所帮助。

    【讨论】:

    • 那是因为你引用的代码是 Swift。你甚至看过 ObjC/C API 吗?应该是AudioServicesPlaySystemSoundWithCompletion((SystemSoundID)1108, nil);
    • OP 问题在 Obj-C 中。公认的答案是 Swift - 我觉得这很奇怪。我添加了我的评论,希望我的实际 Obj-C 响应对某人有所帮助。
    • 答案在 Obj C 中,并且已经用 Swift 进行了编辑。
    猜你喜欢
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多