我注意到AKSequencer会继续播放(AKSequencer.isPlaying为真),即使序列中没有更多的音符可以播放。有没有可能让它在序列结束时自动停止?
发布于 2019-03-11 06:50:35
您可以使用AKMIDICallbackInstrument根据sequencer正在播放的内容来触发代码。在这种情况下,添加一个额外的轨迹并将其输出设置为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()
}
}
}https://stackoverflow.com/questions/55087329
复制相似问题