Android Question Wait for beep has finished to play?

Sub7

Active Member
Licensed User
Longtime User
This might seems stupid and probably it is but i cannot find a way to play a beep for the whole duration inside a loop:

B4X:
 Sub test
Dim rnd_time as int
  For i = 1 To 50
  freq = Rnd(300,1000)
  rnd_time = rnd(100,400)
  beep1.Initialize(rnd_time,freq)
  beep1.Beep
  Next
End sub

Note that i don't know the beep duration as it is random

Tried that but is not ok (play two times)

B4X:
 Sub test
Dim rnd_time as int
  For i = 1 To 50
  freq = Rnd(300,1000)
  rnd_time = rnd(100,400)
  beep1.Initialize(rnd_time,freq)
  beep1.Beep
  Dim seconds As Int
  seconds = 1
  Dim Ti As Long
  Ti = DateTime.Now + (seconds * rnd_time)
  Do While DateTime.Now < Ti
  DoEvents
  Loop
  Next
End sub


Thanks
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I did not understand well what you want to achieve (I have not had the patience to read well).

Maybe you could use a timer; even if the beep has a random duration, you can set the timer for the same duration and then play the next beep.
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
I did not understand well what you want to achieve (I have not had the patience to read well).

Maybe you could use a timer; even if the beep has a random duration, you can set the timer for the same duration and then play the next beep.
Hello, if you execute that sub it will play 50 beeps in a row without duration (a big mess)
I need the beeps finish their duration before looping to the next!

Ty
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Sub7,

as Luca pointed out in his post, you should use a timer to be sure that each sound is played for its intended duration.
The problem with your code is that at each cycle in the for loop you define and start a new beep sound, commanding its immediate playing, which has the effect of stopping the currently executing sound.

So, put the definition of your next sound in the Timer_tick event as long as set the next timer event at the same time this new sound will stop.

Umberto
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub test
    For i = 1 To 50
        Dim freq As Int
      freq = Rnd(300,1000)
        Dim rnd_time As Int
      rnd_time = Rnd(100,400)
        Dim beep1 As Beeper
      beep1.Initialize(rnd_time,freq)
     
        beep1.beep
      Dim Ti As Long = DateTime.Now + rnd_time + 50 ' 50 ms delay between each beep
      Do While DateTime.Now < Ti
          'Log("i="&i&" / Loop")
            DoEvents
      Loop
  Next
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Well done, Manfred!
This one avoids the timer and still lets the sounds to play entirely.
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User

It works, but only one time if you click the button again it wont play.

Thanks!
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
1. You must call Beeper.Release.
2. Don't try to hold the main thread. It will only cause problems. Use a timer instead.

Indeed holding the main thread is causing problems, im now using a timer.
ty
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…