Learn how to a build a cloud-first strategyRegister Now

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

VB 2005: How to loop a sound a limited number of times?

Dear Experts:

I would like to learn how to loop a sound a limited number of times, say two loops.

Thanks!
0
duta
Asked:
duta
  • 10
  • 10
  • 10
2 Solutions
 
Wayne Taylor (webtubbs)Commented:
Related question : http:/Q_23908555.html

You simply need to set the 2nd optional parameter of the Play method to 'WaitToComplete'....

        Dim filePath As String = "C:\Path\File.wav"
        For i As Integer = 1 To 2
            My.Computer.Audio.Play(filePath, AudioPlayMode.WaitToComplete)
        Next

Wayne
0
 
dutaAuthor Commented:
It doesn't work.
The sounds plays a few seconds and then stops.
In addition, the application does not show up until the sounds stops.
Thanks!
0
 
Wayne Taylor (webtubbs)Commented:
>>The sounds plays a few seconds and then stops.
Umm, isn't that how it's supposed to be?? If your sound is only 1 second long, 1sec x 2 = 2 seconds.

>>It doesn't work.
I tested before I posted so I can verify that it definitely works. Where are you running the code?

What exactly are you trying to achieve? Some sort of sound when your application is starting up?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
carlsiyCommented:
try placing the code other than the form load event.
0
 
dutaAuthor Commented:
I am so sorry for not responding sooner.
If I have to place the code outside Form_load event, where may
 I place it?
I would like the sound played when the form is loaded.

I tried the code in button event. Still there was no response.

Thanks a lot!
0
 
Wayne Taylor (webtubbs)Commented:
The code below plays a sound when the form starts up, and ends the sound when the Timer interval elapses.

If it doesn't work for you, there's a problem with your sound file. Or your volume is turned down too low.

Wayne
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SoundTimer = New Timer()
        SoundTimer.Interval = 3000 '3 seconds
        SoundTimer.Start()
 
        Dim filePath As String = "C:\Path\File.wav"
        My.Computer.Audio.Play(filePath, AudioPlayMode.BackgroundLoop)
    End Sub
 
    Private Sub SoundTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles SoundTimer.Tick
        My.Computer.Audio.Stop()
        SoundTimer.Dispose()
    End Sub

Open in new window

0
 
dutaAuthor Commented:
There is a blue antsy line under  "soundTimer" in soundTimer = New Timer()

When I put the cursor over it, I read " Name 'SoundTimer' is not declared".

What I would like to do is to make a sound played three loops, not  3 seconds.

Thanks a lot!
0
 
carlsiyCommented:
shouldn't it be
dim SoundTimer = New Timer()

0
 
dutaAuthor Commented:
I tried:
dim SoundTimer = New Timer()

It does not work.

Thanks though!
0
 
carlsiyCommented:
The approach of webtubbs is totally wrong... he assumes sound timer object here (when created) inherits all the properties automatically which is wrong.
try this
Public Class Form1
   Friend WithEvents SoundTimer As System.Windows.Forms.Timer
       Private Sub SoundTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles SoundTimer.Tick
      My.Computer.Audio.Stop()
      SoundTimer.Dispose()
   End Sub
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Me.SoundTimer = New System.Windows.Forms.Timer(Me.components)
      SoundTimer = New Timer
      SoundTimer.Interval = 3000 '3 seconds
      SoundTimer.Start()
      Dim filePath As String = "C:\File.wav"
      My.Computer.Audio.Play(filePath, AudioPlayMode.BackgroundLoop)
   End Sub
End Class
his logic would stop after the number of seconds interval per say 3 seconds... sou you should have estimated in advance the length of the whole wav. then multiply it by the number of times you want to replay the sound .
0
 
Wayne Taylor (webtubbs)Commented:
>>The approach of webtubbs is totally wrong... he assumes sound timer object here (when created) inherits all the properties automatically which is wrong.

Excuse me??? I simply failed to paste the declaration line for the timer. Be very careful before making incorrect assumptions. You post above is very disrespectful.

Awaiting your apology.

Wayne
0
 
