Vince Gonzales
Vince Gonzales

Reputation: 985

AudioKit 4.1 - How to properly stop AKSampler from playing audio?

I have 2 AKSamplers that are connected to an AKMixer but when I call the stop function for each AKSampler, the audio does not stop playing it only stops when I call the stop of the AKMixer. How do I stop playing an AKSampler which is connected to an AXMixer?

Sample Code:

func stop() {
   let sampler1 = AKSampler()
   let sampler2 = AKSampler()
   let mixer = AKMixer(sampler1, sampler2)

   AudioKit.output = mixer

   do {
        try sampler1.loadWav("Support Objects/audio")
        try sampler2.loadWav("Support Objects/audio")
    } catch {
        return
   }

    do {
        try AudioKit.start()
    } catch let error as NSError{
        print(error.debugDescription)
    }

   sampler1.play(noteNumber: //some midi number)
   sampler2.play(noteNumber: //some midi number)
   sampler1.stop() // does not stop sampler1 from playing
}

Upvotes: 3

Views: 289

Answers (1)

analogcode
analogcode

Reputation: 299

A preferred method to stop notes with AKSampler is to stop the specific AKSampler note that was previously started.

For example:

 sampler1.play(noteNumber: yourMIDINoteNumber)
...
 sampler1.stop(noteNumber: yourMIDINoteNumber)

Should successfully stop the AKSampler.

Upvotes: 2

Related Questions