Hi all, Im trying to detect the volume up being pressed in android device. I have read this event is not caught in unity as its part of the OS but I have read also that some kind of plugin could be made to catch this event and so act in unity when its pressed, how could this be done? I know nothing about pluging making and only got the code someone posted somewhere but really my knowledge in that is zero. This is from what I found, there are similar answers derived from this same code, but this is java, correct me if wrong, so dont know how to combine this inside unity https://stackoverflow.com/questions/2874743/android-volume-buttons-used-in-my-application
#Detect volume up being pressed in android device
1 messages · Page 1 of 1 (latest)
found this, apparently this could work, but i have never made plugin in java for unity, tried it the way the docs said and this didnt work, could you tell me what im missing here? https://forum.unity.com/threads/accessing-and-changing-the-volume-of-the-device.135907/
in the latest comments there is the code that i think my work for this, but as said i dont know how integration should be done
Can you share your code?
I'm not able to test on Android currently.
And I've honestly never worked with this before.
All I know is that you can write Java plugins and interface with them using low- or high-level APIs.
Hopefully, you'll be able to interface with these built-in classes without any plugins.
@gusty wedge.
Thanks for commenting on this, well I started a new project only to test this funcitonality, I mean the moment I detect the volume up pressed then i will add to bigger project, the code Im using comes from the thread I showed before, its java
but i dont know how to incorporate this in the c# code which is what is needed of course as it will access the data the plugin is gathering I think, but all the examples I saw to integrate a plugin made in java are very specific and load modules from android that cant apply to this example
I have to leave for a while, but please let me know what you think of this or if you know how to merge this, I had no idea this was not accesible from unity as is, I thought it was odd because its button of device anyway but apparently its not there or i havent found it yet
Well the original goal is to catch the input from slefie button, but this kind of button seems to reflect n phone as volume up being pressed, I say this just in case it can be easier
maybe to read the bluetooth stream from the device who knows
Oh, thanks a lot and Im very sorry, I swear I read somewhere it was java and after opening it I was like lost in that so thought it could be it, I will have a look and see if I can tackle what i want from here, please dont close the thread as I may still get stuck since it uses stuff i never saw before, will confirm tomorrow if I can do what i think it can do and then we can close it as solved of course, but give me this time please
Well I have managed to deal with the code now that i know its c#, again sorry for error about this and thanks a lot for checking the code. What I did is a workaround based on what someone made, apparently what unity cant catch is the press of the volume up button (this is what people was doing in java), but the user who made this code found a workaround and in this code I showed he is catching the current volume so its easy to make code that just checks if volume level is higher or not, so if it increases then volume up is being pressed, for my case this works really, the other way would be more clean but I think this will work. However I have one doubt, well its not important because as said the code works, but I have one little doubt, he made a function that returns max volume, but in my device this returns 15, however my volume when on top is 1 so it goes from 0 to 1 but according to that function it should be higher, as said its more of a curiosity than anything i need now as the code works for what i need, but do you know why the max volume is seen as different? again please if you dont know dont worry its just a matter of curiosity, I really thank you for the help because now i have the code that works and does what i was needing
There's unscaled volume and scaled volume.
Unscaled volume is what Android uses internally. It goes from 0 to... some number which changes depending on the device. Evidently, this number is 15 for you, so there are 16 steps (including 0).
The unscaled volume is represented as a whole number, an integer.
But the code you're using converts this to a scaled volume which ranges from 0f to 1f, a floating point number.
That means you can set the volume to ~80% by calling SetSystemVolume(.8f) on any device, and it will set it to the closest unscaled volume (rounded down).
I read that the max volume can be 13 or 15, so 14 or 16 steps.
So on your device, .8f scaled would be 12 unscaled.
On a device with 13 as the maximum, .8f would be 10 unscaled. It would be floored (rounded down) to an integer from 10.4f, so you actually lose some precision on some devices. Calling SetSystemVolume(.8f) would actually set it the volume to ~77%, not 80% as it would on your device.
@gusty wedge.
thats impressive knowledge you have, I had no idea about all this, thanks a lot for explaining, indeed I noticed that although I was setting the volume to 0.9f it was not set at that value but more like 0.93333f (I think it was that maybe Im wrong) so this explains why is the case as you said, that the percentage we set is not exactly the volume level we get but dependant on the device characteristics
Exactly!
Also, I know nothing about Android, I just read the code.
Remember to read the documentation. :P
yes but your explanation is so clear and I cant extract all that from the code really, I was thinking volume was being set to a slightly different value for whatever the reason like the top volume allowed or so, you cleared all my doubts and even more really
You're welcome!
Ah yes the docs, I didnt go there in first place as I was thinking this was java, again my bad
Even if it were Java, the docs would still be helpful, actually.
I linked the Android documentation, because I noticed your code was using the AudioManager from Android.
ah, I had no idea about this, i never touch java code, so its good to know in case i meet some java in future code
Also I was not used to access android specific things, i mean I used to make games so in these most of things are very much like the same we do for pc or other platform with slight variations, or well thats what i was doing so this is first time i get out of the usual bounds
but really, as said, thanks a lot for explanation and clarifying this, it works perfect for me
👍 😃
I learned how to use the high-level Android API in Unity, so thanks for that. 👍
well @turbid ore it wasnt me, I just searched everywhere for anyone telling how to achieve this, but Im glad you learned something, also a fact that may or may not be of use for you or other people, this volume up corresponds to the signal selfie buttons send to android devices, or well at least to my device, i dont know if its same for all but i assume yes, so people can use this strategy to detect these buttons being pressed for whatever the goal they have, could be like a game having a single button for all the actions and the smallest gamepad ever i guess
well I dont have a selfie button, but a friend let me have his selfie stick and the signal matches that so I assume all selfie buttons or sticks to the same
Ah @turbid ore , forgot to ask, is there anything I should do to mark the thread as solved or give kudos to you? Im not familiar with the thread structure in discord so Im not certain if I have to do something