Easily Play, Stop, Pause, and Resume MP3/Wav/WMA files in VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (31 votes, average: 3.42 out of 5)
 

This article will use the already established Windows API function MCISendString. The function is part of the Windows Multi-Media system in the winmm.dll using Multimedia Command Strings (MCI). WinMM is available on pretty much every modern Windows OS. Apart from having to install special codecs for uncommon audio/music formats, you can play many of the popular formats without any special setup or installation. If you want to know more about Multimedia Command strings you can check out a beginner tutorial I made at this link.

While the source code in this post is in VB.NET, you can simply change ‘as Integer‘ to ‘as Long‘. Otherwise the code should be fine.

Below is the API Function 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

 

The next snippet is for the path to the file.

'The Chr(34) code is to put quotes at the beginning and end of the file's path.
'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)

 

Below is a command sent to the MCI system to open and setup a new device to play the file that is specified in the fileToPlay 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", vbNullString, 0, 0

 

The device is now setup and you just need to execute the simple commands shown below.


This will start playing the mp3/wav/wma or whatever file you specified in the fileToPlay.

MCISendString "play customAlias", vbNullString, 0, 0

 

The rest of the code below has the commands to Stop, Pause, and Resume the playback of your media.

MCISendString "stop myDevice", vbNullString, 0, 0

MCISendString "pause myDevice", vbNullString, 0, 0

MCISendString "resume myDevice", vbNullString, 0, 0

 

Then when your done, use the snippet below to release all of the resources used by MCI.

MCISendString "close customAlias", vbNullString, 0, 0

And that is all you have to do to have quick, simple sound playback in your Visual Basic 6.0; Visual Basic.NET, Visual Basic 2005, VB 2008, VB 2010, and newer applications! I hope you got something useful from this post. 🙂

Jason

29 thoughts on “Easily Play, Stop, Pause, and Resume MP3/Wav/WMA files in VB and VB.NET

  1. Jerry

    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.

    Reply
  2. Jason Post author

    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

    Reply
  3. Robert

    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?

    Reply
  4. Josh

    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.

    Reply
  5. Jason Post author

    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

    Reply
  6. Robbin

    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

    Reply
  7. Ed

    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.

    Reply
  8. Brian

    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?

    Reply
  9. James

    For those who can’t get it working in VB6 – change the Nothing to “”. Example:

    mciSendString “play theFile”, “”, 0, 0

    Reply
  10. Steve

    Maybe if Ed took a real programming language in college he wouldn’t be looking here for a tutorial on how to use mciSendString()

    Reply
  11. newCommer

    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 …

    Reply
  12. Jason Post author

    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

    Reply
  13. Randy

    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

    Reply
  14. Jason Post author

    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

    Reply
  15. Jason8100

    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

    Reply
  16. Dragan

    @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

    Reply
  17. petester47

    I got every thing to work using the following code:
    the three buttons used in to play .mp3 files and the fourth plays only .wav
    Hopefully it will past in ok:

    Public Class Form1

    Private Declare Function music 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 Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Play.Click
    Dim FileName As String
    FileName = Chr(34) & (“e:\line.MP3”) & Chr(34)

    music(“open ” & FileName & ” alias myDevice”, Nothing, 0, 0)
    music(“play myDevice”, Nothing, 0, 0)

    End Sub

    Private Sub Pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pause.Click

    music(“pause myDevice”, Nothing, 0, 0)
    End Sub

    Private Sub Stop_Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stop_Play.Click

    music(“close myDevice”, Nothing, 0, 0)

    End Sub

    Private Sub Play_Wav_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Play_Wav.Click

    ‘ plays only wav files (I think)
    My.Computer.Audio.Play(“e:\line.wav”)
    ‘ My.Computer.Audio.Stop() ‘this line will stio the wav from playing

    End Sub
    End Class

    Reply
  18. Harold

    I have been searching for a solution to play MP3 files.

    Your solution was too easy. Thanks for posting this.

    I literally copied and pasted into Visual Basic 2010, had to change the Double Quotes and ran a test.

    Worked Perfectly. I was ready to give up on this thread when I was reading everyones comments above and I am so glad I did not.

    Again, thank you and great job.

    Reply
  19. Olis

    Hi Jason, thank you so much for the great code, i could work on it on my apps welcome screen with this code below, I need help so that the mp3 will be replay back automatically ?
    I’m using VB2008 now

    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 Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim FileName As String
    FileName = Chr(34) & “C:\WS.mp3” & Chr(34)
    mciSendString(“open ” & FileName & ” alias t”, vbNullString, 0, 0)
    mciSendString(“play t”, vbNullString, 0, 0)
    End Sub

    Reply

Leave a Reply