当前位置:网站首页>Unity中跨平臺獲取系統音量
Unity中跨平臺獲取系統音量
2022-06-27 05:16:00 【逍遙遊俠】
目前支持安卓和IOS,Windows平臺暫時不支持,Vista之前和之後的處理方式不同,暫時沒有測試,以後有需要再補充。
C#中的核心代碼如下:
#if UNITY_ANDROID
private const string FuncCurrentVolume = "getStreamVolume";//當前音量
private const string FuncMaxVolume = "getStreamMaxVolume";//最大音量
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;
//
// // 獲得音量範圍和獲取/設置當前音量
// public static int MaxValue{get { return int.Parse(iMaxValue.ToString());}}
// public static int MinValue{get { return int.Parse(iMinValue.ToString()); }}
// /*
// * 在winmm.dll中
// *第一個參數可以為0,錶示首選設備
// *第二個參數為音量:0xFFFF為最大,0x0000為最小,
// *其中高比特(前兩比特)錶示右聲道音量,低比特(後兩比特)錶示左 聲道音量 。
// */
// //[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);
// 以上方式似乎只有Windows XP可用!!!
// CoreAudioApi( Windows Vista 及以後)
// #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);//返回0-10
Debug.Log($"GetSystemVolume maxVolume: {maxVolume.ToString()}");
}
catch(Exception e)
{
Debug.Log($"Exception GetSystemVolume {e.ToString()}");
}
#elif UNITY_IOS
volume = IOS_GetSystemVolume();//返回0-1
#endif
//Debug.Log($"GetSystemVolume curVolume: {volume.ToString()}");
return volume;
}IOS版本對應的Plugins目錄下的.mm文件中加入如下代碼:
#if defined(__cplusplus)
extern "C"
{
#endif
float IOS_GetSystemVolume(){
// 需要引入<AVFoundation/AVFoundation.h>
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:true error:NULL];
float systemVolume = audioSession.outputVolume;
return systemVolume;
}
#if defined(__cplusplus)
}
#endif边栏推荐
- 009 C语言基础:C循环
- 微服务系统设计——服务链路跟踪设计
- When STM32 turns off PWM output, it is a method to fix IO output at high or low level.
- [unity] button of UI interactive component & summary of optional base classes
- Microservice system design -- service registration, discovery and configuration design
- Laptop does not have WiFi option solution
- 017 basics of C language: bit field and typedef
- neo4j图数据库基本概念
- 010 C语言基础:C函数
- jq怎么获取倒数的元素
猜你喜欢
![Golang Hello installation environment exception [resolved]](/img/30/bddba695e4c0059102e86de346b58d.png)
Golang Hello installation environment exception [resolved]

Deep dive kotlin synergy (XV): Test kotlin synergy

Vue学习笔记(五)Vue2页面跳转问题 | vue-router路由概念、分类与使用 | 编程式路由导航 | 路由组件的缓存 | 5种路由导航守卫 | 嵌套路由 | Vue2项目的打包与部署

RTP 发送PS流工具(已经开源)

Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems

微服务系统设计——分布式定时服务设计

Pycharm 中 Terminal 无法进入 venv 环境的问题

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

Some articles about component packaging and my experience

微服务系统设计——服务链路跟踪设计
随机推荐
Quick sort (non recursive) and merge sort
How JQ gets the reciprocal elements
洛谷P2939 [USACO09FEB]Revamping Trails G 题解
Flink生产问题(1.10)
Mechanical transcoding journal [17] template, STL introduction
[unity] button of UI interactive component & summary of optional base classes
流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)
清华大学开源软件镜像站网址
006 C语言基础:C存储类
neo4j图数据库基本概念
Deep dive kotlin synergy (XV): Test kotlin synergy
Microservice system design -- distributed lock service design
020 basics of C language: C language forced type conversion and error handling
双位置继电器XJLS-8G/220
微服务系统设计——分布式事务服务设计
Microservice system design -- message caching service design
017 basics of C language: bit field and typedef
016 C language foundation: C language enumeration type
微信小程序刷新当前页面
Vue学习笔记(五)Vue2页面跳转问题 | vue-router路由概念、分类与使用 | 编程式路由导航 | 路由组件的缓存 | 5种路由导航守卫 | 嵌套路由 | Vue2项目的打包与部署