Prefab MidiExternalPlayer

MidiExternalPlayer is a Prefab, available with the Pro version, able to play Midi music from Midi file but outside the Midi database defined with MPTK:

Play Midi from :

  • a folder on your desktop or
  • a web site or
  • a byte array.

Some points:

  • This Prefab is available with MPTK Pro.
  • No line of script is necessary to use this Prefab (out of byte array, obviously). All the job can be done with the inspector.
  • But an API exists for more sophisticated needs in your application. See an example with source code at the end of this page.

Inspector parameters

When running:

The inspector inherits of all properties of the MidiFilePlayer inspector
  • Midi URL or file path type a file path or an URL, select from a folder, clear the content. Example of path:
    • Windows: file://C:/Users/Thierry/Desktop/Midi/WishYouWereHere.mid
    • Mac: file:///Users/thierry/Desktop/Nirvana.mid
    • Web: http://www.midiworld.com/midis/other/bach/bwv1060b.mid
    • Or use the browser icon (on left) to select a file from your desktop.

Others Foldout

The inspector inherits of all properties of the MidiFilePlayer inspector, see below for a detailed explanation of others properties .

Available Demo

As usual, a demonstration is available: non regression tests , help for using script, and fun if possible !

Load the scene “TestExternalMidiPlay” and look at the script example “TestMidiExternalPlayer.cs”

Integration of MidiExternalPlayer in your script

See TestMidiExternalPlayer.cs and events associated in the canvas gameobjects of TestExternalMidiPlay scene for the whole example.

using MidiPlayerTK; ... // MPTK component able to play a Midi file from an external source. // This PreFab must be present in your scene. public MidiExternalPlayer midiExternalPlayer;... ... // This method is fired from button (with predefined URI) // or input field in the screen. public void Play(string uri) { Debug.Log("Play from script:" + uri); midiExternalPlayer.MPTK_Stop(); midiExternalPlayer.MPTK_MidiName = uri; midiExternalPlayer.MPTK_Play(); } // Play a predefined Midi from the web public void PlayStatic() { midiExternalPlayer.MPTK_Stop(); midiExternalPlayer.MPTK_MidiName = "http://www.midiworld.com/midis/other/bach/bwv1060b.mid"; midiExternalPlayer.MPTK_Play(); } public void PlayFromData(string filepath) { // example with filepath = C:\Users\xxx\Midi\DreamOn.mid try { // try to load a byte array and play using (Stream fsMidi = new FileStream(filepath, FileMode.Open, FileAccess.Read)) { byte[] data = new byte[fsMidi.Length]; fsMidi.Read(data, 0, (int)fsMidi.Length); midiExternalPlayer.MPTK_Stop(); midiExternalPlayer.MPTK_Play(data); } } catch (System.Exception ex) { Debug.LogError(ex); } }