Unity 调用 Android Native 方法获得Android系统音量

【Unity 调用 Android Native 方法获得Android系统音量】提兵百万西湖上,立马吴山第一峰!这篇文章主要讲述Unity 调用 Android Native 方法获得Android系统音量相关的知识,希望能为你提供帮助。
学习雷锋,好榜样,接下来的这一系类教程里,将通过unity来实现android端的一些常用功能,
不需要在 Asset/Plugins/Android 目录下引用jar包或者aar包,这是重点。

1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class Volume : MonoBehaviour { 6 7 8private const string currentVolume = "getStreamVolume"; //当前音量 9private const string maxVolume = "getStreamMaxVolume"; //最大音量 10 11private const int STREAM_VOICE_CALL = 0; 12private const int STREAM_SYSTEM = 1; 13private const int STREAM_RING = 2; 14private const int STREAM_MUSIC = 3; 15private const int STREAM_ALARM = 4; 16private const int STREAM_NOTIFICATION = 5; 17const int STREAM_DTMF = 8; 18 19//创建几个 "3D Text" 用于接收音量值 20publicTextMesh STREAM_VOICE_CALL_Text; 21publicTextMesh STREAM_SYSTEM_Text; 22publicTextMesh STREAM_RING_Text; 23publicTextMesh STREAM_MUSIC_Text; 24publicTextMesh STREAM_ALARM_Text; 25publicTextMesh STREAM_NOTIFICATION_Text; 26publicTextMesh STREAM_DTMF_Text; 27 28 29private static AndroidjavaObject audioManager; 30 31 32void Awake() 33{ 34AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 35AndroidJavaObject currentActivity = UnityPlayer.GetStatic< AndroidJavaObject> ("currentActivity"); 36audioManager = currentActivity.Call< AndroidJavaObject> ("getSystemService", new AndroidJavaObject("java.lang.String", "audio")); 37} 38void Update() 39{ 40 ////current volume value 41 //STREAM_VOICE_CALL_Text.text = audioManager.Call< int> (currentVolume, STREAM_VOICE_CALL).ToString (); 42 //STREAM_SYSTEM_Text.text = audioManager.Call< int> (currentVolume, STREAM_SYSTEM).ToString (); 43 //STREAM_RING_Text.text = audioManager.Call< int> (currentVolume, STREAM_RING).ToString (); 44 //STREAM_MUSIC_Text.text = audioManager.Call< int> (currentVolume, STREAM_MUSIC).ToString (); 45 //STREAM_ALARM_Text.text = audioManager.Call< int> (currentVolume, STREAM_ALARM).ToString (); 46 //STREAM_NOTIFICATION_Text.text = audioManager.Call< int> (currentVolume, STREAM_NOTIFICATION).ToString (); 47 //STREAM_DTMF_Text.text = audioManager.Call< int> (currentVolume, STREAM_DTMF).ToString (); 48 49//Max volume value 50STREAM_VOICE_CALL_Text.text = audioManager.Call< int> (maxVolume, STREAM_VOICE_CALL).ToString (); 51STREAM_SYSTEM_Text.text = audioManager.Call< int> (maxVolume, STREAM_SYSTEM).ToString (); 52STREAM_RING_Text.text = audioManager.Call< int> (maxVolume, STREAM_RING).ToString (); 53STREAM_MUSIC_Text.text = audioManager.Call< int> (maxVolume, STREAM_MUSIC).ToString (); 54STREAM_ALARM_Text.text = audioManager.Call< int> (maxVolume, STREAM_ALARM).ToString (); 55STREAM_NOTIFICATION_Text.text = audioManager.Call< int> (maxVolume, STREAM_NOTIFICATION).ToString (); 56STREAM_DTMF_Text.text = audioManager.Call< int> (maxVolume, STREAM_DTMF).ToString (); 57 58} 59 }

 

    推荐阅读