carlsiyCommented:
sorry about that hehehehe... was in the moment.
but you should assumed this also,
If it doesn't work for you, there's a problem with your sound file. Or your volume is turned down too low.
0
 
carlsiyCommented:
correction...
but you shouldn't have assumed this also
If it doesn't work for you, there's a problem with your sound file. Or your volume is turned down too low.
0
 
Wayne Taylor (webtubbs)Commented:
I wasn't making an assumption - I was making a statement. The only way a sound wouldn't be heard when using my code is if there was a problem with the sound file or the volume was too low.
0
 
carlsiyCommented:
so... are you accepting my apology?
btw duta... whats the verdict?
0
 
Wayne Taylor (webtubbs)Commented:
>>so... are you accepting my apology?
Yeah, apology accepted.

duta,

In order to get 3 iterations of your sound, you will probably need to start the sound in a seperate thread. In your Form.Load() event, use this code....

Dim t As New Threading.Thread(AddressOf PlaySound)
t.Start()

...then use this procedure....

Private Sub PlaySound()
Dim filePath As String = "C:\Path\File.wav"
For i As Integer = 1 To 3
My.Computer.Audio.Play(filePath, AudioPlayMode.WaitToComplete)
Next
End Sub

Wayne
0
 
carlsiyCommented:

System.Threading.Thread
0
 
carlsiyCommented:
nice solution.
0
 
dutaAuthor Commented:
Thank you so much for your kind, patience tips.

By the way, I need to put the sound in the Resource folder.
How may I change the path?
0
 
dutaAuthor Commented:
The sound (about 1-minute-long) does play only a few seconds before it stops and plays from the beginning. It goes on . . .

Thanks!
0
 
Wayne Taylor (webtubbs)Commented:
>>By the way, I need to put the sound in the Resource folder.

If you've added your sound to the resources using the Resources tab in the project properties, you can play the sound like this....

    Private Sub PlaySound()
        For i As Integer = 1 To 3
            My.Computer.Audio.Play(My.Resources.MySound, AudioPlayMode.WaitToComplete)
        Next
    End Sub

...where "MySound" is the name of the sound file.

>>The sound (about 1-minute-long) does play only a few seconds before it stops and plays from the beginning. It goes on . . .

I don't understand. What's the problem?

Wayne
0
 
dutaAuthor Commented:
>>The sound (about 1-minute-long) does play only a few seconds before it stops and plays from the beginning. It goes on . . .

I don't understand. What's the problem?

Wayne
_____________________________________

The problem is:
I want the sound to play to the end before playing for the second time.
Currently, the sound does play only a for a second before starting to play for the second time.

Thanks!
0
 
carlsiyCommented:
The puse you are encountering might be beacause there are portions on your soundclip that contains blanks (no sound portion) that may be the reason you are encountering pauses in the sound.
0
 
Wayne Taylor (webtubbs)Commented:
What type of file is it? The My.Computer.Audio.Play method plays WAV files only.

http://msdn.microsoft.com/en-us/library/cf1shcah.aspx

FWIW -  sound that plays for 3 minutes when an application starts up would drive me nuts. Even if it played for 5 seconds, I'd be looking for a way to disable it. Do you know of *any* popular application that plays a sound when you start it up?? Of the top of my head, I can't think of any. There's a reason why.....

Wayne
0
 
carlsiyCommented:
maybe its a game thing....
0
 
dutaAuthor Commented:
The file format is .wav.
It played fine in loop before.

Thanks!
0
 
Wayne Taylor (webtubbs)Commented:
>>It played fine in loop before.

OK, so have you changed the 2nd parameter to AudioPlayMode.WaitToComplete, or is it still set to AudioPlayMode.Background?
0
 
dutaAuthor Commented:
It has been changed to AudioPlayMode.WaitToComplete.

Thanks!
0
 
Wayne Taylor (webtubbs)Commented:
Please post *all* of your code here as is. I simply don't understand how it cannot be working for you, especially since you say "It played fine in loop before.".

Wayne
0
 
dutaAuthor Commented:
I am doing something else very urgently.

I will be back to you a while later (not tonight).

Thanks!

duta
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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