Post

 Resources 

Console

Playing Music in Managed DirectX 9


Playing an Audio File

Playing music in Managed DirectX 9 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.

  1. Imports Microsoft.DirectX
  2. Imports Microsoft.DirectX.AudioVideoPlayback


You will also need to create an instance of AudioVideoPlaybackís Audio object. An example is shown below.

  1. Dim someAudio As Audio = Nothing


Playing a sound file is now as simple as this:

  1. 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:

  1. someAudio.Play()


When you want it to stop playing, you can just use the Audio.Stop method:

  1. someAudio.Stop()


When you are done, make sure you dispose your Audio object and set it to equal Nothing so there are no memory leaks.

  1. someAudio.Dispose()
  2. 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:

  1. 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:

  1. Private Sub MusicEnds(ByVal sender As Object, ByVal e As System.EventArgs)
  2.         someAudio.CurrentPosition = 0
  3.     End Sub


When you no longer want the music to loop, just remove the event handler:

  1. RemoveHandler someAudio.Ending, AddressOf Me.MusicEnds


Conclusion

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).

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.




6 comments

Tutorial Console

Tutorial by:

Lachlan Purdy


Date: 2005 Feb 26


6 comments

Latest comment


by: Lachlan87

Thanks for the comments! I'd like to make one about playing sounds as well, but I'm not sure when I'll have time. If you need to know now, you can find a couple tutorials on that subject at gpwiki.org.

Post a Comment


Printer Friendly



Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.
There have been 22 visitors within the last 20 minutes
RSS News Feed