Skip to content

Commit 9a6d78e

Browse files
committed
Add VolumeButtons.SystemVolumeLevel
1 parent 9064f1e commit 9a6d78e

9 files changed

Lines changed: 244 additions & 211 deletions

File tree

Assets/Plugins/VolumeButtonsPlugin/Android/VolumeButtonsActivity.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import android.os.Bundle;
77
import android.util.Log;
88
import android.view.KeyEvent;
9+
import android.content.Context;
10+
import android.media.AudioManager;
911

1012
import com.unity3d.player.UnityPlayer;
1113
import com.unity3d.player.UnityPlayerActivity;
1214

1315
public class VolumeButtonsActivity extends UnityPlayerActivity {
14-
1516
/**
1617
* Keep in sync with VolumeButtonsEvent in C#
1718
*/
@@ -30,28 +31,26 @@ public String getValue() {
3031
}
3132
}
3233

34+
private static final String TAG = "VolumeButtonsActivity";
3335
private static final String MESSAGE_NAME = "_OnVolumeButtonEvent";
3436
private List<String> gameObjectNames = new ArrayList<>();
3537

3638
protected void onCreate(Bundle savedInstanceState) {
3739
super.onCreate(savedInstanceState);
38-
// print debug message to logcat
39-
Log.d("VolumeButtonsActivity", "onCreate called!");
40+
Log.d(TAG, "onCreate called!");
4041
}
4142

