change Tempo in sample code of metronome

Hi friend,


In Apple's sample code:metronome,

https://developer.apple.com/library/content/samplecode/HelloMetronome/Listings/iOSHelloMetronome_Metronome_swift.html#//apple_ref/doc/uid/TP40017587-iOSHelloMetronome_Metronome_swift-DontLinkElementID_12


there is only one Tempo, and now I need to play 2 new Tempos in my App, besides the old Tempo.

but after I switch from the old Tempo to another Tempo using:


" func setTempo(_ tempo: Float32) {

tempoBPM = tempo


let secondsPerBeat: Float32 = 60.0 / tempoBPM

beatsToScheduleAhead = Int32(GlobalConstants.kTempoChangeResponsivenessSeconds / secondsPerBeat)

if (beatsToScheduleAhead < 1) { beatsToScheduleAhead = 1 }

} "


My App still plays with previous old Tempo for some seconds,then change to my second new Tempo ( I have 2 new Tempos ), instead switch to my first new Tempo.


I use following function to stop previous old Tempo, then play with new Tempo, How to prevent my App from playing with previous old Tempo for some seconds?

I need my App to play with my first Tempo, instead of still playing with previous old Tempo for some seconds. thanks!


func stop() {

isPlaying = false;


/ Note that pausing or stopping all AVAudioPlayerNode's connected to an engine does

NOT pause or stop the engine or the underlying hardware.


The engine must be explicitly paused or stopped for the hardware to stop.

*/

player.stop()

player.reset()


/ Stop the audio hardware and the engine and release the resources allocated by the prepare method.


Note that pause will also stop the audio hardware and the flow of audio through the engine, but

will not deallocate the resources allocated by the prepare method.


It is recommended that the engine be paused or stopped (as applicable) when not in use,

to minimize power consumption.

*/

engine.stop()


playerStarted = false

}

Replies

I've not looked in detail, but I see the code uses threads. maybe there is a sync issue, you wait for the other thread to execute.

thanks. Would you please tell me how to tackle it?