With Ginjo-Builder you have access to the full .NET 3 libraries. This tutorial will show you the basics of setting up a .NET Windows MediaPlayer object. (The full list of properties and methods can be found on MSDN: Windows.Media.MediaPlayer.) The following topics are covered in this tutorial:
The tutorial's code files are installed along with Ginjo-Builder and can be accessed from the 'Project' window that is shown when the editor starts. This tutorial is called '03_PlaySound' in the 'Tutorials' tab.
First we create a project and compiler options XML file. (See Render loop for a more detailed description of creating a project.)
Open "PlaySound.xml", and just like in Render loop start with the <exe> and <startup> elements. (For the full list of XML tags see Compiler options.) We will also need to import three .NET libraries, so we will add a ReferencedAssemblies section:
<?xml version="1.0" encoding="utf-8"?> <root> <exe path=".\game.exe" /> <startup name="SoundPlayer.main" /> <ReferencedAssemblies> <dotnet name="System.Windows.Forms" /> <dotnet name="PresentationCore" /> <dotnet name="WindowsBase" /> </ReferencedAssemblies> </root>
The System.Windows.Forms library is required for Windows.Forms.OpenFileDialog, and PresentationCore and WindowsBase are required for Windows.Media.MediaPlayer.
In our XML file we specified the startup function as "SoundPlayer.main". Double-click on "SoundPlayer.gbc" in the project tree to open it, and add the following function, called "main". When you run your program this is the function where execution starts.
function main(var:string cmdArgs[]) returns Int32 { }
The following code creates a MediaPlayer and an OpenFileDialog. We set the filter of the dialog and open the selected file in our MediaPlayer and start playing the file. Our code becomes:
function main(var:string cmdArgs[]) returns Int32 { var:system.Windows.Media.MediaPlayer Player=new system.Windows.Media.MediaPlayer() var:system.Windows.Forms.OpenFileDialog Dialog=new system.Windows.Forms.OpenFileDialog() Dialog.filter="MP3 files (*.mp3)|*.mp3|All files (*.*)|*.*" if(Dialog.showdialog()){ Player.Open(new Uri(Dialog.FileName)) Player.Play() } }
We also need an Irrlicht window, otherwise the program just exits after opening the music file. The final code becomes:
function main(var:string cmdArgs[]) returns Int32 { var:system.Windows.Media.MediaPlayer Player=new system.Windows.Media.MediaPlayer() var:system.Windows.Forms.OpenFileDialog Dialog=new system.Windows.Forms.OpenFileDialog() Dialog.filter="MP3 files (*.mp3)|*.mp3|All files (*.*)|*.*" if(Dialog.showdialog()){ Player.Open(new Uri(Dialog.FileName)) Player.Play() } var:gb.IrrlichtDevice device = gb.IrrlichtDevice.CreateDevice() //Also set our window's title device.SetWindowCaption('Sound player') while (device.Run()){ //we don't need anything here } }