Can anyone help me? I wish to play a simple .WAV and I would like the smallest code way.
Thanks.
Printable View
Can anyone help me? I wish to play a simple .WAV and I would like the smallest code way.
Thanks.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Public Const SND_ASYNC = &H1 ' play asynchronously
Public Const SND_LOOP = &H8 ' loop the sound until next sndPlaySound
Public Const SND_NOSTOP = &H10 ' don't stop any currently playing sound
Public Const SND_NOWAIT = &H2000 ' don't wait if the driver is busy
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rc As Long
'file in bin irectory
rc = PlaySound(System.AppDomain.CurrentDomain.BaseDirectory & "tada.wav", 0, SND_NOSTOP)
If rc = 0 Then
'Sound didn't play, so just beep...
Beep()
End If
End Sub
End Class
Iouri, you made some mistakes in converting the API declaration to VB.NET. All Long's must be integers, so the declaration must be:
VB Code:
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
Works fine for me. Nice.
I can't seem to get it to work :( rc returns a value of 1 but all I get is a beep! I have this code:
I'm sure i'm missing something silly :confused:VB Code:
Function SaveConfig() FileOpen(1, Application.StartupPath & "\config.ini", OpenMode.Output) PrintLine(1, TreeView1.BackColor.Name) PrintLine(1, TreeView1.ForeColor.Name) PrintLine(1, rtbcodewindow.BackColor.Name) FileClose(1) ' Dim rc As Long 'file in bin irectory rc = PlaySound(System.AppDomain.CurrentDomain.BaseDirectory & "tada.wav", 0, SND_NOSTOP) MsgBox(rc) If rc = 0 Then 'Sound didn't play, so just beep... Beep() End If End Function
Note: I do have the rest of the code declaired also.
Quote:
Originally posted by Frans C
Iouri, you made some mistakes in converting the API declaration to VB.NET. All Long's must be integers, so the declaration must be:
VB Code:
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
You are right for .NET it must be Integer. But it also works with Long. Tried it just now
Then why won't it work for me! :mad: :( Where does everything go? I think I have it in the right place but just in case...
Ca anyone help?
Try to play it in different thread
'Form
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PlayS("c:\windows\media\tada.wav")
End Sub
Private Sub PlayS(ByVal sound As String)
Dim thread As New SoundThread()
Dim mThread As New System.Threading.ThreadStart(AddressOf thread.newThread)
Dim oThread As New System.Threading.Thread(mThread)
thread.sound = sound
oThread.Start()
End Sub
End Class
Class SoundThread
Public sound As String
Sub newThread()
PlaySound(sound, 0, 0)
End Sub
End Class
'Module
Module Module1
Public Declare Function PlaySound Lib "winmm.dll" (ByVal filename As String,ByVal hmodule As Long, ByVal dword As Integer) As Boolean
End Module
Thanks Iouri, that one worked and i've also included it in the next release of the VBCodeBook.NET CodePack update :D
In his original post, you might have overlooked this one small comment he made in the code: 'file in bin irectory ;)
So you're saying that the sound file should have been in the same directory as the exe?
for this particular code yes. If you want to have it in different directory you must give a path of the .wav fileQuote:
Originally posted by RealNickyDude
So you're saying that the sound file should have been in the same directory as the exe?
Riiggghhht... That's why it wasn't working! I assumed that this
would automatically find and play the .wav as it is in the Windows path!VB Code:
rc = PlaySound(System.AppDomain.CurrentDomain.BaseDirectory & "tada.wav", 0, SND_NOSTOP)
I think:
VB Code:
Private Declare Function mciSendString Lib "winmm.dll" (ByVal Command As String, ByVal Return As StringBuilder, ByVal Size As Integer, ByVal Callback As Object) As UInt 'then mciSendString("open c:\tada.wav as type waveaudio alias mysound", Nothing, 0, Nothing) mciSendString("play mysound")
My VB.NET clearly isn't fantastic BUT my C# is fluent;)