Results 1 to 16 of 16

Thread: How do I play sounds?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    10
    Originally (before I found out about API), the only way that I could play sound files was to use the Multimedia Control. My code basically consisted of about four to five lines of code, just to play a simple sound file.

    I'm very new to API, and I was just wondering if there was a simple way of playing sound files using an API call.

    Thanks to anyone who can help.

  2. #2
    Guest
    Use the PlaySound api function.

    Code:
    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 Command1_Click()
        PlaySound "C:\MyFile.wav", 0&, &H1
    End Sub

  3. #3
    Guest
    After you've 'stopped' your MIDI, you should use the following statement to close it.
    Code:
    mciSendString "close myfile", 0, 0, 0

  4. #4
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Question DX?

    And what about DirectX?
    or is it slower

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    DirectX (DirectSound) is more complex but not slower than API.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  6. #6
    Guest
    You should only use DirectSound if you are planning to use other DirectX function in your App. Otherwise, you'll have to include the Type Library all for a couple sounds.

  7. #7
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Arrow SndPlaySound API Function

    The easiset API for playing conventional sounds is:

    code:

    option explicit

    Private Declare Function sndPlaySound _
    Lib "winmm.dll" Alias "sndPlaySoundA" ( _
    ByVal lpszSoundName As String, _
    ByVal uFlags As Long _
    ) As Long

    Private Sub Command1_Click()
    sndPlaySound("C:\windows\media\start.wav", &H1)
    End Sub

    Hope it Works
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  8. #8
    New Member
    Join Date
    Nov 2000
    Posts
    13

    Stopping a sound

    How do I stop a looping sound file from playing without playing another sound file?

    I've looked up the VB API at http://www.vbapi.com/ref/p/playsound.html and the example:
    retval = PlaySound("", 0, SND_PURGE OR SND_NODEFAULT) doesn't work on my program.

    NOTE: I also even set the values of SND_PURGE and SND_NODEFAULT to what they are meant to be.

  9. #9
    Guest
    To stop a sound:

    Code:
    Private Sub Command2_Click()
    
         PlaySound "C:\WavFile.wav", 0&, &H4
    
    End Sub

  10. #10
    Lively Member
    Join Date
    Dec 2000
    Location
    India, Chennai
    Posts
    121

    Question Is it Possible to play MP3's using DirectX or Direct Sound?

    All the above API's will play .wav or .midi. Those API's will not play MP3. Is it Possible to play MP3 files using DirectX or DirectSound. How will be the Quality of the Sound. I'm having DirectX 7a. If it is possible to play, please send me the coding.

  11. #11
    Guest

    playing sounds

    Originally posted by Marble
    Originally (before I found out about API), the only way that I could play sound files was to use the Multimedia Control. My code basically consisted of about four to five lines of code, just to play a simple sound file.

    I'm very new to API, and I was just wondering if there was a simple way of playing sound files using an API call.

    Thanks to anyone who can help.
    I am trying create a CD menu which plays sounds, i can get the form to play the sound using a API call but the problem i have is that the sound plays without the form being displayed. the form only appears after the sound stops.

    i think i need to create the EXE as a multi threeded application but i dont know how.


  12. #12
    Lively Member
    Join Date
    Aug 2000
    Location
    Darlington, United Kingdom
    Posts
    121
    To play an mp3 file instead use this code

    Code:
    Public Function PlayMP3(Filename As String)
    Dim dwreturn As Long
    Dim ret As String * 128
    Dim tmp As String * 255
    Dim lenShort As Long
    Dim ShortPathAndFie As String
    'send a filename to the function
    If Dir(FileName) = "" Then
    msgbox "Error with input file"
    Exit Function
    End If
    lenShort = GetShortPathName(FileName, tmp, 255)
    ShortPathAndFie = Left$(tmp, lenShort)
    glo_hWnd = hwnd
    cmdToDo = "open " & ShortPathAndFie & " type MPEGVideo Alias mpeg"
    dwreturn = mciSendString(cmdToDo, 0&, 0&, 0&)
    
    If dwreturn <> 0 Then  'not success
    mciGetErrorString dwreturn, ret, 128
    MsgBox ret, vbCritical
    Exit Function
    End If
    'then to play just send this
    dwreturn = mciSendString("play mpeg", 0&, 0&, 0&)
    
    If dwreturn <> 0 Then
    mciGetErrorString dwreturn, ret, 128
    MsgBox ret, vbCritical
    Exit Function
    End If
    End Sub
    This will also play wavs...
    Do Not Underestimate The Power Of Simple VBA

    Maybe Its Just Not Possible, But Then Again What Is Impossible

  13. #13
    Guest

    Re: SndPlaySound API Function

    Originally posted by Wak
    The easiset API for playing conventional sounds is:

    code:

    option explicit

    Private Declare Function sndPlaySound _
    Lib "winmm.dll" Alias "sndPlaySoundA" ( _
    ByVal lpszSoundName As String, _
    ByVal uFlags As Long _
    ) As Long

    Private Sub Command1_Click()
    sndPlaySound("C:\windows\media\start.wav", &H1)
    End Sub

    Hope it Works
    sndPlaySound has been superseded by PlaySound

  14. #14

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    10
    Thanks for your help everyone! I didn't expect this many replies

  15. #15
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    MEGATRON....

    [quote]
    sndPlaySound has been superseded by PlaySound
    [/qoute]

    I realize what your saying and all, but the guy just wants to play a sound. What really confused me when I started
    out, is when you have a really simple question, and people
    swamp you with all this crap, which makes you even more confused. He said he's just new to API, so why give him
    an unecessarily hard API function when the easy one will do
    the trick. I mean, he only wants to play a sound. Probably for a mouse event or something. NO OFFENCES.
    Thanx
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  16. #16
    Guest
    How is PlaySound "an unecessarily hard API function"? They're practically the same, except PlaySound allows you to play through a module. If you don't intend to use this argument this simply omit it.

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