Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AudioServicesPlaySystemSound not playing any sound in iOS 8 device

I have AVFoundation and AudioToolbox frameworks added to my project. In the class from where I want to play a system sound, I #include <AudioToolbox/AudioToolbox.h> and I call AudioServicesPlaySystemSound(1007);. I'm testing in a device running iOS 8, sounds are on and volume is high enough, but I don't hear any system sound when I run the app and AudioServicesPlaySystemSound(1007); is called... what could I be missing?

like image 286
AppsDev Avatar asked Sep 10 '15 03:09

AppsDev


3 Answers

With iOS10 playing audio like this doesn't work:

SystemSoundID audioID;

AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &mySSID);
AudioServicesPlaySystemSound(audioID);

Use this instead:

AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioID);

AudioServicesPlaySystemSoundWithCompletion(audioID, ^{
    AudioServicesDisposeSystemSoundID(audioID);
});
like image 161
Tiago Almeida Avatar answered Sep 22 '22 00:09

Tiago Almeida


According to the documentation:

This function (AudioServicesPlaySystemSound()) will be deprecated in a future release. Use AudioServicesPlaySystemSoundWithCompletion instead.

Use the following code snippet to play sounds:

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; //filename can include extension e.g. @"bang.wav"
if (fileURL)
{
    SystemSoundID theSoundID;
    OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
    if (error == kAudioServicesNoError)
    { 
        AudioServicesPlaySystemSoundWithCompletion(theSoundID, ^{
            AudioServicesDisposeSystemSoundID(theSoundID);
        });
    }
}

Also, the completion block ensures that the sound play was completed before it is disposed of.

If this doesn't fix the issue, maybe your problem isn't code related but rather setting related (device on silent / simulator sounds muted from MAC System Preferences, make sure "Play user interface sound effects" is checked)

like image 22
Ameer Sheikh Avatar answered Sep 21 '22 00:09

Ameer Sheikh


This will play system sound.

But remember system sound will not play longer sound.

NSString *pewPewPath  = [[NSBundle mainBundle] pathForResource:@"engine" ofType:@"mp3"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound);
AudioServicesPlaySystemSound(_engineSound);
like image 21
Shubham Narang Avatar answered Sep 22 '22 00:09

Shubham Narang