C# 在UnitySynth上导入自定义MIDI时的NullReference
因此,我从unity answers下载了unity项目。 以下是链接: 我已经完成了我的原型,但是当我尝试加载从web下载的自定义midi时,我遇到了错误 我得到了这些错误代码:C# 在UnitySynth上导入自定义MIDI时的NullReference,c#,unity3d,midi,C#,Unity3d,Midi,因此,我从unity answers下载了unity项目。 以下是链接: 我已经完成了我的原型,但是当我尝试加载从web下载的自定义midi时,我遇到了错误 我得到了这些错误代码: Error Loading Midi: Midi Failed to Load! UnityEngine.Debug:Log(Object) CSharpSynth.Sequencer.MidiSequencer:LoadMidi(String, Boolean) (at Assets/Plugins/CSharp
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)
我很想知道一种简单的方法,将MIDI转换为mid.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);
}
第二个,关于空引用异常的一个来自:
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文件也可以毫无问题地复制。我通过删除代码中的try-and-catch元素解决了自己的问题,如下所示:
public bool LoadMidi(string file, bool UnloadUnusedInstruments)
{
MidiFile mf = File.Open(file, FileMode.Open);
return LoadMidi(mf, UnloadUnusedInstruments);
}
我不知道这里发生了什么,也不知道我是否应该这样做,但现在即使是非.mid.txt文件也可以毫无问题地复制。先读一读:谢谢,但我知道什么是空异常,我的观点是,代码可以与演示mid.txt文件配合使用,但是我在从网上导入随机MIDI时遇到了这个问题。请给我们看一下相关的代码。我们不太可能从网上下载一个随机软件包并安装它。如果所讨论的链接曾经消失,那么你的持久价值问题也不会消失。(Unity在杀掉站点/链接/路径方面享有盛誉)请先阅读以下内容:谢谢,但我知道什么是空异常,我的观点是,该代码可以与demo mid.txt文件配合使用,但我在从web导入随机MIDI时遇到了此问题。请向我们展示相关代码。我们不太可能从网上下载一个随机软件包并安装它。如果所讨论的链接曾经消失,那么你的持久价值问题也不会消失。(而Unity在清除网站/链接/路径方面享有盛誉)