在UnitySynth上导入自定义Midis时为NullReference

克里斯蒂安·塞隆

因此,我从统一答案中下载了统一项目。链接在这里:

UnitySynth链接以统一答案下载页面

我已经完成了原型制作,但是尝试加载从网络下载的自定义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;
    }
克里斯蒂安·塞隆

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

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

我不知道这里会发生什么以及是否应该这样做,但是现在即使是非.mid.txt文件也可以毫无问题地进行复制。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章