You’ll need the Windows Media Player application to do this, it’s pretty simple overall. I’ll show you how to use the “Open file Dialog”, or as I’ll refer to it here, the OFD. If you don’t have WMP, the latest version, WMP11 can be downloaded from Microsofts website.
In your toolbox, right click on some gray space and select “Add Components”. After a minute or two, the window that opens will populate. Click the “COM Library” at the top tabs, and then, after that list has populated, find “Windows Media Player Control” (or similar). Check the box, and then click OK.
Drag WMP control over to your form. A miniature media base will be displayed. At the moment, it does nothing. So we’ll add some code Add an open file dialog, a textbox, and a button, calling them url_text and get_url_btn respectively.
Double click the button, and enter the following code:
dim ofd as new openfiledialog
ofd.show
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then ‘execute following if user clicks OK. Me.url_text.Text = ofd.FileName Me.AxWindowsMediaPlayer1.URL = ofd.FileName Me.Text = “Playing” End IfThen double click the WMP control to enter WMP’s “Playstate.change”Enter the following
Select Case Me.AxWindowsMediaPlayer1.playStateCase 1 Me.Text = “Stopped”Case 2 Me.Text = “Paused”Case 3 Me.Text = “Playing” End SelectThere are many more, adding another “Case” will open intelliSense, and then using the up/down arrow keys scrolling through the choices and waiting for a second will open a tooltip, for example
WMPLib.WMPPlayerState.wmppsMediaEnded = 8 So:
Case 8
Me.text = “Media Ended”
You now have a fully functional music player. You can skip the textbox part, you don’t have to have the URL in the textbox. The following would suffice.
Me.AxWindowsMediaPlayer1.URL = ofd.FileName
//////Pro\\\\\\\
Want to do it pure-vb style?
Play = My.Computer.Audio.Play(TextBox1.Text)
Stop = My.Computer.Audio.Stop
there are more, those are the simple ones. It’s very basic, using WMP control allows you to play a wider variety of formats, doing it pure-vb only allows you to play .wav files.
Got another tutorial you’d like to see? Something complicated made easy?
No comments:
Post a Comment