如果AKSequencer到达序列的末尾,如何自动停止?

问题描述 投票:1回答:1

我注意到AKSequencer继续播放(AKSequencer.isPlaying为真),即使序列中没有更多音符可以播放。是否可以在序列结束时自动停止?

ios audiokit
1个回答
2
投票

您可以使用AKMIDICallbackInstrument根据您的音序器正在播放的内容触发代码。在这种情况下,添加一个额外的轨道并将其输出设置为AKMIDICalllbackInstrument。在您希望音序器停在的位置向此音轨添加一个事件(如果您不知道已知多长时间,可以使用sequencer.length)。然后设置回调乐器的回调函数,以在收到事件时停止音序器。

var seq = AKSequencer()
var callbackInst: AKMIDICallbackInstrument!
var controlTrack: AKMusicTrack!

func setUpCallback() {
    // set up a control track
    controlTrack = seq.newTrack()

    // add an event at the end
    // we don't care about anything here other than the position
    controlTrack.add(noteNumber: 60,
                     velocity: 60,
                     position: seq.length,
                     duration: AKDuration(beats: 1))

    // set up the MIDI callback instrument
    callbackInst = AKMIDICallbackInstrument()
    controlTrack?.setMIDIOutput(callbackInst.midiIn)

    // stop the sequencer when the control track's event's noteOn is recieved
    callbackInst.callback = { statusByte, _, _ in
        guard let status  = AKMIDIStatus(statusByte: statusByte) else { return }
        if status == .noteOn {
            self.seq.stop()
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.