can anyone give a sample code for applying sounds on vb..
thanks.. i already applied a code but it wont work..

Public MTime As Byte

MMControl1.FileName = App.Path & "\music\Music.mid"

Private Sub Timer4_Timer()
MTime = MTime + 1
If MTime = 113 Then
ShutYerNoise
Play
MTime = 0
End If
End Sub

Sub Play()
MMControl1.Command = "Open"
MMControl1.Command = "Play"
Timer4.Enabled = True
End Sub


Sub ShutYerNoise()
MMControl1.Command = "Stop"
MMControl1.Command = "Close"
Timer4.Enabled = False
End Sub

what's wrong with this?..anyone?

Dani AI

Generated

This thread shows a classic VB6 audio/Timer interaction. and already narrowed it down, so here are focused checks and two reliable VB6 alternatives that avoid control-specific surprises.

  • Confirm the file actually exists before trying to play it (use Dir or FileSystemObject).
  • Make sure the Timer is enabled and its Interval is appropriate (very small values can be unreliable).
  • Verify the sound format and system support: WAV is simplest; MIDI relies on the system MIDI mapper/device; MP3 needs the appropriate MCI support/codecs.
  • If using a manual counter, use an Integer/Long so the value does not overflow.
  • Capture return codes (or errors) from API/MCI calls so you can see why a play failed.

A simple, robust option for WAV files is the PlaySound API:

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
    (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Private Const SND_FILENAME = &H20000
Private Const SND_ASYNC = &H1

Sub PlayWavFile(path As String)
    If Dir(path) = "" Then Exit Sub
    PlaySound path, 0, SND_FILENAME Or SND_ASYNC
End Sub

For more flexible control (MIDI, MP3, advanced commands) use MCI via mciSendString:

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

Sub PlayMci(path As String)
    If Dir(path) = "" Then Exit Sub
    mciSendString "open """ & path & """ alias track1", vbNullString, 0, 0
    mciSendString "play track1", vbNullString, 0, 0
End Sub

Official references: and . For new projects consider managed APIs (SoundPlayer, NAudio) rather than VB6 MM controls.

Recommended Answers

All 3 Replies

First of all, you declared MTime as Byte when you're using it as an integer. Secondly, why not just use the Timer event instead of MTime?

Private Sub Form_Load()
MMControl1.Command = "Open"
MMControl1.Command = "Play"
End Sub

Private Sub Timer1_Timer()
MMControl1.Command = "Stop"
MMControl1.Command = "Close"
MMControl1.Command = "Open"
MMControl1.Command = "Play"
End Sub

Ok..thanks..i'm already done with it...=)

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.