4

I have the following code:

void Update(){
    GetComponent<UnityEngine.UI.Text> ().text = AudioListener.volume.ToString();
}

After pushing to my Android device, it always says "1" when I push the physical volume buttons of the Android. So it is not detecting what the volume of the phone actually is. Is it possible to check the current volume of the phone?

I'm working with the Google Cardboard SDK. Maybe that's important as it uses several cameras at the same time. Maybe is it something related with permissions?

chelder
  • 3,819
  • 6
  • 56
  • 90
  • (Note: AudioListener.volume is totally unrelated. It's just setting the volume Unity plays that item at, just like setting the volume on AudioSource.) – Fattie May 05 '16 at 19:04

2 Answers2

5

Just for the record,

It's a great question, can you get the phone volume setting, from Unity?

Firstly: do note that AudioListener.volume is totally unrelated. That is simply the volume as Unity thinks of it for your playing music or sound effects.

Secondly: I'm afraid to say, I'm fairly sure there is no way to get the phone volume setting, from Unity, as of 2016.

If someone knows better - answer!

Nice question.


UPDATE (from comments)

Note. It is possibly possible to control system volume. But OP would have to write native plugins for that.

Consider these QA:

Note however that changing the volume in iOS (nothing to do with Unity) is a tricky issue regarding whether Apple approve it on the app store. Same for Android. These issues are unrelated to Unity and require knowledge about the app store, Samsung hardware, etc, in whatever year you are reading this. AND note that in many QA about "changing volume" on phones, there is again simply confusion about changing the volume of something in an app versus "the actual phone volume control".

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • That's right. I think that the OP doesn't know the difference between volume of sound/audio in Unity vs System Sound. Every volume control API in Unity is just for the audio coming out of unity. It is possible to control system volume but he needs to write a plugin for that. For Android http://stackoverflow.com/questions/4593552/how-do-you-get-set-media-volume-not-ringtone-volume-in-android – Programmer May 05 '16 at 22:19
  • right. You know, as a curiosity I wonder if that still works on Android? (post is old). They may have clamped down on it. For iOS I should know but I don't. I think they clamped down on it on iOS. – Fattie May 05 '16 at 22:30
  • 1
    Android is almost the-same. I noticed that most things that change in Android is the graphic/ui stuff. Hardware stuff still remain the-same. I just tested it and it still work and not even deprecated. iOS is another story. Everything changes each day. The latest working code is here http://stackoverflow.com/questions/33168497/ios-9-how-to-change-volume-programmatically-without-showing-system-sound-bar-po Not tested but it should work. – Programmer May 05 '16 at 22:56
0

Update for anyone wondering. You CAN check if the device is muted, search for it in Unity docs. It wont really help for all cases, but in my case I had things animated according to volume. So being able to see if the device was muted, was a cool way to stop the animations and make the scene more "dynamic" per say

Jordan
  • 1
  • "search for it in Unity docs" is not a helpful answer on this website. Please reference the part and ideally quote the relevant section so your answer stays useful if unity docs are not available. – Lincoln Mar 24 '23 at 10:22