42-
public void addGameObjectListener(String gameObjectName)
43-
{
44-
synchronized (gameObjectNames)
45-
{
43+
public void addGameObjectListener(String gameObjectName) {
44+
synchronized (gameObjectNames) {
4645
gameObjectNames.add(gameObjectName);
46+
Log.d(TAG, "addGameObjectListener " + gameObjectName);
4747
}
4848
}
4949

50-
public void removeGameObjectListener(String gameObjectName)
51-
{
52-
synchronized (gameObjectNames)
53-
{
50+
public void removeGameObjectListener(String gameObjectName) {
51+
synchronized (gameObjectNames) {
5452
gameObjectNames.remove(gameObjectName);
53+
Log.d(TAG, "removeGameObjectListener " + gameObjectName);
5554
}
5655
}
5756

@@ -78,4 +77,19 @@ public boolean dispatchKeyEvent(KeyEvent event) {
7877

7978
return super.dispatchKeyEvent(event);
8079
}
80+
81+
public float getSystemVolumeLevel() {
82+
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
83+
if (audioManager == null) {
84+
return -1.f;
85+
}
86+
87+
Log.d(TAG, "getSystemVolumeLevel called!");
88+
89+
// https://stackoverflow.com/questions/43886901/how-to-convert-android-stream-volume-to-a-value-between-0-and-1
90+
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
91+
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
92+
93+
return currentVolume / (float) maxVolume;
94+
}
8195
}

Assets/Plugins/VolumeButtonsPlugin/VolumeButtons.cs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,26 @@ public class VolumeButtons : MonoBehaviour
1414

1515
public VolumeButtonsEvent OnVolumeButtonEvent;
1616

17+
//[Range(0.0f, 1.0f)] // -1.0f in case of error or not supported
18+
public float SystemVolumeLevel
19+
{
20+
get
21+
{
22+
#if UNITY_ANDROID
23+
return _CallActivityFloat("getSystemVolumeLevel");
24+
#elif UNITY_IOS
25+
return _GetSystemVolumeLevel();
26+
#else
27+
Debug.LogFormat(UnsupportedError, Application.platform, string.Join("", SupportedPlatforms));
28+
return -1.0f;
29+
#endif
30+
}
31+
}
32+
1733
void Start()
1834
{
1935
#if UNITY_ANDROID
20-
_CallActivity("addGameObjectListener", gameObject.name);
36+
_CallActivityVoid("addGameObjectListener", gameObject.name);
2137
#elif UNITY_IOS
2238
_AddGameObjectListener(gameObject.name, gameObject.name.Length);
2339
#else
@@ -28,7 +44,7 @@ void Start()
2844
void OnDisable()
2945
{
3046
#if UNITY_ANDROID
31-
_CallActivity("removeGameObjectListener", gameObject.name);
47+
_CallActivityVoid("removeGameObjectListener", gameObject.name);
3248
#elif UNITY_IOS
3349
_RemoveGameObjectListener(gameObject.name, gameObject.name.Length);
3450
#else
@@ -40,7 +56,10 @@ private void _OnVolumeButtonEvent(string value)
4056
{
4157
if (OnVolumeButtonEvent != null)
4258
{
43-
this.OnVolumeButtonEvent.Invoke((VolumeButtonsEventType)Int32.Parse(value));
59+
#if DEBUG
60+
Debug.LogFormat("Volume event {0} delivered!", value);
61+
#endif
62+
this.OnVolumeButtonEvent.Invoke((VolumeButtonsEventType)Int32.Parse(value), this);
4463
}
4564
else
4665
{
@@ -50,25 +69,42 @@ private void _OnVolumeButtonEvent(string value)
5069

5170
#if UNITY_ANDROID
5271

53-
private void _CallActivity(string methodName, string arg0)
72+
private float _CallActivityFloat(string methodName)
5473
{
55-
AndroidJNIHelper.debug = true;
5674
using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
5775
{
5876
using (AndroidJavaObject activity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"))
5977
{
60-
Debug.LogFormat("Call {0}({1})", methodName, arg0);
61-
activity.Call(methodName, arg0);
78+
#if DEBUG
79+
Debug.LogFormat("Call {0}()", methodName);
80+
#endif
81+
return activity.Call<float>(methodName);
6282
}
6383
}
6484
}
6585

86+
private void _CallActivityVoid(string methodName, string args0)
87+
{
88+
using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
89+
{
90+
using (AndroidJavaObject activity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"))
91+
{
92+
#if DEBUG
93+
Debug.LogFormat("Call {0}({1})", methodName, args0);
94+
#endif
95+
activity.Call(methodName, args0);
96+
}
97+
}
98+
}
99+
66100
#elif UNITY_IOS
67101

68102
[DllImport("__Internal", EntryPoint="VBP_addGameObjectListener")]
69103
private static extern void _AddGameObjectListener([MarshalAs(UnmanagedType.LPWStr)]string gameObjectName, int gameObjectNameLen);
70104
[DllImport("__Internal", EntryPoint="VBP_removeGameObjectListener")]
71105
private static extern void _RemoveGameObjectListener([MarshalAs(UnmanagedType.LPWStr)]string gameObjectName, int gameObjectNameLen);
106+
[DllImport("__Internal", EntryPoint="VBP_getSystemVolumeLevel")]
107+
private static extern float _GetSystemVolumeLevel();
72108

73109
#endif
74110
}

Assets/Plugins/VolumeButtonsPlugin/VolumeButtonsEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
using UnityEngine.Events;
44

55
[Serializable]
6-
public class VolumeButtonsEvent : UnityEvent<VolumeButtonsEventType>
6+
public class VolumeButtonsEvent : UnityEvent<VolumeButtonsEventType, VolumeButtons>
77
{
88
}

Assets/Plugins/VolumeButtonsPlugin/iOS/VolumeButtonsPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
void VBP_addGameObjectListener(const char *go_name, int go_name_length);
44
void VBP_removeGameObjectListener(const char *go_name, int go_name_length);
5+
float VBP_getSystemVolumeLevel(void);

Assets/Plugins/VolumeButtonsPlugin/iOS/VolumeButtonsPlugin.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ -(void)hackSystemVolume:(float)volumeLevel {
9494

9595
@end
9696

97-
void VBP_addGameObjectListener(const char *go_name, int go_name_length)
98-
{
97+
void VBP_addGameObjectListener(const char *go_name, int go_name_length) {
9998
if (!observer) {
10099
observer = [_VBPAVAudioSessionObserver new];
101100
}
@@ -109,12 +108,15 @@ void VBP_addGameObjectListener(const char *go_name, int go_name_length)
109108
[observer addGameObject:gon];
110109
}
111110

112-
void VBP_removeGameObjectListener(const char *go_name, int go_name_length)
113-
{
111+
void VBP_removeGameObjectListener(const char *go_name, int go_name_length) {
114112
NSString* gon = [[NSString alloc]
115113
initWithBytes:go_name
116114
length:sizeof(__CHAR16_TYPE__) * go_name_length
117115
encoding:NSUTF16LittleEndianStringEncoding];
118116

119117
[observer removeGameObject:gon];
120118
}
119+
120+
float VBP_getSystemVolumeLevel(void) {
121+
return [[AVAudioSession sharedInstance] outputVolume];
122+
}

0 commit comments

Comments
 (0)