simple sounds will not play

I'm trying to play some simple interface sound effects, and the method I found online does not work. I don't get any errors but the sounds just don't play. My phone is not on silent mode, the volume is all the way up.



if let soundUrl = Bundle.main.url(forResource: filename, withExtension: ext) {
        var soundId: SystemSoundID = 0
      
        AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId)
   
        AudioServicesPlaySystemSoundWithCompletion(soundId, {
            AudioServicesDisposeSystemSoundID(soundId);
        });
}


I also tried adding this but it did not help:


AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)


Any ideas?

Answered by bsabiston in 257704022

Okay found the answer to this. In my settings, Ringers and Alerts has its own volume which apparently is independent of the silent switch and the volume. Mine was set to zero. I went to

settings
>
sounds
>
Ringer and alerts and increased the volume and it worked.


My app uses SpriteKit and before I figured this out, I found that SKAction.playSoundFileNamed(sound, waitForCompletion: false) will also play a sound. So I might just use that since it does not rely on the Ringers and Alerts volume control.

This is how I did it (back to XCode 7)

I created .wav resources, as ButtonTap


In the controller :


import AVFoundation
    func playBeep() {
        var filePath: String?
        filePath = NSBundle.mainBundle().pathForResource("ButtonTap", ofType: "wav")
        let fileURL = NSURL(fileURLWithPath: filePath!)
        var soundID:SystemSoundID = 0
        AudioServicesCreateSystemSoundID(fileURL, &soundID)
        AudioServicesPlaySystemSound(soundID)
    }


Then I call

playBeep()

wherever I need



In OSX App, I declare globals :


let beep = NSSound(named: "ButtonTap")

func playBeep() {
    beep!.play()
}



You can also use system sounds:


import AVFoundation

let systemSoundID: SystemSoundID = 1016  // the tweet sound. You can experiment with others

AudioServicesPlaySystemSound (systemSoundID) // // to play sound


Here is the list of system sounds

h ttps://github.com/TUNER88/iOSSystemSoundsLibrary

Thanks -- but that is essentially what I am doing. I started with AudioServicesPlaySystemSound, which didn't work either, then I learned that it has been deprecated. The new version is AudioServicesPlaySystemSoundWithCompletion, but it is still not doing anything.

I edited my post, test what I propose at the end.


if completion is needed, just pass nil.


PS : ad how solution, with a .wav does work in IOS 10.

I don't know what you mean by "ad how solution" but your test of the system tweet sound does not do anything on my phone either. There must be something else going on.

Accepted Answer

Okay found the answer to this. In my settings, Ringers and Alerts has its own volume which apparently is independent of the silent switch and the volume. Mine was set to zero. I went to

settings
>
sounds
>
Ringer and alerts and increased the volume and it worked.


My app uses SpriteKit and before I figured this out, I found that SKAction.playSoundFileNamed(sound, waitForCompletion: false) will also play a sound. So I might just use that since it does not rely on the Ringers and Alerts volume control.

autocorrection has striken again ad-hoc solution became ad how solution !

To aid with Claude 31, in SWIFTUI 2.0 I had to change the line of code to access the Bundle sound in playBeep() using:

Code Block
filePath = Bundle.main.path(forResource: "beep", ofType: "wav")





simple sounds will not play
 
 
Q