在UnitySynth上导入自定义midis时的NullReference

时间:2015-07-24 03:39:52

标签: c# unity3d midi

所以,我从统一答案下载了团结项目。 这是链接:

UnitySynth link to download page in unity answers

我已经完成了我的原型,但是当我尝试加载从网上下载的自定义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转换为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;
    }

1 个答案:

答案 0 :(得分:0)

我通过删除代码中的try和catch元素解决了我自己的问题,如下所示:

   public bool LoadMidi(string file, bool UnloadUnusedInstruments)
{
    MidiFile mf = File.Open(file, FileMode.Open);
return LoadMidi(mf, UnloadUnusedInstruments);
}

我不知道这里发生了什么,如果我应该这样做,但现在即使非.mid.txt文件也可以毫无问题地被复制。