Android Question Walkie Talkie example - PTT with volume buttons

trueboss323

Active Member
Licensed User
Longtime User
I saw the tutorial for making an example Walkie Talkie app and had the idea to make it better. I would like it so I can do push to talk with the volume buttons instead. How could I do this? Thanks.
 

Didier9

Well-Known Member
Licensed User
Longtime User
I can see a problem looming... How will you adjust the audio volume if you use the volume control button for PTT?
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
I can see a problem looming... How will you adjust the audio volume if you use the volume control button for PTT?
I could simply make it an option if user prefers to use the volume for PTT otherwise I could add on screen volume buttons.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
I use this code to intercept the phone controls (primarily to intercept the Back/Exit button, but the volume buttons get caught by the same function):

B4X:
ub Activity_KeyPress( KeyCode As Int ) As Boolean 'Return True to consume the event
    Dim result, cv, mv, nv, vc As Int
    'vc = ph.VOLUME_ALARM ' Alarms channel
    vc = Starter.ph.VOLUME_MUSIC ' Music channel ===> Beep uses Music channel
    'vc = ph.VOLUME_NOTIFICATION ' Notifications channel
    'vc = ph.VOLUME_RING ' Phone ring channel
    'vc = ph.VOLUME_SYSTEM ' System sounds channel
    'vc = ph.VOLUME_VOICE_CALL ' Voice calls channel
    If KeyCode = 4 Then ' Exit key
        result = Msgbox2( "QUIT?", "MyApp", "YES", "", "NO", Null)
        If result = DialogResponse.POSITIVE Then Return False
    Else If KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then
        mv = Starter.ph.GetMaxVolume( vc )
        cv = Starter.ph.GetVolume( vc )
        nv = cv + 1
        If nv>mv Then nv = mv
        Starter.ph.SetVolume( vc, nv, True )
    else If KeyCode = KeyCodes.KEYCODE_VOLUME_DOWN Then
        mv = Starter.ph.GetMaxVolume( vc )
        cv = Starter.ph.GetVolume( vc )
        nv = cv - 1
        If nv<0 Then nv = 0
        Starter.ph.SetVolume( vc, nv, True )
    End If
    Return True
End Sub ' Activity_KeyPress

I also have this declaration in the starter module:

B4X:
Dim ph As Phone ' requires Phone library

As long as you return True, the button presses will not be transmitted down to the OS.
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
Heres what I found:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
If KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then
      Log("Up")
                'PTT code
      Return True
   End If
   If KeyCode = KeyCodes.KEYCODE_VOLUME_DOWN Then
      Log("Down")
                'PTT code
      Return True
   End If
End Sub

This code does seem to work in a way but not exactly. I can press one of the volume buttons and get the message shown on the log. But when i hold either button down, then that message starts flooding the log screen. So it is going to try transmit audio many times per second which is not convenient. I need it to recognize it when I'm holding the button down so it only runs once and stops when released. I also tried doing it with KeyUp and that didnt work either.
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
I finally got the KeyUp to work , but with the Keypress it's the same issue I described before. While holding down , it's raising the event simultaneously and I only need it to raise it once.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
use a global var raised = true/false
on first event you set it to true, on keyup you set it to false
If the event raises again and raised is already true then do nothing in the event
 
Last edited:
Upvote 0
Top