Swift AKMIDIListener:ReceivedNoteOn无法更改UI组件
我正在创建一个接收MIDI音符的应用程序,并根据收到的音符在应用程序中播放音乐。我正在使用AudioKit的AKMIDIListener协议和函数ReceiveDmidNoteon。它能够接收这些事件并相应地播放我想从我的应用程序播放的音符。但是,我也在尝试更改ReceiveDmidNoteon函数中的UI组件,但我无法这样做。否则,ReceiveDmidNoteon会正确处理其事件,UI不会因为某些原因而改变以反映这一点。我正在更改的UIViewController也具有AKMIDIListener协议,并且在该UIViewController中还定义了ReceiveDmidNoteon函数。我在下面提供了一个代码片段来帮助识别问题。为什么UI组件不能更新?在下面的代码中,“playButton”在收到midNoteon函数时没有被更新,尽管我知道函数运行是因为它播放背景音乐。此外,每当我在代码中的其他函数中使用“更改标题”时,playButton会相应地更改其标题Swift AKMIDIListener:ReceivedNoteOn无法更改UI组件,swift,xcode,uiviewcontroller,midi,audiokit,Swift,Xcode,Uiviewcontroller,Midi,Audiokit,我正在创建一个接收MIDI音符的应用程序,并根据收到的音符在应用程序中播放音乐。我正在使用AudioKit的AKMIDIListener协议和函数ReceiveDmidNoteon。它能够接收这些事件并相应地播放我想从我的应用程序播放的音符。但是,我也在尝试更改ReceiveDmidNoteon函数中的UI组件,但我无法这样做。否则,ReceiveDmidNoteon会正确处理其事件,UI不会因为某些原因而改变以反映这一点。我正在更改的UIViewController也具有AKMIDIListe
let midi = AudioKit.midi
class firstPageViewController: UIViewController, AKMIDIListener {
@IBOutlet var MusicButtons: [UIButton]!
@IBOutlet var playButton: UIButton!
@IBOutlet var pauseButton: UIButton!
@IBOutlet var WaveForm: AudioVisualizationView!
@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var secondButton: UIButton!
@IBOutlet weak var thirdButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
isOnFirstPage = true
isOnSecondPage = false
isOnThirdPage = false
self.secondButton.setTitleColor(UIColor.blue, for: .normal)
self.thirdButton.setTitleColor(UIColor.blue, for: .normal)
midi.openInput()
midi.addListener(self)
self.init_buttons()
self.init_soundwave()
self.init_audio()
}
func receivedMIDINoteOn(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel) {
let currNum = mapping[Int(noteNumber)]
if currNum == 64 { //Play button
self.playBackground(0)
self.playButton.setTitle("Currently Playing", for: .normal)
}
else if currNum == 67 { //Pause button
self.pauseMusic(0)
self.playButton.setTitle("Play", for: .normal)
}
else if currNum == 68 { //Switch button - first page
self.firstPageSwitch(0)
}
else if currNum == 69 { //Switch button - second page
self.secondPageSwitch(0)
}
else if currNum == 70 { //Switch button - third page
self.thirdPageSwitch(0)
}
else if currNum == 71 {
//
}
else if currNum == 65 {
//
}
else if currNum == 66 {
//
}
else {
if isOnFirstPage! {
self.playMusic(currNum: currNum!, holder: audioPlayer)
self.MusicButtons![currNum!].backgroundColor = UIColor(red: 247/255, green: 183/255, blue: 98/255, alpha: 1.0)
}
if isOnSecondPage!{
self.playMusic(currNum: currNum!, holder: secondAudioPlayer)
self.MusicButtons![currNum!].backgroundColor = UIColor(red: 247/255, green: 183/255, blue: 98/255, alpha: 1.0)
}
}
}
}评论者是正确的,您需要将UI调用包装在
DispatchQueue.main.async {
// ... code here
}
并且可能还必须在末尾放置一个
self.playButton.setNeedsDisplay()
。是否在主UI线程上调用receivedMIDINoteOn()
方法?曾经有一段时间,MIDI完全是在中断中处理的(在过去),所以如果它是在后台线程中处理的,我也不会感到惊讶。此外,您还可以尝试添加对self.MusicButtons的调用![currNum!]needsDisplay=true
更改背景颜色后。