当前位置:网站首页>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
边栏推荐
猜你喜欢
Gao Xiang slam14 lecture - note 1
Edge在IE模式下加载网页 - Edge设置IE兼容性
Microservice system design -- microservice monitoring and system resource monitoring design
双位置继电器XJLS-8G/220
Web3 has not been implemented yet, web5 suddenly appears!
Niuke practice 101-c reasoning clown - bit operation + thinking
Microservice system design - service fusing and degradation design
Almost because of json Stringify lost his bonus
【FPGA】基于bt1120时序设计实现棋盘格横纵向灰阶图数据输出
Web3还没实现,Web5乍然惊现!
随机推荐
three. JS first person camera gun following
Acwing's 57th weekly match -- BC question is very good
The most detailed download tutorial of MySQL
Microservice system design -- message caching service design
019 C语言基础:C预处理
neo4j图数据库基本概念
011 C语言基础:C作用域规则
【622. 设计循环队列】
Chapter 2 Introduction to key technologies
009 C语言基础:C循环
导航【机器学习】
微服务系统设计——服务注册与发现和配置设计
013 C语言基础:C指针
快速排序(非遞歸)和歸並排序
快速排序(非递归)和归并排序
双位置继电器XJLS-8G/220
[622. design cycle queue]
013 basics of C language: C pointer
008 C language foundation: C judgment
017 C语言基础:位域和typedef