Android Question Play a sound as long as a view is pressed

hatzisn

Expert
Licensed User
Longtime User
Hey everybody,

I tried to create a trial app which a beeper sound plays when a user touches the activity in two different areas. I've came up with the following but the response is not good enough as a minor time interval is apparent between the ending of the first and the beginning of the other and further more if the toughing is really brief no sound is heard. Is there a way to overcome this?

This is the code:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private note1 As Beeper
 
    Private ScreenWidth As Long
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
 
    Dim lv As LayoutValues
    lv = GetDeviceLayoutValues
    ScreenWidth = lv.Width
 
End Sub

Sub Activity_Touch (Action As Int, X As Float, Y As Float)
     
    Select Action
        Case Activity.ACTION_DOWN
         
            If x < 0.5 * ScreenWidth Then
                note1.Initialize(60000, 440)
                note1.beep
            Else
                note1.Initialize(60000, 880)
                note1.beep
            End If
         
         
        Case Activity.ACTION_UP
            Try
                note1.Release
            Catch
                Log(LastException)
            End Try
         
    End Select
End Sub
 
Last edited:

Midimaster

Active Member
Licensed User
You should use a timer to "feed" the AudioStreamer. This has the lowest latency.

At the beginning you initialize the AudioStreamer, but you do not send data. The timer is initialized but not enabled. So you have silence. When pressing the view you enable the timer. He will feed the audiostreamer every interval of 100msec with the datas necessary for 100msec, not more. When releasing the view you stop the timer and the sound will stop max 100msec later. This latency is not perceptible. You can have a look on my source codes related to this topic or ask me for writing a sample here.

In this methode the sound can be a simple beep sound like erel showed in his sinus example or also a mp3, which is playing as long as you press the view. Also different sounds for different aereas are no problem or free sound tuning depending on the touched position would be easy to define.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Why is this strange code needed:
B4X:
Dim lv As LayoutValues
    lv = GetDeviceLayoutValues
    ScreenWidth = lv.Width
???

It is used in Sub Activity_Touch to determine if the user has touched in the right part of the screen or the left part of the screen.


Create two Beeper objects and reuse them. Don't initialize and release each time.

I have tried that also but there is no beeper.stop to stop the sound when I don't touch the screen and if I touch on the right side of the screen for the second sound it plays both sounds together.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…