« How to easily Shutdown, Restart, and Log Off the Computer without APIs (Update: Added VB 6.0 codes) | Home | Able to use Controls/Class made with Visual Basic 2005 in Visual Basic.NET 2002/2003? »
Easily Play/Pause/Resume/Stop MP3/Wav/WMA files
By Jason | August 30, 2007
There are many paths you can take. 3rd Party librarys, Windows Media Player component, Windows API and more. The answer i'm going to give is using the already established Windows APIs.
The APIs are connected to the Windows Multi-Media System. Or winmm.dll. This multi-media system is installed on every modern Windows OS. So, apart from having to install special codecs for uncommon formats, you can play many of the popular formats without any special setup or installation. Anyways, below is the API to get started...
'
'Api to send the commands to the mci device.
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _Integer, ByVal hwndCallback As Integer) As Integer
With this API you can do a great deal of playback and even some recording features for your applications. This article is meant to be very simple and a basic starting point only. But if your interested to learn more about this mciSendString interface, then goto my website: http://www.vbcodesource.org under the VB.NET Tutorials Page, and download my mciSendString Tutorial which goes in-depth using these APIs. You can also goto this Microsoft link to access the reference for this API: http://msdn2.microsoft.com/en-us/library/ms710815.aspx . You can also goto my website and download one of my full-featured librarys. Lets get started on the purpose of this answer article.
Below is a command sent to the MCI system to open and setup a new device to play the sound that is specified as the path...
'Put " and " at the beginning and end of the filename and path. The device could fail without 'these quotes. You can convert the long filename to the short filename and not need 'the quotes. That would just be more code that really isn't needed.
Dim _fileToPlay As String
_fileToPlay = Chr(34) + ("c:\MyFileToPlay.MP3) + Chr(34)
The example variable above will contain the filename you want to play.
-----------------------------------------------------------------------------------------------
Now setup and load the device with the file that is specified in the _fileToPlay example variable.
'Let the command interface decide which device to use. Just specify the alias. The alias is the name you use to program that device. You can create multiple devices to play media at the same time and such with different alias's.
mciSendString("open " & _fileToPlay & " alias myDevice", Nothing, 0, 0)
The device is now setup and you just need to send the command for mci to play your file.
--------------------------------------------------------------------------------------------
mciSendString("play customAlias", Nothing, 0, 0)
The command above will start playing the mp3/wav/wma or whatever file you specified in the _fileToPlay variable.
-------------------------------------------------------------------------
You can also setup the device to stop and/or pause/resume the playback of your media. as shown below....
mciSendString("stop myDevice, Nothing, 0, 0)
mciSendString("pause myDevice", Nothing, 0, 0)
mciSendString("resume myDevice", Nothing, 0, 0)
And this is all you have to do to get quick, and easy playback in your Visual Basic 5.0/6.0; Visual Basic.NET 2002/2003; Visual Basic 2005 applications! I do hope you get something useful from this small answer article.
Jason
Popularity: 57% [?]
Topics: - .All VB (Related to All) |



(19 votes, average: 3.37 out of 5)
January 19th, 2008 at 11:26 pm
Thanks, I appreciate your work. I was using MMPlayer (MCI32.OCX) in VS2005. When I deployed my project I kept getting a .Net 2.0 error. I had the OCX registered, but I kept getting the error. I probably could have made it work but thought I should go with something better that could play more than Waves. Your code got me, what I think, on the right track. There are a few underscores that mess with the code if copying and pasting from the page.
Thanks again.
January 20th, 2008 at 2:11 am
Hi, unfortunately the text editor messes around with the text and changes the format/text for some characters. I know it gets annoying. I am trying to find a better richtrext editor. Sorry
Jason
March 7th, 2008 at 12:17 pm
the only thing I can't seem to get is when you talk about the devices...If I just want to rip a music file off a flashdrive persay, to simply play when I click a button on my form, what "device" do I use? Is this something we make up that the computer reads or is it a set constant I need to find?
March 7th, 2008 at 7:28 pm
Hi, a device is what is used to playback your media. Check out the tutorial at this link and let me know if you still need help. http://www.vbforfree.com/?p=155
Jason
April 2nd, 2008 at 10:30 pm
What do we add this code to on the form (picture box, label etc) for it to work?
April 13th, 2008 at 4:28 am
I would like to thank you greatly for this code. I've searched forever for an easy solution to playing MP3's without external libraries or custom controls. I was told by many that mciSendString was not capable of playing MP3 files, and you have just proved them all wrong.
However, it took me a great deal of time to get your code working. You should consider revising it. There are missing quotation marks, some typos ("customAlias" doesn't match up with "myDevice"), and some things you used aren't valid in VB5, like the underscores or the use of "Nothing" as opposed to "vbNullString". Also your function call examples require them to be assigned to a return variable, which may be misleading for newcomers.
But thank you nonetheless.
April 19th, 2008 at 10:44 pm
Thankyou for the feedback. You are right about the post needing code/format updates. I will do it as soon as I am able too. Thanks again
Jason
May 13th, 2008 at 9:48 am
great, but how can you show the ID3 tag information of a musicfile? i want to show it in a label. can you give me a code or a link? tnxx
robbin
November 23rd, 2008 at 9:40 pm
Sorry but this code is crap, if your a good VB programmer and went to college for it, you would know that using strict options on does not cooperate with this code.
All I am looking for is a simple MP3 code and this isn't doing the trick.
January 5th, 2009 at 5:43 pm
Hmmm.
Waiting for Ed to post his own, "non crap", code so's we can all git our schoolin' on how'z to be a big time VB programmer.
Ed?
January 22nd, 2009 at 5:07 pm
Don't wait for Ed. He was a student of mine and I can assure you he did not pass his class.
June 4th, 2009 at 5:57 pm
My.computer.Audio.play("file location")
June 30th, 2009 at 2:52 am
For those who can't get it working in VB6 - change the Nothing to "". Example:
mciSendString "play theFile", "", 0, 0
October 22nd, 2009 at 3:55 pm
Maybe if Ed took a real programming language in college he wouldn't be looking here for a tutorial on how to use mciSendString()
December 30th, 2009 at 9:52 am
i facing a problem when using mciSendString, when the file path is a UNC path or when the file is on a network, more than this when the file path or name have a space on it (ex: C:\file name.mp3) also this API function will not work. Am I missing something? or is ther any trick?
Thanks in advance ...
December 30th, 2009 at 1:07 pm
newCommer: whenever you have a special path, like a network path, be sure to wrap the path in quotes. Otherwise it could fail. Someone else had a problem of the path being a mapped network path and it would fail. He was using the:
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
api call and it would not work. I had him put the path in quotes ( "my path" ) and it worked fine.
But since yours is an actual UNC path, I am not exactly sure why its failing. Try using the GetShortPathName api function I quoted above and see if that will help you.
Otherwise, be sure to wrap the unc path literally in quotes ( "mypath" ) before passing the path to the mci function.
If its still a no go, let me know.
Jason
January 1st, 2010 at 5:08 pm
I am having a problem playing an mp3 file. It does not start playing at the beginning. It starts maybe a 1/4 second in.
retVal = mciSendString("close all", 0, 0, 0)
ErrorSuccess = mciGetErrorString(retVal, errorString, 128)
strFilename = Chr(34) & strFilename & Chr(34)
retVal = mciSendString("open " & strFilename & " type mpegvideo alias oursong", 0, 0, 0)
ErrorSuccess = mciGetErrorString(retVal, errorString, 128)
retVal = mciSendString("play oursong ", 0, 0, 0)
ErrorSuccess = mciGetErrorString(retVal, errorString, 128)
Any Ideas
January 2nd, 2010 at 7:37 pm
Randy, try removing the space after oursong on the following line of code.
retVal = mciSendString("play oursong ", 0, 0, 0)
Make it:
retVal = mciSendString("play oursong", 0, 0, 0)
That could be a problem. You can also try adding the code below once you open and create your alias...
Like below:
retVal = mciSendString("seek oursong to start", 0, 0, 0)
Then try playing it after you send that seek command. See if any of this fixes your problem.
Jason
January 28th, 2010 at 7:01 am
Dear sir,
I need to play mp3 in visual basic 2008
if possible i also want to see the time of played sound in textbox on a form.
thanks
email me please mrooble@hotmail.com
April 10th, 2010 at 10:17 am
Hi,
I tryed your code but I had to make much changes to get it running without anny errors. It still doesn't work.
My code
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Load_Click()
Dim play As String
Path.ShowOpen
If Path.FileName = "" Then
Exit Sub
End If
play = Chr(34) & Path.FileName & Chr(34)
mciSendString "open " & play & " t", vbNullString, 0, 0
mciSendString "play t", vbNullString, 0, 0
End Sub
Am I missing something?
Please help,
Jason
April 14th, 2010 at 10:41 am
@Jason8100
Try this way...
Dim play As String
Path.ShowOpen
If Path.FileName = "" Then
Exit Sub
End If
play = Chr(34) & Path.FileName & Chr(34)
mciSendString ("open " & play & "alias t", vbNullString, 0, 0)
mciSendString ("play t", vbNullString, 0, 0)
End Sub
August 26th, 2010 at 11:57 pm
Hi Jason, thanks for this. It's still helping people three years after you wrote it!
August 29th, 2010 at 7:29 pm
Thanks John, I can't believe its was 3 years ago....