Swift - Bong sound instead of "beep"

I need to make a harsh sound to indicate the user made an error, and am using the following code

func beep() {
    let systemSoundID: SystemSoundID = 1000

but 1000 is the code for the standard “beep”, and while I found a list of all the valid sound codes,
the are 124 in the list, and I don’t really want to work thru all of them.

So… does anyone know the best # to use???

seems #1053 is what I wanted, just had to work down the list to find it

Somewhere I have the Objective-C code for playing a sound by name, and the code for getting the sound name. I’ll try to remember to look it up.

Thanks, I had that same code, but this was much more compact, and doesn’t rely on external sound files (well external in the sense they aren’t part of the OS already)

I couldn’t find the original Obj-C code, but I found the Xojo conversion.
The key is AudioServicesCreateSystemSoundID which takes an URL and returns a soundID.

Public Const kSoundNameSystem as String = "_system"

Public Sub playSystemSound(name as string)
  #if targetMacOS then
    if name <> kSoundNameSystem and name <> trim( "" ) then
      declare sub AudioServicesPlayAlertSound lib "AudioToolbox" ( soundID as integer )
      static soundIDs as new dictionary
      if soundIDs.hasKey( name ) then
        AudioServicesPlayAlertSound( soundIds.value( name ).integerValue )
      end if
      if rightb( name, 5 ) <> ".aiff" then name = name + ".aiff"
      Dim soundfile as folderItem = specialFolder.System.child( "Library" ).child( "Sounds" ).child( name )
      if soundFile = nil or soundFile.exists = false then
        oak.log currentmethodName + " system sound " + name + " doesn't appear to exist."
      end if
      declare Function NSURL_fileURLWithPathIsDirectory lib "Foundation" selector "fileURLWithPath:isDirectory:" _
      (NSURLClass as integer, path as CFStringRef, directory as boolean) As integer
      declare function NSClassFromString lib "Foundation" ( inClassName as CFStringRef ) as integer
      Dim nURL as integer = NSURL_fileURLWithPathIsDirectory( NSClassFromString( "NSURL" ), soundfile.nativePath, false )
      declare function AudioServicesCreateSystemSoundID lib "AudioToolbox" ( URLInstance as integer, byRef soundID as integer ) as integer
      Dim soundID as integer
      call AudioServicesCreateSystemSoundID( nURL, soundID )
      if soundID <> 0 then
        soundIDs.value( name ) = soundID
        AudioServicesPlayAlertSound( soundID )
      end if
    End if
End Sub

1 Like