AudioServicesPlaySystemSound not playing any sound in iOS 8 device
Asked Answered
G

6

6

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?

Graeco answered 10/9, 2015 at 3:45 Comment(4)
Did you check the silent switch??Villose
@JasonNam yes, sound is on...Graeco
Do other audios sound well?Villose
@JasonNam Ring tones and Music play well...Graeco
C
2

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);
Chromoprotein answered 10/9, 2015 at 9:33 Comment(1)
Can u give some suggestion that how to play longer sound or repeated tone? I have to play repeated sound until the call is not picked.Goniometer
S
9

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);
});
Sivia answered 23/9, 2016 at 13:14 Comment(0)
M
5

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)

Maddocks answered 16/8, 2016 at 8:25 Comment(2)
Can you link to the documentation? I couldn't find your quote.Kayo
This is from the SDK header file. Try using the deprecated method and XCode will show you the warning message.Maddocks
C
2

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);
Chromoprotein answered 10/9, 2015 at 9:33 Comment(1)
Can u give some suggestion that how to play longer sound or repeated tone? I have to play repeated sound until the call is not picked.Goniometer
F
1

I have just tested the code on an iPad and iPhone running iOS 8, and it is working on the real devices.

For some very strange reason, it isn't working on iOS 8 simulator for any device, even though it works on iOS 7 and 7.1 simulators.

Otherwise below code works fine in all real devices.

NSString *pewPewPath  = [[NSBundle mainBundle] pathForResource:@"engine" ofType:@"mp3"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound);
AudioServicesPlaySystemSound(_engineSound);
Foamy answered 5/1, 2017 at 8:48 Comment(0)
V
1

Here's how you do it in Swift 5.8 and Xcode 14:

let soundID: SystemSoundID = 1104 // kSystemSoundID_Tock
AudioServicesPlaySystemSoundWithCompletion(soundID) {
  AudioServicesDisposeSystemSoundID(soundID)
}

⚠️ Make sure the device is not on silent mode!

Vaud answered 19/7, 2023 at 12:35 Comment(0)
J
0

for swift 3.x and xcode 8:

var theSoundID : SystemSoundID = 0
let bundleURL = Bundle.main.bundleURL
let url = bundleURL.appendingPathComponent("Invitation.aiff")

let urlRef = url as CFURL

let err = AudioServicesCreateSystemSoundID(urlRef, &theSoundID)
if err == kAudioServicesNoError{
    AudioServicesPlaySystemSoundWithCompletion(theSoundID, {
        AudioServicesDisposeSystemSoundID(theSoundID)
    })
}
Joplin answered 4/2, 2017 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.