在UnitySynth上导入自定义midis时为NullReference
本文关键字:midis 时为 NullReference 自定义 导入 UnitySynth | 更新日期: 2023-09-27 18:04:18
所以,我从unity答案下载了unity项目。这是链接:
UnitySynth链接下载unity答案页面
我已经完成了我的原型,但是当我试图加载我从网上下载的自定义midi时,我得到了错误。
我得到了这些错误代码:
Error Loading Midi:
Midi Failed to Load!
UnityEngine.Debug:Log(Object)
CSharpSynth.Sequencer.MidiSequencer:LoadMidi(String, Boolean) (at Assets/Plugins/CSharpSynth/Sequencer/MidiSequencer.cs:153)
UnitySynthTest:Awake() (at Assets/Scripts/UnitySynthTest.cs:42)
NullReferenceException: Object reference not set to an instance of an object
CSharpSynth.Sequencer.MidiSequencer.Play () (at Assets/Plugins/CSharpSynth/Sequencer/MidiSequencer.cs:167)
UnitySynthTest.OnGUI () (at Assets/Scripts/UnitySynthTest.cs:161)
我想知道一个简单的方法来转换midis到midi .txt的例子在演示中使用。
编辑:第一个错误代码,关于不加载midi,来自这里:
public bool LoadMidi(string file, bool UnloadUnusedInstruments)
{
if (playing == true)
return false;
MidiFile mf = null;
try
{
mf = new MidiFile(file);
}
catch (Exception ex)
{
//UnitySynth
//THIS IS THE ERROR LINE **************************
Debug.Log("Error Loading Midi:'n" + ex.Message);
return false;
}
return LoadMidi(mf, UnloadUnusedInstruments);
}
第二个关于null引用异常的例子来自:
public void Play()
{
if (playing == true)
return;
//Clear the current programs for the channels.
Array.Clear(currentPrograms, 0, currentPrograms.Length);
//Clear vol, pan, and tune
ResetControllers();
//set bpm
// THIS IS THE ERROR LINE *****************************
_MidiFile.BeatsPerMinute = 120;
//Let the synth know that the sequencer is ready.
eventIndex = 0;
playing = true;
}
我通过删除代码中的try和catch元素来解决自己的问题,如下所示:
public bool LoadMidi(string file, bool UnloadUnusedInstruments)
{
MidiFile mf = File.Open(file, FileMode.Open);
return LoadMidi(mf, UnloadUnusedInstruments);
}
我不知道这里发生了什么,如果我应该这样做,但现在即使是非。mid.txt文件也可以毫无问题地复制。