[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Can't get app to play wav file

I can't get my app to play a wav file.
Here is my code.


Dim dtStartTime, dtFinishTime, dtLastBeep, dtPauseTime As DateTime
    Dim tsTimeLeft As TimeSpan
    Dim iSecondsLeft, iMinutesLeft, iMilliLeft As Integer
    Dim newText As String
    Dim WithEvents tmrCountdown As New Timers.Timer(25) 'the 25 is 25 milliseconds which is the update interval

    Dim iBeepGap As Integer = 30 ' change this to make the beeps more/less frequent
    Public Const SND_SYNC = &H0                     'play synchronously (default)
    Public Const SND_NODEFAULT = &H2                'silence not default, if sound not found

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


    Public Sub OpenWave(ByVal sVoicefile As String)
        Dim x As Long
        Dim sPath As String = "C:\Temp"    '<-------- Directory of wave files
        x = sndPlaySound(sPath & sVoicefile, SND_SYNC Or SND_NODEFAULT)
    End Sub

    Sub StartTimer_Minutes(ByVal OffsetMinutes As Integer)
        StartTimer_Seconds(OffsetMinutes * 60)
    End Sub

    Sub StartTimer_Seconds(ByVal OffsetSeconds As Integer)
        tmrCountdown.Stop()

        dtStartTime = Now
        dtFinishTime = dtStartTime.AddSeconds(OffsetSeconds)
        dtLastBeep = dtStartTime

        Label1.Text = Fix(OffsetSeconds / 60).ToString("00") & ":" & (OffsetSeconds Mod 60).ToString("00") & ":00"
        tmrCountdown.Start()
    End Sub

    Sub TimerEvent(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs) Handles tmrCountdown.Elapsed
        tsTimeLeft = dtFinishTime.Subtract(Now)
        If tsTimeLeft.Ticks > 0 Then
            iMinutesLeft = tsTimeLeft.Minutes + Fix(tsTimeLeft.TotalHours) * 60
            iSecondsLeft = tsTimeLeft.Seconds
            iMilliLeft = Fix(tsTimeLeft.Milliseconds / 10)

            newText = iSecondsLeft.ToString("00") & ":" & iMilliLeft.ToString("00")

            If iMinutesLeft > 0 Then
                newText = iMinutesLeft.ToString("00") & ":" & newText
            End If
            Label1.Text = newText

            If Now.Subtract(dtLastBeep).TotalSeconds >= iBeepGap Then
                dtLastBeep = Now
                'Beep() : Beep() : Beep()
                OpenWave("warning.wav")
            End If
        Else
            Label1.Text = "00:00:00"
            Beep() : Beep() : Beep()
            tmrCountdown.Stop()
        End If
    End Sub

    Sub PauseCountdown()
        If tmrCountdown.Enabled = False Then Exit Sub
        tmrCountdown.Stop()
        dtPauseTime = Now
    End Sub

    Sub ResumeCountdown()
        If tmrCountdown.Enabled = True Then Exit Sub
        If dtFinishTime = Nothing Then Exit Sub
        dtLastBeep = dtLastBeep.Add(Now.Subtract(dtPauseTime))
        dtFinishTime = dtFinishTime.Add(Now.Subtract(dtPauseTime))
        tmrCountdown.Start()
    End Sub

    Sub ToggleCountdown()
        If tmrCountdown.Enabled Then
            PauseCountdown()
        Else
            ResumeCountdown()
        End If
    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        Dim iCountDownFrom As Integer
        If IsNumeric(txtSeconds.Text) And IsNumeric(txtMinutes.Text) Then
            iCountDownFrom = CInt(txtMinutes.Text) * 60
            iCountDownFrom += CInt(txtSeconds.Text)

            StartTimer_Seconds(iCountDownFrom)
        End If
    End Sub

    Private Sub cmdToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdToggle.Click
        ToggleCountdown()
    End Sub

    Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
        Dim iCountDownFrom As Integer
        If IsNumeric(0) And IsNumeric(1) Then
            iCountDownFrom = CInt(1) * 60
            iCountDownFrom += CInt(0)

            StartTimer_Seconds(iCountDownFrom)
        End If
    End Sub

    Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
        Dim iCountDownFrom As Integer
        If IsNumeric(0) And IsNumeric(2) Then
            iCountDownFrom = CInt(2) * 60
            iCountDownFrom += CInt(0)

            StartTimer_Seconds(iCountDownFrom)
        End If
    End Sub
0
jettman26
Asked:
jettman26
  • 5
  • 4
  • 4
2 Solutions
 
Bob LearnedCommented:
Try this instead:

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

Bob
0
 
jettman26Author Commented:
I tried that and still no sound.  When I uncomment out the Beeps then I hear the beeps but not the warning.wav file.
0
 
Bob LearnedCommented:
Then, try this:

Const SND_ASYNC As Integer = 1
Const SND_FILENAME As Integer = 131072

x = sndPlaySound(sPath & sVoicefile, SND_ASYNC Or SND_FILENAME)

Bob

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jettman26Author Commented:
Still won't play warning.wav
I have warning.wav in C:\Temp.
I don't understand why it isn't working.
0
 
S-TwilleyCommented:
I posted it on the previous thread... but just to keep your code tidy:

replace ->        Beep() : Beep() : Beep()
with     ->        AlertMe()  'different name from last time, doesn't matter


then add a sub/method (and also the few lines leading to it ... this is learned one's code, don't give me credit)

Const SND_ASYNC As Integer = 1
Const SND_FILENAME As Integer = 131072

Private iSoundResp as Integer

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

Private Sub AlertMe()
       Beep() : Beep() : Beep()   ' keep these in here in case you don't get the code to work
     
       'Now using Learned One's code  (don't give me credit)

        iSoundResp = sndPlaySound("C:\Temp\warning.wav", SND_ASYNC Or SND_FILENAME)
        Msgbox(iSoundResp.ToString)
End Sub

------------

Let us know the value displayed in the MsgBox... this should tell us if the API was a success or not, and narrow down the problem
0
 
S-TwilleyCommented:
Again, it's not my code...  as much I'd love being given points, i know how annoyin it can be when someone else gets given ur points :P
0
 
Bob LearnedCommented:
Actually, I don't really care, it's just fun to complain sometimes :))  *GRIN*

