Search Unity

Accessing and changing the volume of the device

Discussion in 'Android' started by bumble864, May 14, 2012.

  1. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    So it's getting late and this might start to sound like ramblings, I've been doing readings on Unity and Android's website for a few hours now and while I think I'm closer to an answer I'm also confused as ;).
    I figured out how to know when the buttons on my phone are pushed, and to use them to in my game. Now I'm looking to raise and lower the volume of the device I'm playing on, and it's not as easy as I'd hoped.

    This has been one of the more helpful links I've found so far http://sudarmuthu.com/blog/adjusting-the-volume-in-android-through-code
    So far what I've gathered is I have to get an Instance of the audio manager, then pass certain code on to that.

    Then I was reading on Unity's website that it looks like I have to write a plugin that allows me to call these functions.

    I'm very early on in moving my game to my android. I'm currently looking for some direction and reassurance that I'm on the right path to change the volume of my device. Do I need to write a plug in? So any help would be great. I'm looking for more direction so I can figure out how to access the android commands and less "write this code" because if I figure out how to do it I'm sure I'll figure out more along the way.
     
  2. WARdd

    WARdd

    Joined:
    Aug 10, 2015
    Posts:
    6
    I know this is an ancient topic, but I stumbled across this issue and found a solution, so I'll post the answer for anyone else who might find themselves here. In order to change the volume you need to access Unity java code. The code below seems to work for all devices I had.

    Code (CSharp):
    1.  
    2.     private static int GetDeviceMaxVolume() {
    3.         int streammusic = 3;
    4.         return deviceAudio.Call<int>("getStreamMaxVolume", streammusic);
    5.     }
    6.  
    7.     private static void SetDeviceVolume(int value) {
    8.         int streammusic = 3;
    9.         int flagshowui = 0;
    10.      
    11.         deviceAudio.Call("setStreamVolume", streammusic, value, flagshowui);
    12.     }
    13.  
    14.     private static AndroidJavaObject audioManager;
    15.  
    16.     private static AndroidJavaObject deviceAudio { get {
    17.         if(audioManager == null) {
    18.             AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    19.             AndroidJavaObject context = up.GetStatic<AndroidJavaObject>("currentActivity");
    20.  
    21.             string audioName = context.GetStatic<string>("AUDIO_SERVICE");
    22.  
    23.             audioManager = context.Call<AndroidJavaObject>("getSystemService", audioName);
    24.         }
    25.         return audioManager;
    26.     } }
    27.  
     
    Fl0oW likes this.
  3. Kobald-Klaus

    Kobald-Klaus

    Joined:
    Jun 20, 2014
    Posts:
    29
    hm, I get an error when I use my bundle id:
    AndroidJavaException: java.lang.ClassNotFoundException: com.sfinxit.vrmovieplayer

    Device: Oculus Go
     
  4. Fl0oW

    Fl0oW

    Joined:
    Apr 24, 2017
    Posts:
    18
    A bit late, but
    Code (CSharp):
    1. AndroidJavaClass up = AndroidJavaClass("com.unity3d.player.UnityPlayer");
    does not take your bundleID as input, this is the class name of
    UnityPlayer
    that is accessed as a
    AndroidJavaClass
    here.

    See also:
    https://docs.unity3d.com/540/Documentation/Manual/PluginsForAndroid.html
     
  5. iansp

    iansp

    Joined:
    Nov 21, 2016
    Posts:
    4
    I got the following error:
    JNI DETECTED ERROR IN APPLICATION: static jfieldID 0x6f933670 not valid for class java.lang.Class<com.unity3d.player.UnityPlayerActivity>
    04-23 09:42:05.221 1423 1441 F art : art/runtime/java_vm_ext.cc:470] in call to GetStaticObjectField


    Seems like "AUDIO_SERVICE" maybe isn't the right field anymore?
     
  6. Fl0oW

    Fl0oW

    Joined:
    Apr 24, 2017
    Posts:
    18
    I saw some problems with some Android devices as well. Samsung S8 wouldn't work, while Google Pixel worked just fine. I ended up re-writing this differently and haven't found any issue since, so hopefully this helps someone out as well.

    Code (CSharp):
    1.  
    2. public class AndroidNativeVolumeService
    3.     {
    4.         static int STREAMMUSIC;
    5.         static int FLAGSHOWUI = 1;
    6.  
    7.         private static AndroidJavaObject audioManager;
    8.  
    9.         private static AndroidJavaObject deviceAudio
    10.         {
    11.             get
    12.             {
    13.                 if (audioManager == null)
    14.                 {
    15.                     AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    16.                     AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
    17.                     AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
    18.                     AndroidJavaClass audioManagerClass = new AndroidJavaClass("android.media.AudioManager");
    19.                     AndroidJavaClass contextClass = new AndroidJavaClass("android.content.Context");
    20.  
    21.                     STREAMMUSIC = audioManagerClass.GetStatic<int>("STREAM_MUSIC");
    22.                     string Context_AUDIO_SERVICE = contextClass.GetStatic<string>("AUDIO_SERVICE");
    23.  
    24.                     audioManager = context.Call<AndroidJavaObject>("getSystemService", Context_AUDIO_SERVICE);
    25.  
    26.                     if (audioManager != null)
    27.                         DebugLog.Log("[AndroidNativeVolumeService] Android Audio Manager successfully set up");
    28.                     else
    29.                         DebugLog.Log("[AndroidNativeVolumeService] Could not read Audio Manager");
    30.                 }
    31.                 return audioManager;
    32.             }
    33.  
    34.         }
    35.  
    36.         private static int GetDeviceMaxVolume()
    37.         {
    38.             return deviceAudio.Call<int>("getStreamMaxVolume", STREAMMUSIC);
    39.         }
    40.  
    41.         public float GetSystemVolume()
    42.         {
    43.             int deviceVolume = deviceAudio.Call<int>("getStreamVolume", STREAMMUSIC);
    44.             float scaledVolume = (float)(deviceVolume / (float)GetDeviceMaxVolume());
    45.  
    46.             return scaledVolume;
    47.         }
    48.  
    49.         public void SetSystemVolume(float volumeValue)
    50.         {
    51.             int scaledVolume = (int)(volumeValue * (float)GetDeviceMaxVolume());
    52.             deviceAudio.Call("setStreamVolume", STREAMMUSIC, scaledVolume, FLAGSHOWUI);
    53.         }
    54.     }