Results 1 to 15 of 15

Thread: Quickest, smallest way to play a .WAV *[RESOLVED]*

  1. #1

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435

    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.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  2. #2
    Member
    Join Date
    Dec 2002
    Location
    NY, USA
    Posts
    52
    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 Boutchkine

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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:
    1. Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer

  4. #4
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Works fine for me. Nice.
    ~Peter


  5. #5

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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:
    1. Function SaveConfig()
    2.         FileOpen(1, Application.StartupPath & "\config.ini", OpenMode.Output)
    3.         PrintLine(1, TreeView1.BackColor.Name)
    4.         PrintLine(1, TreeView1.ForeColor.Name)
    5.         PrintLine(1, rtbcodewindow.BackColor.Name)
    6.         FileClose(1)
    7.         '
    8.         Dim rc As Long
    9.         'file in bin irectory
    10.         rc = PlaySound(System.AppDomain.CurrentDomain.BaseDirectory & "tada.wav", 0, SND_NOSTOP)
    11.         MsgBox(rc)
    12.         If rc = 0 Then
    13.             'Sound didn't play, so just beep...
    14.             Beep()
    15.         End If
    16.     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.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  6. #6
    Member
    Join Date
    Dec 2002
    Location
    NY, USA
    Posts
    52
    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:
    1. 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
    Iouri Boutchkine

  7. #7

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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...
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  8. #8

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Ca anyone help?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  9. #9
    Member
    Join Date
    Dec 2002
    Location
    NY, USA
    Posts
    52
    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
    Iouri Boutchkine

  10. #10

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Thanks Iouri, that one worked and i've also included it in the next release of the VBCodeBook.NET CodePack update
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  11. #11
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277
    In his original post, you might have overlooked this one small comment he made in the code: 'file in bin irectory
    ~Peter


  12. #12

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    So you're saying that the sound file should have been in the same directory as the exe?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  13. #13
    Member
    Join Date
    Dec 2002
    Location
    NY, USA
    Posts
    52
    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
    Iouri Boutchkine

  14. #14

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Riiggghhht... That's why it wasn't working! I assumed that this

    VB Code:
    1. 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!
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  15. #15
    Addicted Member
    Join Date
    Aug 2002
    Location
    London UK
    Posts
    255
    I think:

    VB Code:
    1. 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
    2.  
    3. 'then
    4.  
    5. mciSendString("open c:\tada.wav as type waveaudio alias mysound", Nothing, 0, Nothing)
    6. 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
  •  



Click Here to Expand Forum to Full Width