cs guy
cs guy

Reputation: 929

AudioKit AKPlayer stop with at AVAudioTime method

In AudioKit there is this method for AKPlayer:

@objc dynamic public func play(at audioTime: AVAudioTime?)

I want the same for stop method because I want to be able to stop the player at any time when the user hits the stop button. I am making a music app and I need to stop the sound in X time which is calculated based on BPM and etc.

Here is how I start my AKPlayer:

drums.play(at: AVAudioTime.now() + timeToClosestBeatGrid)

I want the same API with stop:

drums.stop(at: AVAudioTime.now() + timeToClosestBeatGrid) // this api doesnt exist :(((

I tried using endTime property by setting it but it does not seem to do anything...

How may I accomplish this?

PS: I am not looking for a Timer solution this is because a timer is not 100% accurate. I want my stop method to be 100% accurate just like play method

Upvotes: 0

Views: 229

Answers (2)

vindur
vindur

Reputation: 436

The most accurate way to schedule events in AudioKit is by using AKSequencer. The sequencer can be connected to a callback instrument, which is a node that passes the events to an user-defined function.

In your case, you would add an event at the time where you want the player to stop. In your callback function, you would stop the player as a response to that event.

This is an outline of what should be done:

  1. Create a track to contain the stop event, using AKSequencer's addTrack method. Connect this track to an AKCallbackInstrument. Please see this answer on how to connect an AKCallbackInstrument to an AKSequencer track.
  2. Add the stop event to the track, at the time position where you want the music to stop. As you will be interpreting the event yourself with a callback function, it doesn't really matter what type of event you use. You could simply use a Note On.
  3. In the callback function, stop the player when that event is received.

This is what your callback function would look like:

func stopCallback(status:UInt8, note:MIDINoteNumber, vel:MIDIVelocity) -> () {
    guard let status = AKMIDIStatus(byte: status),
        let type = status.type,
        type == .noteOn else { return }
    drums.stop()
}

Upvotes: 1

Christopher Kolodziej
Christopher Kolodziej

Reputation: 92

According to AudioKit documentation, you can try using the schedule(at:) method:

You can call this to schedule playback in the future or the player will call it when play() is called to load the audio data

After the play() method you should declare this schedule(at:) with an offset AVAudioTime.now() + timeToClosestBeatGrid and specify .dataPlayedBack as completion callback type, because this completion is called when (from docs)...

The buffer or file has finished playing

and now (in completion block) you can call drums.stop()

But... If the .stop() method should be called whenever the button is pressed, why not use some form of delay (Timer or DispatchQueue) with the value timeToClosestBeatGrid as the offset?

Upvotes: 0

Related Questions