当前位置:网站首页>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边栏推荐
- 面试:Selenium 中有哪几种定位方式?你最常用的是哪一种?
- 关于元器件封装的一些文章和一下我的体会
- 020 C语言基础:C语言强制类型转换与错误处理
- 022 basics of C language: C memory management and C command line parameters
- [C language] keyword supplement
- pycharm 如何安装 package
- Vue学习笔记(五)Vue2页面跳转问题 | vue-router路由概念、分类与使用 | 编程式路由导航 | 路由组件的缓存 | 5种路由导航守卫 | 嵌套路由 | Vue2项目的打包与部署
- 微服务系统设计——统一鉴权服务设计
- How JQ gets the ID name of an element
- 【FPGA】 基于FPGA分频,倍频设计实现
猜你喜欢

【FPGA】 基于FPGA分频,倍频设计实现

Terminal in pychar cannot enter the venv environment

微服务系统设计——分布式事务服务设计

Web3还没实现,Web5乍然惊现!

牛客练习赛101-C 推理小丑---位运算+思维
![[622. design cycle queue]](/img/f2/d499ac9ddc50b73f8c83e8b6af2483.png)
[622. design cycle queue]

什么是BFC?有什么用?

双位置继电器XJLS-8G/220
![[station B up dr_can learning notes] Kalman filter 1](/img/18/ee21d31f6a118e4e4ad466b55361cc.gif)
[station B up dr_can learning notes] Kalman filter 1

Niuke practice 101-c reasoning clown - bit operation + thinking
随机推荐
Reading graph augmentations to learn graph representations (lg2ar)
halcon常用仿射变换算子
Gao Xiang slam14 lecture - note 1
Edge在IE模式下加载网页 - Edge设置IE兼容性
019 C语言基础:C预处理
AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法
016 C语言基础:C语言枚举类型
Almost because of json Stringify lost his bonus
使用域名转发mqtt协议,避坑指南
DAST black box vulnerability scanner part 6: operation (final)
Halon common affine transformation operators
【FPGA】基于bt1120时序设计实现棋盘格横纵向灰阶图数据输出
Tsinghua University open source software mirror website
[622. design cycle queue]
019 basics of C language: C preprocessing
[C language] keyword supplement
高翔slam14讲-笔记1
微服务系统设计——分布式事务服务设计
py2neo基本语法
认知篇----2022高考志愿该如何填报