Overview
MidiExternalPlay is a prefab included in the Pro version that can play MIDI music from a MIDI file, which is not part of the MPTK-defined MIDI database.
You app will be able to play a MIDI:
- From a file in a folder anywhere on the device.
- From a web site with a URL.
- From a byte array (by script).
Some points:
- This prefab is available with MPTK Pro.
- Using this prefab does not require any scripting. All tasks can be accomplished through the inspector.
API
For more advanced requirements in your application, an API is typically available. A complete example with source code can be found at the bottom of this page.
Important note: The MidiExternalPlay class inherits from both the MidiFilePlayer and MidiSynth classes. Consequently, all attributes and methods from these classes are accessible through MidiExternalPlay.
Below are some quick examples:
Play from a MIDI on a web site:
midiExternalPlayer.MPTK_MidiName = "http://www.midiworld.com/midis/other/c2/bolero.mid";
midiExternalPlayer.MPTK_Play();
Code language: JavaScript (javascript)
Play from a byte 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)
Loop inside the MIDI loaded
MidiExternalPlayer midiExternalPlayer;
private void Start()
{
midiExternalPlayer = FindObjectOfType<MidiExternalPlayer>();
if (midiExternalPlayer != null)
{
// Event trigger when midi file start playing
midiExternalPlayer.OnEventStartPlayMidi.AddListener(StartPlay);
midiExternalPlayer.MPTK_MidiName = yourUri;
midiExternalPlayer.MPTK_Play();
}
}
public void StartPlay(string midiname)
{
Debug.Log("Start Midi " + midiname);
Debug.Log("Duration: " + midiExternalPlayer.MPTK_Duration.TotalSeconds);
midiExternalPlayer.MPTK_InnerLoop.Enabled = true;
midiExternalPlayer.MPTK_InnerLoop.Start = 10000;
midiExternalPlayer.MPTK_InnerLoop.Resume = 10000;
midiExternalPlayer.MPTK_InnerLoop.End = 10200;
midiExternalPlayer.MPTK_InnerLoop.Log = true;
}
Code language: C# (cs)
Add the prefab in your scene
Before scripting with MidiExternalPlay, it is necessary to add a MidiExternalPlay prefab to your scene.
Inspector parameters
When running:
- 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.
- Windows
Others Foldout
The inspector inherits of all properties of the MidiFilePlayer inspector, see below for a detailed explanation of others properties .
- Foldout Midi Parameters See here
- Foldout Events See here
- Foldout Midi Info See here
- Synth Parameters See here
- Default Editor See here
Available Demo
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;<br>...<br>// MPTK component able to play a Midi file from an external source.<br>// This prefab must be present in your scene and associated to this variable in the inspector.<br>public MidiExternalPlayer midiExternalPlayer;...<br>...<br>// This method is fired from button (with predefined URI) <br>// or input field in the screen.<br>public void Play(string uri)<br>{<br> Debug.Log("Play from script:" + uri);<br> midiExternalPlayer.MPTK_Stop();<br> midiExternalPlayer.MPTK_MidiName = uri;<br> midiExternalPlayer.MPTK_Play();<br>}<br>// Play a predefined Midi from the web<br>public void PlayStatic()<br>{<br> midiExternalPlayer.MPTK_Stop();<br> midiExternalPlayer.MPTK_MidiName = "http://www.midiworld.com/midis/other/bach/bwv1060b.mid";<br> midiExternalPlayer.MPTK_Play();<br>}<br><br>public void PlayFromData(string filepath)<br>{<br> // example with filepath = C:\xxx\Midi\DreamOn.mid<br> try<br> {<br> // try to load a byte array and play<br> using (Stream fsMidi = new FileStream(filepath, FileMode.Open, FileAccess.Read))<br> {<br> byte[] data = new byte[fsMidi.Length];<br> fsMidi.Read(data, 0, (int)fsMidi.Length);<br> midiExternalPlayer.MPTK_Stop();<br> midiExternalPlayer.MPTK_Play(data);<br> }<br> }<br> catch (System.Exception ex)<br> {<br> Debug.LogError(ex);<br> }<br>}<br>
Code language: HTML, XML (xml)