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 
 8     private const string currentVolume = "getStreamVolume";//当前音量
 9     private const string maxVolume = "getStreamMaxVolume";//最大音量
10 
11     private const int STREAM_VOICE_CALL = 0;
12     private const int STREAM_SYSTEM = 1;
13     private const int STREAM_RING = 2;
14     private const int STREAM_MUSIC = 3;
15     private const int STREAM_ALARM = 4;
16     private const int STREAM_NOTIFICATION = 5;
17      const int STREAM_DTMF = 8;
18 
19     //创建几个 "3D Text" 用于接收音量值
20     public  TextMesh STREAM_VOICE_CALL_Text;
21     public  TextMesh STREAM_SYSTEM_Text;
22     public  TextMesh STREAM_RING_Text;
23     public  TextMesh STREAM_MUSIC_Text;
24     public  TextMesh STREAM_ALARM_Text;
25     public  TextMesh STREAM_NOTIFICATION_Text;
26     public  TextMesh STREAM_DTMF_Text;
27 
28 
29     private static AndroidJavaObject audioManager;
30 
31 
32     void Awake()
33     {
34         AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
35         AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
36         audioManager = currentActivity.Call<AndroidJavaObject>("getSystemService", new AndroidJavaObject("java.lang.String", "audio"));
37     }
38     void 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
50         STREAM_VOICE_CALL_Text.text = audioManager.Call<int> (maxVolume, STREAM_VOICE_CALL).ToString ();
51         STREAM_SYSTEM_Text.text = audioManager.Call<int> (maxVolume, STREAM_SYSTEM).ToString ();
52         STREAM_RING_Text.text = audioManager.Call<int> (maxVolume, STREAM_RING).ToString ();
53         STREAM_MUSIC_Text.text = audioManager.Call<int> (maxVolume, STREAM_MUSIC).ToString ();
54         STREAM_ALARM_Text.text = audioManager.Call<int> (maxVolume, STREAM_ALARM).ToString ();
55         STREAM_NOTIFICATION_Text.text = audioManager.Call<int> (maxVolume, STREAM_NOTIFICATION).ToString ();
56         STREAM_DTMF_Text.text = audioManager.Call<int> (maxVolume, STREAM_DTMF).ToString ();
57 
58     }
59 }

 

posted on 2017-11-01 13:28  Jason_c  阅读(2500)  评论(0编辑  收藏  举报