App sound stops music

Hi there, my App plays an alert sound, which stops the music played on iTunes in the background. Any way to avoid this?

Yes!

You can add points to this feature request: <https://xojo.com/issue/49992>

Solution:

  1. Drag the sound file into your project (I guess you already did that)

  2. Add this code in a module

[code]Protected Sub PlaySound1()

Static soundID As UInt32

if soundID = 0 then

Declare Function AudioServicesCreateSystemSoundID Lib "AudioToolbox.framework" (inFileURL As ptr, ByRef SystemSoundID As UInt32) As Integer

Dim f As Xojo.IO.FolderItem = xojo.io.SpecialFolder.GetResource("sound_file_name.aif") //Replace the file name with your own sound

declare function initFileURLWithPath_ lib “Foundation” selector “initFileURLWithPath:” (obj_id as ptr, path as CFStringRef) as ptr
declare function NSClassFromString lib “Foundation” (clsName as CFStringRef) as ptr
declare function alloc lib “Foundation” selector “alloc” (clsRef as Ptr) as ptr

Dim NSUrlRef as ptr = NSClassFromString(“NSURL”)
Dim urlPtr As Ptr = initFileURLWithPath_(alloc(NSUrlRef), f.path)

Dim res As Integer = AudioServicesCreateSystemSoundID(urlPtr, soundID)
res = res

End If

If soundID > 0 then
Declare Sub AudioServicesPlaySystemSound Lib “AudioToolbox.framework” (snd As uint32)
AudioServicesPlaySystemSound(soundID)
End If
End Sub
[/code]

It works perfect!

What an elegant solution, thank you so much Jeremie - you made my day. Sitting here in Namibia, I will tell my French friends tonight in the Yoga class, how you helped me. Have an awesome day… :slight_smile: