|
-
Feb 5th, 2003, 08:26 AM
#1
Thread Starter
Hyperactive Member
Quickest, smallest way to play a .WAV *[RESOLVED]*
Can anyone help me? I wish to play a simple .WAV and I would like the smallest code way.
Thanks.
Last edited by RealNickyDude; Feb 6th, 2003 at 11:17 AM.
-
Feb 5th, 2003, 09:50 AM
#2
Member
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
-
Feb 5th, 2003, 10:49 AM
#3
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
-
Feb 5th, 2003, 12:05 PM
#4
-
Feb 5th, 2003, 12:19 PM
#5
Thread Starter
Hyperactive Member
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:
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
I'm sure i'm missing something silly 
Note: I do have the rest of the code declaired also.
Last edited by RealNickyDude; Feb 5th, 2003 at 12:23 PM.
-
Feb 5th, 2003, 03:12 PM
#6
Member
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
-
Feb 5th, 2003, 03:15 PM
#7
Thread Starter
Hyperactive Member
Then why won't it work for me! Where does everything go? I think I have it in the right place but just in case...
-
Feb 6th, 2003, 07:35 AM
#8
Thread Starter
Hyperactive Member
-
Feb 6th, 2003, 08:58 AM
#9
Member
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
-
Feb 6th, 2003, 11:17 AM
#10
Thread Starter
Hyperactive Member
Thanks Iouri, that one worked and i've also included it in the next release of the VBCodeBook.NET CodePack update
-
Feb 6th, 2003, 01:56 PM
#11
Frenzied Member
In his original post, you might have overlooked this one small comment he made in the code: 'file in bin irectory
~Peter

-
Feb 6th, 2003, 01:59 PM
#12
Thread Starter
Hyperactive Member
So you're saying that the sound file should have been in the same directory as the exe?
-
Feb 6th, 2003, 02:47 PM
#13
Member
Originally posted by RealNickyDude
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 file
-
Feb 6th, 2003, 02:52 PM
#14
Thread Starter
Hyperactive Member
Riiggghhht... That's why it wasn't working! I assumed that this
VB Code:
rc = PlaySound(System.AppDomain.CurrentDomain.BaseDirectory & "tada.wav", 0, SND_NOSTOP)
would automatically find and play the .wav as it is in the Windows path!
-
Feb 7th, 2003, 12:39 PM
#15
Addicted Member
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
Not at all related to sheep...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|