Prefab MidiExternalPlay

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

You app will be able to play a MIDI from :

  • a MIDI file from a folder anywhere on the desktop or
  • from a web site or
  • from a byte array (by script).

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.


As usual, an API exists for more sophisticated needs in your application. See a full example with source code at the end of this page. Just below a quick overview:

From a MIDI on a web site:

midiExternalPlayer.MPTK_MidiName = ""; midiExternalPlayer.MPTK_Play();
Code language: JavaScript (javascript)

From a bye array:

using (Stream fsMidi = new FileStream("C:\xxx\Midi\DreamOn.mid", FileMode.Open, FileAccess.Read)) { byte[] data = new byte[fsMidi.Length]; fsMidi.Read(data, 0, (int)fsMidi.Length); midiExternalPlayer.MPTK_Play(data); }
Code language: JavaScript (javascript)

Add the prefab in your scene

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:
    • 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. 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 and associated to this variable in the inspector. 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 = ""; midiExternalPlayer.MPTK_Play(); } public void PlayFromData(string filepath) { // example with filepath = C:\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); } }
Code language: PHP (php)