当前位置:网站首页>Get system volume across platforms in unity

Get system volume across platforms in unity

2022-06-27 05:16:00 Free Ranger

Currently supports Android and IOS,Windows The platform does not support ,Vista Before and after the treatment is different , There is no test for now , It needs to be added in the future . 

C# The core code in is as follows :

#if UNITY_ANDROID
        private const string FuncCurrentVolume = "getStreamVolume";// Current volume 
        private const string FuncMaxVolume = "getStreamMaxVolume";// Maximum volume 

        private const int STREAM_VOICE_CALL = 0;
        private const int STREAM_SYSTEM = 1;
        private const int STREAM_RING = 2;
        private const int STREAM_MUSIC = 3;
        private const int STREAM_ALARM = 4;
        private const int STREAM_NOTIFICATION = 5;
        private const int STREAM_DTMF = 8;

        private AndroidJavaObject m_mainActivity;
        private AndroidJavaObject m_audioManager;
#endif
  
      private void Init()
        {
#if UNITY_ANDROID 
            try
            {
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
                //Debug.Log($"currentActivity : {jo}");
                m_mainActivity = jo.GetStatic<AndroidJavaObject>("Instance");
                m_audioManager = m_mainActivity.Call<AndroidJavaObject>("getSystemService", new AndroidJavaObject("java.lang.String", "audio"));
                //Debug.Log($"m_mainActivity : {m_mainActivity}");
            }
            catch(Exception e)
            {
                Debug.LogError(".MainActivity is missing !!!");
            }
#elif UNITY_IOS

#endif
        }

#if UNITY_IOS
        [DllImport("__Internal")]
        private static extern float IOS_GetSystemVolume();
#endif
 
       // #if UNITY_EDITOR_WIN
        //         private const UInt32 iMaxValue = 0xFFFF;
        //         private const UInt32 iMinValue = 0x0000;
        //          
        //          //  Get volume range and get / Set the current volume 
        //          public static int MaxValue{get { return int.Parse(iMaxValue.ToString());}}
        //          public static int MinValue{get { return int.Parse(iMinValue.ToString()); }}
        //         /*
        //         *  stay winmm.dll in    
        //         * The first parameter can be 0, Indicates the preferred device    
        //         * The second parameter is volume :0xFFFF For the biggest ,0x0000 Is minimum ,
        //         * The high position ( Top two ) Indicates the right channel volume , Low position ( The last two ) To the left   Channel volume  .
        //         */
        //         //[DllImport("winmm.dll")]
        //         //private static extern long waveOutSetVolume(UInt32 deviceID, UInt32 Volume);
        //         [DllImport("winmm.dll")]
        //         private static extern long waveOutGetVolume(UInt32 deviceID, out UInt32 Volume);

        //          There seems to be only Windows XP You can use !!!

        //         CoreAudioApi( Windows Vista  And later )
        // #endif

        public float GetSystemVolume()
        {
            float volume = 1f;
#if UNITY_EDITOR_WIN
//             UInt32 d, v;
//             d = 0;
//             long result = waveOutGetVolume(d, out v);
// 
//             UInt32 vleft = v & 0xFFFF;
//             UInt32 vright = (v & 0xFFFF0000) >> 16;
//             UInt32 all = vleft | vright;
//             Debug.Log($"GetSystemVolume vleft: {vleft.ToString()} vright: {vright.ToString()}");
// 
//             UInt32 value = (all * UInt32.Parse((MaxValue - MinValue).ToString()) / ((UInt32)iMaxValue));
//             volume = int.Parse(value.ToString());
#elif UNITY_ANDROID
            try
            {
                volume = m_audioManager.Call<int>(FuncCurrentVolume, STREAM_MUSIC);
                int maxVolume = m_audioManager.Call<int> (FuncMaxVolume, STREAM_MUSIC);// return 0-10
                Debug.Log($"GetSystemVolume maxVolume: {maxVolume.ToString()}");
            }
            catch(Exception e)
            {
                Debug.Log($"Exception GetSystemVolume {e.ToString()}");
            }
#elif UNITY_IOS
            volume = IOS_GetSystemVolume();// return 0-1
#endif
            //Debug.Log($"GetSystemVolume curVolume: {volume.ToString()}");
            return volume;
        }

IOS The version corresponds to Plugins In the catalog .mm Add the following code to the file :

#if defined(__cplusplus)
extern "C"
{
#endif

float IOS_GetSystemVolume(){
    //  Need to introduce <AVFoundation/AVFoundation.h>
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive:true error:NULL];
    float systemVolume = audioSession.outputVolume;
    return systemVolume;
}

#if defined(__cplusplus)
}
#endif

原网站

版权声明
本文为[Free Ranger]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270516040204.html