【问题标题】:AudioServicesPlaySystemSound while AVCaptureSession is activeAVCaptureSession 处于活动状态时的 AudioServicesPlaySystemSound
【发布时间】:2013-07-30 20:13:29
【问题描述】:

我正在开发一个录音应用程序,我想在录音开始时播放声音。

不幸的是,从 iOS 5 开始,当带有音频设备的 AVCaptureSession 处于活动状态时,似乎无法播放系统声音。

这是我正在使用的代码的相关部分

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL * soundURL = [[NSBundle mainBundle] URLForResource:@"recording-starts" withExtension:@"aif"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &_recStartSound);

    //...

    if ([self.captureManager setupSession]) {
        // ... 

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [self.captureManager.session startRunning];
        });

        // ...                    
    }
}

稍后,我只是打电话给AudioServicesPlaySystemSound(_recStartSound),但没有任何反应。 但是,如果我在会话设置之前进行相同的调用,声音会按预期播放。

我发现了这个与我的问题完全相关的bug report,以及thisthis 的问题,但我都找不到任何解决方法。

如何在开始 AV 录制之前播放短声音?

【问题讨论】:

标签: ios avcapturesession audiotoolbox


【解决方案1】:

改用 AVAudioPlayer 播放声音。

【讨论】:

  • 我不明白为什么这被否决了。这实际上有效并解决了问题的问题(在 AVCaptureSession 处于活动状态时播放声音)。
【解决方案2】:

Are there any other iOS system sounds available other than 'tock'?

在对可用的系统声音进行了一些试验和错误之后,在 iOS 8.1.3 / iPhone 6 上,我能够播放这两个:

AudioServicesPlaySystemSound(1114);

AudioServicesPlaySystemSound(1113);

只需要一行代码即可在链接后播放声音

  • AudioToolbox/AudioToolbox.h

    (详情见以上链接)

这里是可用声音的部分列表(来自上面的 cmets) http://iphonedevwiki.net/index.php/AudioServices

【讨论】:

    【解决方案3】:

    扩展 sc0rp10n 的答案,问题中代码的更改只是:

    初始化声音播放器(_recStartSound 是 AVAudioPlayer* 类型):

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSURL * soundURL = [[NSBundle mainBundle] URLForResource:@"recording-starts" withExtension:@"aif"];
        _recStartSound = [[AVAudioPlayer alloc] initWithContentsOfURL: soundURL error: NULL];
    
        ...
    }
    

    当你想播放声音时:

    [_recStartSound play];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多