AudioServicesPlaySystemSound with active AVCaptureSession

I am developing a recording application and I want to play sound when recording starts.

Unfortunately, it seems that with iOS 5 it is not possible to play system sound when AVCaptureSession with an audio device is active.

Here is the relevant piece of code that I use

 - (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 I just call AudioServicesPlaySystemSound(_recStartSound) and nothing happens. However, if I make the same call before setting up the session, the sound plays as expected.

I found this report an error report that exactly matches my problem, as well as this and this question, but I could not find any workaround in any of them.

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

+6
source share
3 answers

Play sound using AVAudioPlayer.

0
source

Extension of the answer sc0rp10n, changes in the code in the question will be simple:

Initialization of the sound player (_recStartSound type AVAudioPlayer *):

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

If you want to play sound:

 [_recStartSound play]; 
0
source

See Are there any other sound systems for iOS besides "tock"?

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

AudioServicesPlaySystemSound (1114);

and

AudioServicesPlaySystemSound (1113);

only one line of code is required to play the sound after the link:

  • AudioToolbox / AudioToolbox.h

    (see link above for more details)

And here is an incomplete list of available sounds (from the comments above) http://iphonedevwiki.net/index.php/AudioServices

0
source

Source: https://habr.com/ru/post/950659/


All Articles