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.
Printable View
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.
Opps , and this demo I've just created for MP3 Files .
can you post the code as text please?
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:
Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Play Button Dim OFD As New OpenFileDialog OFD.Filter = "MP3 Files (*.MP3)|*.MP3" If OFD.ShowDialog = DialogResult.OK Then mciExecute("OPEN " & OFD.FileName & " TYPE MPEGVideo ALIAS mp3song ") mciExecute("PLAY mp3song") End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'Stop Button mciExecute("CLOSE mp3song") End Sub
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?
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 ?
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?
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 .
Why don't you try BASS?
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 :o ]
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 .
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.
VB6 Code
Module File
VB Code:
'************************************* 'Copyright © 2001 by Alexander Anikin 'e-mail: [email][email protected][/email] 'http://www.hotmix.kiev.ua '************************************* Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Public Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long Public FileOP As String Public Const OFN_FILEMUSTEXIST = &H1000 Public Const OFN_HIDEREADONLY = &H4 Public OFN As OPENFILENAME Public Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type
In a form
VB Code:
Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long 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 Private mmOpen As String, sec As Integer, mins As Integer Dim nFileName As String Public Function MP3Play(wndHandle As Long, sFileName As String) Dim cmdToDo As String * 255 Dim dwReturn As Long Dim ret As String * 128 Dim tmp As String * 255 Dim lenShort As Long Dim ShortPathAndFie As String, glo_HWND As Long If Dir(sFileName) = "" Then mmOpen = "Error with input file" Exit Function End If lenShort = GetShortPathName(sFileName, tmp, 255) ShortPathAndFie = Left$(tmp, lenShort) glo_HWND = wndHandle cmdToDo = "open " & ShortPathAndFie & " type MPEGVideo Alias MP3Play" dwReturn = mciSendString(cmdToDo, 0&, 0&, 0&) If dwReturn <> 0 Then 'not success mciGetErrorString dwReturn, ret, 128 mmOpen = ret MsgBox ret, vbCritical Exit Function End If mmOpen = "Success" mciSendString "play MP3Play", 0, 0, 0 End Function Public Function MP3Pause() mciSendString "pause MP3Play", 0, 0, 0 End Function Public Function MP3UnPause() mciSendString "play MP3Play", 0, 0, 0 End Function Public Function MP3Stop() As String mciSendString "stop MP3Play", 0, 0, 0 mciSendString "close MP3Play", 0, 0, 0 End Function Private Sub Check1_Click() Timer1 = Not Timer1 If Timer1 Then Check1.Value = 1 Else Check1.Value = 0 End Sub Private Sub Command1_Click() Command1.Enabled = False Command5.Enabled = False Open_file End Sub Private Sub Command2_Click() If Command2.Caption = "Pause" Then Command2.Caption = "Play" MP3Pause Else Command2.Caption = "Pause" MP3UnPause End If End Sub Private Sub Command3_Click() Command2.Enabled = False Command3.Enabled = False Command4.Enabled = False Command1.Enabled = True Command2.Caption = "Pause" MP3Stop End Sub Private Sub Command4_Click() mciSendString "stop MP3Play", 0, 0, 0 mciSendString "play MP3Play from 0", 0, 0, 0 Command2.Caption = "Pause" End Sub Private Sub Command5_Click() Unload Me End Sub Private Sub Form_Unload(Cancel As Integer) MP3Stop End Sub Private Sub Open_file() Dim cderr As Long OFN.lStructSize = 76& OFN.hwndOwner = Form1.hWnd OFN.lpstrFilter = "mp3 (*.mp3)" + Chr(0) + "*.mp3" + Chr(0) + Chr(0) OFN.lpstrCustomFilter = String(256, Chr(0)) OFN.nMaxCustFilter = 256 OFN.lpstrFile = "" + String(512, Chr(0)) OFN.nMaxFile = 512 OFN.lpstrFileTitle = String(256, Chr(0)) OFN.nMaxFileTitle = 256 OFN.flags = OFN_FILEMUSTEXIST + OFN_HIDEREADONLY '************ DoEvents '************ If GetOpenFileName(OFN) Then OFN.lpstrFile = Mid(OFN.lpstrFile, 1, InStr(OFN.lpstrFile, Chr(0)) - 1) nFileName = OFN.lpstrFile OFN.lpstrFileTitle = Mid(OFN.lpstrFileTitle, 1, InStr(OFN.lpstrFileTitle, Chr(0)) - 1) InitialDir = Left(OFN.lpstrFile, Len(OFN.lpstrFile) - Len(OFN.lpstrFileTitle)) Else cderr = CommDlgExtendedError GoTo ex End If MP3Play hWnd, nFileName Command2.Enabled = True Command3.Enabled = True Command4.Enabled = True Command5.Enabled = True Exit Sub ex: Command1.Enabled = True Command5.Enabled = True End Sub Public Function IsPlaying() As Boolean Static s As String * 30 mciSendString "status MP3Play mode", s, Len(s), 0 IsPlaying = (Mid$(s, 1, 7) = "playing") End Function Private Sub Timer1_Timer() If IsPlaying = False And Command1.Enabled = False And Command2.Caption = "Pause" Then Command4_Click End Sub
I was bored; bored enough to make a binary clock :D but I also made a class for playing mp3 files in vb .net
VB Code:
Option Strict On Option Explicit On Imports System.IO Public Class cMCI Private Const MCI_DEFAULT_ALIAS As String = "MP3Play" Public Enum MCI_STATUS_MODE Not_Ready = 1 Paused = 2 Playing = 4 Stopped = 8 Open = 16 Parked = 32 Recording = 64 Seeking = 128 Unknown = 0 End Enum Private strMCIAlias As String #Region " API DECLARE STATEMENTS " Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Integer, ByVal lpstrBuffer As String, ByVal uLength As Integer) As Integer Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Integer) As Integer 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 #End Region #Region " Private Class Member Variables " Private m_strFilePath As String #End Region #Region " Public Class Properties " Public ReadOnly Property MCI_STATUS() As MCI_STATUS_MODE Get Return GetStatus() End Get End Property Public Property FilePath() As String Get Return m_strFilePath End Get Set(ByVal Value As String) m_strFilePath = Value End Set End Property #End Region #Region " Private Functions " Private Function ThrowMCIError(ByVal errCode As Integer) As Boolean Dim strBuffer As String = Space(255) mciGetErrorString(errCode, strBuffer, 255) strBuffer = strBuffer.Trim(Chr(0)).TrimEnd(Chr(34)) Throw New MCISendStringException(errCode, strBuffer) Return True End Function Private Function GetShortPath(ByVal strFullPath As String) As String Dim strBuffer As String = Space(255) Dim retVal As Integer = GetShortPathName(strFullPath, strBuffer, 255) Dim I As Integer Return strBuffer.Trim(CChar(" ")).Trim(Chr(0)) End Function Private Function GetStatus() As MCI_STATUS_MODE Dim strBuffer As String = Space(255) mciSendString("status " & strMCIAlias & " mode", strBuffer, strBuffer.Length, 0) Dim strMode As String = strBuffer.TrimEnd(Chr(0)) Select Case strMode Case Is = "not ready" Return MCI_STATUS_MODE.Not_Ready Case Is = "paused" Return MCI_STATUS_MODE.Paused Case Is = "playing" Return MCI_STATUS_MODE.Playing Case Is = "stopped" Return MCI_STATUS_MODE.Stopped Case Is = "open" Return MCI_STATUS_MODE.Open Case Is = "parked" Return MCI_STATUS_MODE.Parked Case Is = "recording" Return MCI_STATUS_MODE.Recording Case Is = "seeking" Return MCI_STATUS_MODE.Seeking Case Else Return MCI_STATUS_MODE.Unknown End Select End Function #End Region #Region " Public Functions " Public Overloads Function Play(ByVal strFilePath As String) As Boolean If File.Exists(strFilePath) = False Then Throw New FileNotFoundException("The file '" & m_strFilePath & "' does not exist!") Return False End If If Open(strFilePath) And Play() Then Return True Else Return False End If End Function Public Overloads Function Play() As Boolean Dim retVal As Integer = mciSendString("play " & strMCIAlias, CStr(0), 0, 0) If retVal = 0 Then 'Sucess Return True Else ThrowMCIError(retVal) Return False End If End Function Public Overloads Function Open(ByVal strFilePath As String) As Boolean If File.Exists(strFilePath) = False Then Throw New FileNotFoundException("The file '" & m_strFilePath & "' does not exist!") Return False End If Dim strMCIString As String strMCIString = "open " & GetShortPath(strFilePath) & " type MPEGVideo Alias " & strMCIAlias Dim retVal As Integer = mciSendString(strMCIString, CStr(0), 0, 0) If retVal = 0 Then 'Sucess Return True Else ThrowMCIError(retVal) Return False End If End Function Public Overloads Function Open() As Boolean If File.Exists(m_strFilePath) = False Then Throw New FileNotFoundException("The file '" & m_strFilePath & "' does not exist!") Return False End If Dim strMCIString As String strMCIString = "open " & GetShortPath(m_strFilePath) & " type MPEGVideo Alias " & strMCIAlias Dim retVal As Integer = mciSendString(strMCIString, CStr(0), 0, 0) If retVal = 0 Then 'Sucess Return True Else ThrowMCIError(retVal) Return False End If End Function Public Function [Stop]() As Boolean Dim retVal As Integer = mciSendString("stop " & strMCIAlias, CStr(0), 0, 0) If retVal = 0 Then 'Success Return True Else ThrowMCIError(retVal) Return False End If End Function Public Function Close() As Boolean Dim retVal As Integer = mciSendString("close " & strMCIAlias, CStr(0), 0, 0) If retVal = 0 Then 'Sucess Return True Else ThrowMCIError(retVal) Return False End If End Function Public Function Pause(Optional ByVal blnResume As Boolean = False) As Boolean Dim strMCISendString As String If blnResume = False Then strMCISendString = "pause " & strMCIAlias Else strMCISendString = "play " & strMCIAlias End If Dim retVal As Integer = mciSendString(strMCISendString, CStr(0), 0, 0) If retVal <> 0 Then ThrowMCIError(retVal) Return False Else Return True End If End Function #End Region #Region " Constructor and Destructors " Public Sub New() strMCIAlias = MCI_DEFAULT_ALIAS End Sub Public Sub New(ByVal MCIAlias As String) If MCIAlias.Length <> 0 Then strMCIAlias = MCIAlias Else strMCIAlias = MCI_DEFAULT_ALIAS End If End Sub #End Region End Class Public Class MCISendStringException Inherits Exception Public Sub New(ByVal errorCode As Integer, ByVal strDescription As String) MyBase.New("MCI ERROR CODE: " & errorCode & " Description: " & strDescription) End Sub End Class
Attached zip contans:
cMCI.vb
PlayMp3.vb Sample usage of cMCI.vb
thanks so much.
it will take me some time to work through all that code.
marvin
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...
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 :D