7

I'm developing a recording application and I want to play a sound when the recording starts.

Unfortunately it appears that since iOS 5 it's not possible to play a system sound when a AVCaptureSession with an audio device is active.

Here's the relevant part of the code I'm using

- (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];
        });

        // ...                    
    }
}

Later on, I just call AudioServicesPlaySystemSound(_recStartSound) and nothing goes on. However if I make the same call before the session setup, the sound plays as expected.

I found this bug report, which is exactly relevant to my problem, as well as this and this question, but I couldn't find any workaround in none of them.

How can I play a short sound right before starting the AV recording?

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235

3 Answers3

1

Play the sound using AVAudioPlayer instead.

sc0rp10n
  • 1,118
  • 7
  • 8
  • I don't understand why this is voted down. This actually works and solves what the question is asking about (play a sound while an AVCaptureSession is active). – simonfi Aug 30 '14 at 11:06
0

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

After some trial and error with system sounds available, on iOS 8.1.3 / iPhone 6 I was able to play these two:

AudioServicesPlaySystemSound(1114);

and

AudioServicesPlaySystemSound(1113);

requires only one line of code to play the sound after linking:

  • AudioToolbox/AudioToolbox.h

    (see above link for more details)

And here's a partial-list of sounds available (from the comments in the above) http://iphonedevwiki.net/index.php/AudioServices

Community
  • 1
  • 1
-1

Extending sc0rp10n's answer, the changes to the code in the question would simply be:

Initializing the sound player (_recStartSound being of type AVAudioPlayer*):

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL * soundURL = [[NSBundle mainBundle] URLForResource:@"recording-starts" withExtension:@"aif"];
    _recStartSound = [[AVAudioPlayer alloc] initWithContentsOfURL: soundURL error: NULL];

    ...
}

When you want to play the sound:

[_recStartSound play];
simonfi
  • 293
  • 4
  • 12