Playing Music in Managed DirectX
Playing an Audio File
Playing music in Managed DirectX is very easy, so this will be a short tutorial. Music playback is handled by the AudioVideoPlayback module of DirectX, so you must reference the appropriate dll’s:
- Microsoft.DirectX and Microsoft.DirectX.AudioVideoPlayback
Next, you should import them into the class that will handle the music playback, as demonstrated.
Imports Microsoft.DirectX Imports Microsoft.DirectX.AudioVideoPlayback
You will also need to create an instance of AudioVideoPlayback’s Audio object. An example is shown below.
Dim someAudio As Audio = Nothing
Playing a sound file is now as simple as this:
someAudio = New Audio("onestop.mid", True)
We have just told our someAudio object to be set as a new audio object, passing two parameters. The first one is the path to the Audio file we wish to play. This could be a Midi file, or mp3, etc. The second parameter tells the Audio object whether or not it should automatically start playing the music file. If you passed False instead of True, you could command the Audio object to begin playing with this command:
When you want it to stop playing, you can just use the Audio.Stop method:
When you are done, make sure you dispose your Audio object and set it to equal Nothing so there are no memory leaks.
someAudio.Dispose() someAudio = Nothing
Making Your Music File Loop
The AudioVideoPlayback section of Managed DirectX has no built in function to make your music loop, but it’s really pretty easy to do yourself. After you initialize your someAudio object, add an event handler for the someAudio.Ending event, like this:
AddHandler someAudio.Ending, AddressOf Me.MusicEnds
Next, you’ll need to write out the MusicEnds method for your class. This method will simply set the current position in the audio file to zero:
Private Sub MusicEnds(ByVal sender As Object, ByVal e As System.EventArgs) someAudio.CurrentPosition = 0 End Sub
When you no longer want the music to loop, just remove the event handler:
RemoveHandler someAudio.Ending, AddressOf Me.MusicEnds
That sums up the basics of playing music with Managed DirectX. For your convenience, I have described a few other handy built in methods:
someAudio.State() - This method returns the current state of the audio file, which can be Playing, Stopped, or Paused.
someAudio.CurrentPosition() - This method can be used to check to see how much of the music file has been played, or you can use it to skip around in the music file.
someAudio.Duration() - This method will tell you how long the Audio file is.
someAudio.FromUrl() - This function can be used to load a music file off the internet!
someAudio.Pause - This function will pause the playback of the Audio.
someAudio.Volume() - This can be used to change the volume of the Music file, which can be 0 (maximum) to -10000 (which is silent) (-10000 is a negative value).
There are more functions as well, but these are the ones I suspect you’ll be using most often. If you need any further assistance, I can usually be found on the forums on this site.