当前位置:网站首页>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边栏推荐
- 导航【机器学习】
- Redis high availability cluster (sentry, cluster)
- Terminal in pychar cannot enter the venv environment
- 面试:Selenium 中有哪几种定位方式?你最常用的是哪一种?
- Machunmei, the first edition of principles and applications of database... Compiled final review notes
- Microservice system design -- API gateway service design
- 【C语言】关键字的补充
- 微服务系统设计——分布式事务服务设计
- 微服务系统设计——分布式锁服务设计
- Unity point light disappears
猜你喜欢

Microservice system design - service fusing and degradation design

快速排序(非递归)和归并排序

微服务系统设计——微服务调用设计

Reading graph augmentations to learn graph representations (lg2ar)

微信小程序WebSocket使用案例

【B站UP DR_CAN学习笔记】Kalman滤波3

es6 0622三

Microservice system design -- microservice monitoring and system resource monitoring design

STM32 reads IO high and low level status

Cognition - how to fill in 2022 college entrance examination volunteers
随机推荐
Microservice system design -- service registration, discovery and configuration design
017 C语言基础:位域和typedef
Laptop does not have WiFi option solution
Penetration test - file upload / download / include
STM32 reads IO high and low level status
three.js第一人称 相机前枪的跟随
Reading graph augmentations to learn graph representations (lg2ar)
双位置继电器RXMD2-1MRK001984 DC220V
流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)
Some articles about component packaging and my experience
008 C language foundation: C judgment
Epics record reference 5 -- array analog input recordarray analog input (AAI)
016 C language foundation: C language enumeration type
Basic concepts of neo4j graph database
微服务系统设计——分布式事务服务设计
【622. 设计循环队列】
双位置继电器DLS-34A DC0.5A 220VDC
机械转码日记【17】模板,STL简介
015 basics of C language: C structure and common body
013 C语言基础:C指针