how to play sound in vb
(when i press key the music plays

Recommended Answers

All 2 Replies

It was so much easier in the good old DOS Days... You had direct control of the hardware and didn't have to fight with the Hardware Abstraction Layer...

In <Project> <Components> you can add the "Windows Media Player" Control and this will give you control of MP3; WMA; and .WAV (Maybe a few others).

This is the code from a desktop Alarm Clock I put together

Private Sub Do_Alarm(aFile As String, aPrompt As String)
    
    If Not Muted Then
        wmp.settings.volume = 100
        wmp.URL = aFile
        wmp.Controls.play
    End If
    
    Call Set_Prompt
    If Not Hold_Off Then Prompt.Show

    Prompt.Prompt.Caption = " "
    Prompt.Prompt.Caption = aPrompt

End Sub

You can also use the Media Control Interface by Declaring it in a Module

Declare Function mciSendString Lib "winmm.dll" Alias _
        "mciSendStringA" (ByVal lpstrCommand As String, ByVal _
        lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
        hwndCallback As Long) As Long

However, the MCI interface will not work on some "Locked Down" Computers...

Once you start the sound the following code should stop it

Private Sub mmnuStop_Click()
    Clock.wmp.Close
End Sub

DoAlarm is called as follows

Do_Alarm Alarms.fgAlarms.TextMatrix(n, 6), Alarms.fgAlarms.TextMatrix(n, 7)

Where field 6 is the File Path to be Played and Field 7 is the User Prompt to be Displayed.

There is a delay in playing using the WMP control.

If the system is not locked down then the MCI control is a bit faster if memory serves me well. But, it is also much more complex to use...

I may have the old code lying around some where...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.