[quote=213066:@Ruth Millard]@Michel Bujardet An alternative to a thread if you want to keep the UI responsive is to delegate the note playing to another app that runs concurrently, a helper, launched from the main app when needed.
Where would I find out more about how to do this?[/quote]
Long time no see. I had forgotten about this thread.
A helper app is just another Xojo program you call from your main program thread using a shell. Since it is a different program, it uses a different core of the processor and is entirely independent, therefore will not be slowed down by whatever you do.
Here is how it works.
The notes helper is a windowless app which sole purpose is to play music. I created mine with the NotePlayer example from the LR.
Everything happens in the App Open event.
- In the inspector, set the default window as None
- Add a
Noteplayer1 as NotePlayer
property to App.
- Add the Open event, I added the LR example inside. Note the last line
Quit
which closes the app when music is over :
Noteplayer1 = New NotePlayer
NotePlayer1.Instrument = 1
// Notes for Do Re Mi Fa So La Ti Do
// see http://en.wikipedia.org/wiki/Do-Re-Mi from The Sound of Music http://en.wikipedia.org/wiki/The_Sound_of_Music
// (C, D, E, F, G, A, B, C)
Dim doReMi(7) As Integer
doReMi = Array(60, 62, 64, 65, 67, 69, 71, 60)
For Each note As Integer In doReMi
NotePlayer1.PlayNote(note, 100)
// Pause to let note play
App.SleepCurrentThread(500)
Next
quit
The Caller app has this in the Run event of Thread1 :
Sub Thread1Run(Sender as Thread)
dim s as new shell
dim f as FolderItem = GetFolderItem("").child("notes.app")
s.Execute("open "+f.ShellPath)
End Sub
Both apps are in the same folder, so the path to notes.app is based on that. For distribution, you can also place notes.app in the bundle, so everything is self contained. I used to place such apps in Resources, but I remember Sam Rowlands mentioned new recommendations from Apple about that. Unfortunately, I cannot locate that at the moment.
To point to Resources within the Caller bundle, here is the line :
dim f as folderitem = App.ExecutableFile.parent.parent.child("Resources").child("Notes.app")
This archive contains the two example projects and the builds. Open Caller and click the button to see it working.
Notes.zip
Last, you need to avoid displaying the icon in the dock for the helper app. See
https://forum.xojo.com/11024-how-to-start-app-with-no-window-and-without-icon-in-dock/0
I have used that in the notes program. See the OS X build settings Script1