我正在研究在线乐谱阅读教练a prototype is available here。我能够实现对以前连接的MIDI键盘的访问,但是,我在努力实现热插拔场景。我的最后一招看起来像这样: setupDevices(inputs){ let deviceName = ''; inputs.forEach((value, key, map) => { if (value.connection === 'open' || value.connection === 'pending' ){ value.open().then((midiInput) => { console.log(`Opened ${midiInput.name}`); midiInput.onmidimessage = midiController.onMIDIMessage; }, null); } else { value.onmidimessage = null; } }); if (deviceName.length === 0) { deviceName = 'none'; } document.getElementById('connectedDevice').setAttribute('value', deviceName);},onMIDISuccess(midiAccess) { midiAccess.onstatechange = (evt) => { console.log('MIDI config changed'); midiController.setupDevices(evt.currentTarget.inputs); };}在调试所有的Promise时,会使用漂亮的参数来调用它们,但我无法收到任何按键消息。同样console.log(Opened ${midiInput.name})被调用的次数比预期的还要多:midiController.js:57 MIDI config changedmidiController.js:40 Opened KOMPLETE KONTROL - 1midiController.js:57 MIDI config changedmidiController.js:40 Opened KOMPLETE KONTROL - 1midiController.js:57 MIDI config changedmidiController.js:40 Opened KOMPLETE KONTROL - 1midiController.js:40 Opened KOMPLETE KONTROL EXT - 1midiController.js:40 Opened KOMPLETE KONTROL - 13midiController.js:40 Opened KOMPLETE KONTROL EXT - 1midiController.js:57 MIDI config changedmidiController.js:40 Opened KOMPLETE KONTROL - 1midiController.js:40 Opened KOMPLETE KONTROL EXT - 1midiController.js:57 MIDI config changedmidiController.js:40 Opened KOMPLETE KONTROL - 1midiController.js:40 Opened KOMPLETE KONTROL EXT - 12midiController.js:40 Opened Komplete Kontrol DAW - 1midiController.js:57 MIDI config changedmidiController.js:40 Opened KOMPLETE KONTROL - 1midiController.js:40 Opened KOMPLETE KONTROL EXT - 1midiController.js:57 MIDI config changedmidiController.js:40 Opened KOMPLETE KONTROL - 1midiController.js:40 Opened KOMPLETE KONTROL EXT - 1 (所有这些都在我刚打开 Controller 后立即打印)。有什么提示吗? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 function midiOnMIDImessage(event) { console.log(event); }function requestMIDIAccessSuccess(midi) { var inputs = midi.inputs.values(); for (var input = inputs.next(); input && !input.done; input = inputs.next()) { input.value.onmidimessage = midiOnMIDImessage; } }navigator.requestMIDIAccess().then(requestMIDIAccessSuccess, requestMIDIAccessFailure);参见Working example (adsbygoogle = window.adsbygoogle || []).push({}); 09-28 03:01