Beeper Bug?

RandomCoder

Well-Known Member
Licensed User
Longtime User
Beeper Bug? [SOLVED]

I've searched the Forum and read the help but nothing is working for me and after spending all day trying to get my app to work it's now time to ask for help.

Attached is a sample app that I've created which demonstrates the problem and I'd be grateful if anyone here is able to test it and see if they get the same results. Basically the beeper appears to stop working if called more than 30 times!

The actual app I've created counts down from a preset time to zero, any screen presses or motion resets the time and should the time start to get low then the phone warns the user by beeping. All has been working well and I've made several modifications as requested by the inteded user BUT, a problem that has been occuring is that the time has counted to zero with no audible warning. I've experimented today and found that it appears to be the audio library that might be at fault (however it's more usual that I'm actually doing something wrong :eek:).

The sample app simply counts down from 15s, when it gets to 10s it starts to beep every other second and when the time is less than 5s it beeps twice per second. The value under the time shows how many times the beep sub has been called and every time it gets to 30 on my device the beep stops working even though it is still being called. I've tried declaring the beep object as a global rather than each time the sub is called and I've played with the threading library which is what I'm actually using in my app, but in the interests of keeping the sample as simple as possible I've left the threading stuff out.

Maybe the problem is with my device (an LG GT540 running 2.1) but I just don't know and I've come to the limit of my ability.
Any advice on what I've done wrong is as ever greatly appreciated.

Kind regards,
Randomcoder
 

Attachments

  • BeeperBug.zip
    6.6 KB · Views: 389
Last edited:

stevel05

Expert
Licensed User
Longtime User
Hi RandomCoder,

I think the problem is that each time you initialize the beeper, it creates a new object. And eventually too many.

I am sure it wasn't envisaged that the beeper would be used in this way when it was written, but when Erel reads this he may like to add a release method which would allow this to be done.

In the meantime you can achieve the same thing using agrahams reflection library, create the beeper as a Global variable (may not be necessary but that's how I tested it) change your beep sub to:

B4X:
Sub Process_Globals
     'add
     Dim b As Beeper
end sub

Sub Beep(T As Int)
   'Sound a beep with length T

   If lblBeeps.Text > 0 Then
      Dim r As Reflector
      r.Target=b
      r.Target=r.GetField("at")
      r.RunMethod("release")
   End If
   
   b.Initialize(T,1500)
   
   b.Beep
   'Show how many times this sub is called
   Dim bCount As Int
   bCount=lblBeeps.Text+1
   lblBeeps.Text=bCount
End Sub

I've tested this on my HTC hero and it appears to do the trick.

Another alternative would have been to create two beepers with the required durations and alternate between them.

Steve

Edit: Out of curiosity I just checked the unfiltered logs which when it failed gave the error message:

[ android.media.AudioTrack ] Error code -20 when initializing AudioTrack. no more track names available
 
Last edited:
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
You can use two process global Beeper objects. One set to 200ms and the other to 1000ms.

Hi Guys,

Thanks for the advice, I'm at work at the moment but will try as soon as I get home. Am I right in thinking the problem is then due to the number of times the object was being initialised? (As I had already attempted difining it as a global with no better success).

Thanks,
RandomCoder
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Question about initialisation.

Thanks for the help, I can confirm that my app is now working correctly :sign0188:

May I ask just one last question though about initialisting objects? The reason for declaring and initialising the beeper locally was because most of the time the beeper is not required and so the memory could be freed-up when not in use, this is what I pressumed to be the preferred method as many examples tend to do it this way. The user guide is a little vague on best practice for declaring and initialising objects, but page 120 states...
10.5.3 Local variables
Variables declared in a subroutine are local to this subroutine.
They are "private" and can only be accessed from within the subroutine where they were declared.
All objects types can be declared as local variables.
At each call of the subroutine the local variables are initialized to their default value or to any other
value you have defined in the code and are 'destroyed' when the subroutine is left.

Declaring and initialising the beeper locally also meant that the duration and frequency could be passed to the sub to give different types of beep (not that I actually require this functionality). So is my approach correct and this is just a one off problem that affects the Beep object? The reason for asking is that I also use the phones vibrator and this is still being declared locally but without any noticeable problems.

Cheers,
RandomCoder
 
Upvote 0
Top