Bob
0
 
jettman26Author Commented:
Thanks
It played this time.  
There is a 1 in the messagebox.
Is there a way to make it look in the folder where the program resides for the wav file as in the following line?
iSoundResp = sndPlaySound("C:\Temp\warning.wav", SND_ASYNC Or SND_FILENAME)
I will be using different wav files throughout the program.
0
 
S-TwilleyCommented:
application.StartupPath  is the path of the executable
0
 
Bob LearnedCommented:
What value does sPath & sVoicefile produce in your old method?

The assumption is that it was producing a valid filename.  There won't be any error from PlaySound, it just won't play.

Bob
0
 
S-TwilleyCommented:
Well I don't tend to complain... if i were to complain, it would probably be at the people not paying attention to where they divy up the points... not that im a bitter person or anything :P
0
 
S-TwilleyCommented:
how about changing the AlertMe method

Private sDefault As String = "c:\WINDOWS\Media\ding.wav"

Sub AlertMe(ByVal sFileName As String)
    sFileName = sFileName.ToLower
    sFileName = sFileName.Replace("myapppath-", Application.StartupPath)

    If IO.File.Exists(sFileName) Then
        iSoundResp = sndPlaySound(sFileName, SND_ASYNC Or SND_FILENAME)
    Else
        If IO.File.Exists(sDefault) Then
            iSoundResp = sndPlaySound(sFileName, SND_ASYNC Or SND_FILENAME)
        Else
            Beep() : Beep() : Beep()  
         End If
    End If  
End Sub

===========

Then in your timer Event

AlertMe("myapppath-warning.wav") ' once inside method, code will replace myapppath with path of exe'

or

AlertMe("C:\temp\warning.wav")    ' or use a fully qualified path
0
 
jettman26Author Commented:
Thanks guys.
S-Twilley, this worked, thanks.

TheLearnedOne, maybe I should have used the entire path i.e. C:\Temp\warning.wav instead of C:\Temp.  Thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now