?
Solved

stop sound the number of seconds entered in a textbox

Posted on 2013-11-04
7
Medium Priority
?
251 Views
Last Modified: 2013-11-04
I want to play a sound and run a sub(StopSound) after the number of seconds entered into a text box(txtSoundLength)
How ?
0
Comment
Question by:isnoend2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39622856
Try this:

1. Create text box lost focus event handler (or (text) change, may be)
2. Put his code into handler
      My.Computer.Audio.Play("C:\mysounds.wav") where C:\mysounds.wav - path to your sound file
3. Start timer
4. After timer elapses N seconds where N is a number from text box - call:
      My.Computer.Audio.Stop()
0
 

Author Comment

by:isnoend2001
ID: 39622991
Didn't know you could play sounds like that
The sound is in a resource file i don't need the code to play the sound. just need code to play it for the length in the texbox:
This does not work:

Private Sub PlaySound()
sndData = LoadResData("SIREN", "SOUND")
sndPlaySound sndData(0), SND_LOOP Or SND_ASYNC Or SND_MEMORY'play the sound
mSoundLength = Val(txtSoundLength) 'how long to play sound
    Timer2.Interval = 60000 'one minute
    Timer2.Enabled = True
    If Now >= mSoundLength Then
    StopSound
    Timer2.Enabled = False
    End If
End Sub
 
it goes directly to StopSound. Why ?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39623142
Unfortunately I have no VB6 on me current computer.
What you need to do is start sound in one method, e.g., when you type a number or click a button and stop it to another method, in timer handler, when you have a tick after required period.

In VB.Net it woud look like:

    Private timlength As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        timlength = CInt(TextBox1.Text) '' convert text to a number
        PlaySound()
    End Sub


    Private Sub PlaySound()

        Me.Timer1.Interval = timlength * 1000
        My.Computer.Audio.Play("C:\temp\Police.wav", AudioPlayMode.Background)
        Me.Timer1.Start()

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' StopSound()
        My.Computer.Audio.Stop()
    End Sub

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:isnoend2001
ID: 39623168
i have code to start and stop the sound:

Private Sub PlaySound()
sndData = LoadResData("SIREN", "SOUND")
sndPlaySound sndData(0), SND_LOOP Or SND_ASYNC Or SND_MEMORY

Sub StopSound()
sndPlaySound ByVal 0, 0 'stop sound
End Sub
just want the timer to run the sub StopSound it after the seconds in a textbox
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 39623260
See, currently you have the call to StopSound inside the PlaySound method:
Private Sub PlaySound()
sndData = LoadResData("SIREN", "SOUND")
sndPlaySound sndData(0), SND_LOOP Or SND_ASYNC Or SND_MEMORY'play the sound
mSoundLength = Val(txtSoundLength) 'how long to play sound
    Timer2.Interval = 60000 'one minute
    Timer2.Enabled = True
    If Now >= mSoundLength Then
    StopSound
    Timer2.Enabled = False
    End If
End Sub

Open in new window


But you need to move this call to Timer1_Tick method. Otherwise you call it immediately.
0
 

Author Closing Comment

by:isnoend2001
ID: 39623287
Private Sub PlaySound()
    sndData = LoadResData("SIREN", "SOUND")
    sndPlaySound sndData(0), SND_LOOP Or SND_ASYNC Or SND_MEMORY
    Timer1.Interval = CLng(Text1.Text) * 1000
    Timer1.Enabled = True
End Sub

Sub StopSound()
    sndPlaySound ByVal 0, 0 'stop sound
    Timer1.Enabled = True
End Sub
I got it
Private Sub Timer1_Timer()
    StopSound
    Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
    PlaySound
End Sub
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39623302
That's it! :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question