Is it possible to use the phones volume buttons/keys for my App?

awama

Active Member
Licensed User
Longtime User
Hello,

I want to give the smartphone's volume buttons/keys another function in my app?

Is that possible?

Many thanks for help and a little code example!

Walter
 

awama

Active Member
Licensed User
Longtime User
Thanks, the code works fine.

There is also a sound of the Volume Control (peep, peep, peep) everytime I press the buttons. How can I mute this sound?

Walter
 
Upvote 0

awama

Active Member
Licensed User
Longtime User
... I try

Phone.SetMute(Phone.VOLUME_SYSTEM,True)
and
Phone.VOLUME_ALARM
Phone.VOLUME_RING
Phone.VOLUME_VOICE_CALL
Phone.VOLUME_MUSIC
Phone.VOLUME_NOTIFICATION

but the sound does not mute. Do I have a mistake?

Walter
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code, it works for me:
B4X:
Sub Process_Globals
    Dim phone1 As Phone
    Dim previousVolume As Int
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub
Sub Activity_Resume
    previousVolume = phone1.GetVolume(phone1.VOLUME_RING)
    phone1.SetVolume(phone1.VOLUME_RING, 0, False)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    phone1.SetVolume(phone1.VOLUME_RING, previousVolume, False)
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    Select KeyCode
        Case KeyCodes.KEYCODE_VOLUME_UP
            Log("Up")
            Return True
        Case KeyCodes.KEYCODE_VOLUME_DOWN
            Log("Down")
            Return True
    End Select
End Sub
 
Upvote 0
Top