Results 1 to 19 of 19

Thread: Play MP3

  1. #1

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Play MP3

    Hello...

    How can I play .mp3 and .wav files in VB.NET. I don't have to use Media Player Control rather have to use .NET class.

    Please help.

    Thanks.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Opps , and this demo I've just created for MP3 Files .
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    can you post the code as text please?

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Sure , anyone has any problem with the project version , he has to get the converter from below link . Winrar is giving you error , get it and unpack the demo and it should all go smooth .
    VB Code:
    1. Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Integer
    2.    
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         'Play Button
    5.         Dim OFD As New OpenFileDialog
    6.         OFD.Filter = "MP3 Files (*.MP3)|*.MP3"
    7.  
    8.         If OFD.ShowDialog = DialogResult.OK Then
    9.             mciExecute("OPEN " & OFD.FileName & " TYPE MPEGVideo ALIAS mp3song ")
    10.             mciExecute("PLAY mp3song")
    11.         End If
    12.  
    13.  
    14.     End Sub
    15.  
    16.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    17.         'Stop Button
    18.         mciExecute("CLOSE mp3song")
    19.     End Sub

  6. #6
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    this gives me some error in the OPEN line. maybe this only works for vs2003? any ideas what the code for the older version might be like?

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This uses API which is not specific to any version of VS.NET . I used that in both VB.NET 2002 & 2003 and even before in VB6 days and they're working fine . How did you use anyway ?

  8. #8
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    ok. i found out the problem. if the filename or path has a space in it, then then i get an error. i made a file c:\test.mp3 and it works.
    how do i get around this severe limitation?

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's right . I'll try to look for a fix but not now since I'm busy with something else . Search MSDN for any associated errors with mciExecute API Function , you might come up with a treat .

  10. #10
    Member
    Join Date
    Nov 2003
    Location
    Amsterdam, The Netherlands
    Posts
    53
    Why don't you try BASS?

  11. #11
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    If i remember rite, you need to put the file name in the old dos 8.3 format eg. c:\progra~1\asdf~1\mp3n~1.mp3

    In vb6 you could use The GetShortPathName API

    too tired to look it up rite now [6:04 am here, I need some sleep ]
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I've tried this proj in VB6 on the same path and same file and worked fine . I does use GetShortPathName API as <ABX said . It's easy to port it to VB.NET .
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    sorry can you post the code as text )I dont have vb on the comp i use for internet right now.) and my vb comp dosn't have any removable storage drives.

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB6 Code

    Module File
    VB Code:
    1. '*************************************
    2. 'Copyright © 2001 by Alexander Anikin
    3. 'e-mail: [email][email protected][/email]
    4. 'http://www.hotmix.kiev.ua
    5. '*************************************
    6. Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    7. "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    8.  
    9. Public Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
    10.  
    11. Public FileOP As String
    12. Public Const OFN_FILEMUSTEXIST = &H1000
    13. Public Const OFN_HIDEREADONLY = &H4
    14.  
    15. Public OFN As OPENFILENAME
    16. Public Type OPENFILENAME
    17.         lStructSize As Long
    18.         hwndOwner As Long
    19.         hInstance As Long
    20.         lpstrFilter As String
    21.         lpstrCustomFilter As String
    22.         nMaxCustFilter As Long
    23.         nFilterIndex As Long
    24.         lpstrFile As String
    25.         nMaxFile As Long
    26.         lpstrFileTitle As String
    27.         nMaxFileTitle As Long
    28.         lpstrInitialDir As String
    29.         lpstrTitle As String
    30.         flags As Long
    31.         nFileOffset As Integer
    32.         nFileExtension As Integer
    33.         lpstrDefExt As String
    34.         lCustData As Long
    35.         lpfnHook As Long
    36.         lpTemplateName As String
    37. End Type

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    In a form

    VB Code:
    1. Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long
    2. Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
    3. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    4. Private mmOpen As String, sec As Integer, mins As Integer
    5. Dim nFileName As String
    6. Public Function MP3Play(wndHandle As Long, sFileName As String)
    7.   Dim cmdToDo As String * 255
    8.   Dim dwReturn As Long
    9.   Dim ret As String * 128
    10.   Dim tmp As String * 255
    11.   Dim lenShort As Long
    12.   Dim ShortPathAndFie As String, glo_HWND As Long
    13.     If Dir(sFileName) = "" Then
    14.          mmOpen = "Error with input file"
    15.          Exit Function
    16.     End If
    17.   lenShort = GetShortPathName(sFileName, tmp, 255)
    18.   ShortPathAndFie = Left$(tmp, lenShort)
    19.   glo_HWND = wndHandle
    20.   cmdToDo = "open " & ShortPathAndFie & " type MPEGVideo Alias MP3Play"
    21.   dwReturn = mciSendString(cmdToDo, 0&, 0&, 0&)
    22.   If dwReturn <> 0 Then  'not success
    23.      mciGetErrorString dwReturn, ret, 128
    24.      mmOpen = ret
    25.      MsgBox ret, vbCritical
    26.      Exit Function
    27.   End If
    28.   mmOpen = "Success"
    29.   mciSendString "play MP3Play", 0, 0, 0
    30. End Function
    31.  
    32. Public Function MP3Pause()
    33.   mciSendString "pause MP3Play", 0, 0, 0
    34. End Function
    35.  
    36. Public Function MP3UnPause()
    37.   mciSendString "play MP3Play", 0, 0, 0
    38. End Function
    39.  
    40. Public Function MP3Stop() As String
    41.   mciSendString "stop MP3Play", 0, 0, 0
    42.   mciSendString "close MP3Play", 0, 0, 0
    43. End Function
    44.  
    45. Private Sub Check1_Click()
    46. Timer1 = Not Timer1
    47. If Timer1 Then Check1.Value = 1 Else Check1.Value = 0
    48. End Sub
    49.  
    50. Private Sub Command1_Click()
    51. Command1.Enabled = False
    52. Command5.Enabled = False
    53.  
    54. Open_file
    55.  
    56. End Sub
    57.  
    58. Private Sub Command2_Click()
    59. If Command2.Caption = "Pause" Then
    60.   Command2.Caption = "Play"
    61.   MP3Pause
    62. Else
    63.   Command2.Caption = "Pause"
    64.   MP3UnPause
    65. End If
    66.  
    67. End Sub
    68.  
    69. Private Sub Command3_Click()
    70. Command2.Enabled = False
    71. Command3.Enabled = False
    72. Command4.Enabled = False
    73. Command1.Enabled = True
    74. Command2.Caption = "Pause"
    75. MP3Stop
    76. End Sub
    77.  
    78. Private Sub Command4_Click()
    79.   mciSendString "stop MP3Play", 0, 0, 0
    80.   mciSendString "play MP3Play from 0", 0, 0, 0
    81.   Command2.Caption = "Pause"
    82.  
    83. End Sub
    84.  
    85. Private Sub Command5_Click()
    86. Unload Me
    87. End Sub
    88.  
    89. Private Sub Form_Unload(Cancel As Integer)
    90. MP3Stop
    91. End Sub
    92.  
    93. Private Sub Open_file()
    94. Dim cderr As Long
    95. OFN.lStructSize = 76&
    96. OFN.hwndOwner = Form1.hWnd
    97. OFN.lpstrFilter = "mp3 (*.mp3)" + Chr(0) + "*.mp3" + Chr(0) + Chr(0)
    98. OFN.lpstrCustomFilter = String(256, Chr(0))
    99. OFN.nMaxCustFilter = 256
    100. OFN.lpstrFile = "" + String(512, Chr(0))
    101. OFN.nMaxFile = 512
    102. OFN.lpstrFileTitle = String(256, Chr(0))
    103. OFN.nMaxFileTitle = 256
    104. OFN.flags = OFN_FILEMUSTEXIST + OFN_HIDEREADONLY
    105. '************
    106. DoEvents
    107. '************
    108. If GetOpenFileName(OFN) Then
    109.     OFN.lpstrFile = Mid(OFN.lpstrFile, 1, InStr(OFN.lpstrFile, Chr(0)) - 1)
    110.     nFileName = OFN.lpstrFile
    111.     OFN.lpstrFileTitle = Mid(OFN.lpstrFileTitle, 1, InStr(OFN.lpstrFileTitle, Chr(0)) - 1)
    112.     InitialDir = Left(OFN.lpstrFile, Len(OFN.lpstrFile) - Len(OFN.lpstrFileTitle))
    113. Else
    114.     cderr = CommDlgExtendedError
    115.     GoTo ex
    116. End If
    117. MP3Play hWnd, nFileName
    118. Command2.Enabled = True
    119. Command3.Enabled = True
    120. Command4.Enabled = True
    121. Command5.Enabled = True
    122. Exit Sub
    123. ex:
    124. Command1.Enabled = True
    125. Command5.Enabled = True
    126. End Sub
    127.  
    128. Public Function IsPlaying() As Boolean
    129. Static s As String * 30
    130. mciSendString "status MP3Play mode", s, Len(s), 0
    131. IsPlaying = (Mid$(s, 1, 7) = "playing")
    132. End Function
    133.  
    134. Private Sub Timer1_Timer()
    135. If IsPlaying = False And Command1.Enabled = False And Command2.Caption = "Pause" Then Command4_Click
    136. End Sub

  16. #16
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Talking

    I was bored; bored enough to make a binary clock but I also made a class for playing mp3 files in vb .net

    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.IO
    5.  
    6. Public Class cMCI
    7.  
    8.     Private Const MCI_DEFAULT_ALIAS As String = "MP3Play"
    9.  
    10.     Public Enum MCI_STATUS_MODE
    11.         Not_Ready = 1
    12.         Paused = 2
    13.         Playing = 4
    14.         Stopped = 8
    15.         Open = 16
    16.         Parked = 32
    17.         Recording = 64
    18.         Seeking = 128
    19.         Unknown = 0
    20.     End Enum
    21.  
    22.     Private strMCIAlias As String
    23.  
    24. #Region " API DECLARE STATEMENTS "
    25.  
    26.     Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Integer, ByVal lpstrBuffer As String, ByVal uLength As Integer) As Integer
    27.     Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Integer) As Integer
    28.     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
    29.  
    30. #End Region
    31.  
    32. #Region " Private Class Member Variables "
    33.  
    34.     Private m_strFilePath As String
    35.  
    36. #End Region
    37.  
    38. #Region " Public Class Properties "
    39.  
    40.     Public ReadOnly Property MCI_STATUS() As MCI_STATUS_MODE
    41.         Get
    42.             Return GetStatus()
    43.         End Get
    44.     End Property
    45.  
    46.     Public Property FilePath() As String
    47.         Get
    48.             Return m_strFilePath
    49.         End Get
    50.         Set(ByVal Value As String)
    51.             m_strFilePath = Value
    52.         End Set
    53.     End Property
    54.  
    55.  
    56. #End Region
    57.  
    58. #Region " Private Functions "
    59.  
    60.     Private Function ThrowMCIError(ByVal errCode As Integer) As Boolean
    61.         Dim strBuffer As String = Space(255)
    62.  
    63.         mciGetErrorString(errCode, strBuffer, 255)
    64.  
    65.         strBuffer = strBuffer.Trim(Chr(0)).TrimEnd(Chr(34))
    66.  
    67.         Throw New MCISendStringException(errCode, strBuffer)
    68.  
    69.         Return True
    70.     End Function
    71.  
    72.     Private Function GetShortPath(ByVal strFullPath As String) As String
    73.  
    74.         Dim strBuffer As String = Space(255)
    75.  
    76.         Dim retVal As Integer = GetShortPathName(strFullPath, strBuffer, 255)
    77.  
    78.         Dim I As Integer
    79.  
    80.         Return strBuffer.Trim(CChar(" ")).Trim(Chr(0))
    81.     End Function
    82.  
    83.     Private Function GetStatus() As MCI_STATUS_MODE
    84.  
    85.         Dim strBuffer As String = Space(255)
    86.  
    87.         mciSendString("status " & strMCIAlias & " mode", strBuffer, strBuffer.Length, 0)
    88.  
    89.         Dim strMode As String = strBuffer.TrimEnd(Chr(0))
    90.  
    91.         Select Case strMode
    92.             Case Is = "not ready"
    93.                 Return MCI_STATUS_MODE.Not_Ready
    94.             Case Is = "paused"
    95.                 Return MCI_STATUS_MODE.Paused
    96.             Case Is = "playing"
    97.                 Return MCI_STATUS_MODE.Playing
    98.             Case Is = "stopped"
    99.                 Return MCI_STATUS_MODE.Stopped
    100.             Case Is = "open"
    101.                 Return MCI_STATUS_MODE.Open
    102.             Case Is = "parked"
    103.                 Return MCI_STATUS_MODE.Parked
    104.             Case Is = "recording"
    105.                 Return MCI_STATUS_MODE.Recording
    106.             Case Is = "seeking"
    107.                 Return MCI_STATUS_MODE.Seeking
    108.             Case Else
    109.                 Return MCI_STATUS_MODE.Unknown
    110.         End Select
    111.  
    112.     End Function
    113.  
    114. #End Region
    115.  
    116. #Region " Public Functions "
    117.  
    118.     Public Overloads Function Play(ByVal strFilePath As String) As Boolean
    119.  
    120.         If File.Exists(strFilePath) = False Then
    121.             Throw New FileNotFoundException("The file '" & m_strFilePath & "' does not exist!")
    122.             Return False
    123.         End If
    124.  
    125.         If Open(strFilePath) And Play() Then
    126.             Return True
    127.         Else
    128.             Return False
    129.         End If
    130.  
    131.     End Function
    132.  
    133.     Public Overloads Function Play() As Boolean
    134.  
    135.         Dim retVal As Integer = mciSendString("play " & strMCIAlias, CStr(0), 0, 0)
    136.  
    137.         If retVal = 0 Then
    138.             'Sucess
    139.             Return True
    140.         Else
    141.             ThrowMCIError(retVal)
    142.             Return False
    143.         End If
    144.  
    145.     End Function
    146.  
    147.     Public Overloads Function Open(ByVal strFilePath As String) As Boolean
    148.  
    149.         If File.Exists(strFilePath) = False Then
    150.             Throw New FileNotFoundException("The file '" & m_strFilePath & "' does not exist!")
    151.             Return False
    152.         End If
    153.  
    154.         Dim strMCIString As String
    155.  
    156.         strMCIString = "open " & GetShortPath(strFilePath) & " type MPEGVideo Alias " & strMCIAlias
    157.  
    158.         Dim retVal As Integer = mciSendString(strMCIString, CStr(0), 0, 0)
    159.  
    160.         If retVal = 0 Then
    161.             'Sucess
    162.             Return True
    163.         Else
    164.             ThrowMCIError(retVal)
    165.             Return False
    166.         End If
    167.  
    168.     End Function
    169.  
    170.     Public Overloads Function Open() As Boolean
    171.  
    172.         If File.Exists(m_strFilePath) = False Then
    173.             Throw New FileNotFoundException("The file '" & m_strFilePath & "' does not exist!")
    174.             Return False
    175.         End If
    176.  
    177.         Dim strMCIString As String
    178.  
    179.         strMCIString = "open " & GetShortPath(m_strFilePath) & " type MPEGVideo Alias " & strMCIAlias
    180.  
    181.         Dim retVal As Integer = mciSendString(strMCIString, CStr(0), 0, 0)
    182.  
    183.         If retVal = 0 Then
    184.             'Sucess
    185.             Return True
    186.         Else
    187.             ThrowMCIError(retVal)
    188.             Return False
    189.         End If
    190.  
    191.     End Function
    192.  
    193.     Public Function [Stop]() As Boolean
    194.         Dim retVal As Integer = mciSendString("stop " & strMCIAlias, CStr(0), 0, 0)
    195.  
    196.         If retVal = 0 Then
    197.             'Success
    198.             Return True
    199.         Else
    200.             ThrowMCIError(retVal)
    201.             Return False
    202.         End If
    203.     End Function
    204.  
    205.     Public Function Close() As Boolean
    206.         Dim retVal As Integer = mciSendString("close " & strMCIAlias, CStr(0), 0, 0)
    207.  
    208.         If retVal = 0 Then
    209.             'Sucess
    210.             Return True
    211.         Else
    212.             ThrowMCIError(retVal)
    213.             Return False
    214.         End If
    215.     End Function
    216.  
    217.     Public Function Pause(Optional ByVal blnResume As Boolean = False) As Boolean
    218.         Dim strMCISendString As String
    219.  
    220.         If blnResume = False Then
    221.             strMCISendString = "pause " & strMCIAlias
    222.         Else
    223.             strMCISendString = "play " & strMCIAlias
    224.         End If
    225.  
    226.         Dim retVal As Integer = mciSendString(strMCISendString, CStr(0), 0, 0)
    227.  
    228.         If retVal <> 0 Then
    229.             ThrowMCIError(retVal)
    230.             Return False
    231.         Else
    232.             Return True
    233.         End If
    234.     End Function
    235.  
    236. #End Region
    237.  
    238. #Region " Constructor and Destructors "
    239.  
    240.     Public Sub New()
    241.         strMCIAlias = MCI_DEFAULT_ALIAS
    242.     End Sub
    243.  
    244.     Public Sub New(ByVal MCIAlias As String)
    245.         If MCIAlias.Length <> 0 Then
    246.             strMCIAlias = MCIAlias
    247.         Else
    248.             strMCIAlias = MCI_DEFAULT_ALIAS
    249.         End If
    250.     End Sub
    251.  
    252. #End Region
    253. End Class
    254.  
    255. Public Class MCISendStringException
    256.     Inherits Exception
    257.  
    258.     Public Sub New(ByVal errorCode As Integer, ByVal strDescription As String)
    259.  
    260.         MyBase.New("MCI ERROR CODE: " & errorCode & " Description: " & strDescription)
    261.  
    262.     End Sub
    263.  
    264. End Class

    Attached zip contans:
    cMCI.vb
    PlayMp3.vb Sample usage of cMCI.vb
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  17. #17
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    thanks..

    thanks so much.
    it will take me some time to work through all that code.

    marvin

  18. #18
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    i implemented it all and everything works. that is some sick code. i learned a lot of unrelated stuff too, like overloading functions and stuff. i didn't know vb could do that. you can't overload operators, can you?

    stop and pause do the same thing though. i would think that stop would also reset the song, but it dosn't...

  19. #19
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    vb .net currently does not support operator overloading

    glad you like my class, I do this just for fun (pranking mostly) and this class that i just wrote will certainly come in handy
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width