SDLAudioManager.java: Fixed the support for Android older than API 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java
index b6c7b2c..2a74fb0 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java
@@ -20,21 +20,29 @@ public class SDLAudioManager {
protected static AudioRecord mAudioRecord;
protected static Context mContext;
- private static final AudioDeviceCallback mAudioDeviceCallback = new AudioDeviceCallback() {
- @Override
- public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
- Arrays.stream(addedDevices).forEach(deviceInfo -> addAudioDevice(deviceInfo.isSource(), deviceInfo.getId()));
- }
+ private static final int[] NO_DEVICES = {};
- @Override
- public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
- Arrays.stream(removedDevices).forEach(deviceInfo -> removeAudioDevice(deviceInfo.isSource(), deviceInfo.getId()));
- }
- };
+ private static AudioDeviceCallback mAudioDeviceCallback;
public static void initialize() {
mAudioTrack = null;
mAudioRecord = null;
+ mAudioDeviceCallback = null;
+
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+ {
+ mAudioDeviceCallback = new AudioDeviceCallback() {
+ @Override
+ public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
+ Arrays.stream(addedDevices).forEach(deviceInfo -> addAudioDevice(deviceInfo.isSink(), deviceInfo.getId()));
+ }
+
+ @Override
+ public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
+ Arrays.stream(removedDevices).forEach(deviceInfo -> removeAudioDevice(deviceInfo.isSink(), deviceInfo.getId()));
+ }
+ };
+ }
}
public static void setContext(Context context) {
@@ -229,7 +237,7 @@ public class SDLAudioManager {
return null;
}
- if (deviceId != 0) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && deviceId != 0) {
mAudioRecord.setPreferredDevice(getOutputAudioDeviceInfo(deviceId));
}
@@ -256,7 +264,7 @@ public class SDLAudioManager {
return null;
}
- if (deviceId != 0) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && deviceId != 0) {
mAudioTrack.setPreferredDevice(getInputAudioDeviceInfo(deviceId));
}
@@ -275,45 +283,65 @@ public class SDLAudioManager {
}
private static AudioDeviceInfo getInputAudioDeviceInfo(int deviceId) {
- AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
- return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS))
- .filter(deviceInfo -> deviceInfo.getId() == deviceId)
- .findFirst()
- .orElse(null);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS))
+ .filter(deviceInfo -> deviceInfo.getId() == deviceId)
+ .findFirst()
+ .orElse(null);
+ } else {
+ return null;
+ }
}
private static AudioDeviceInfo getOutputAudioDeviceInfo(int deviceId) {
- AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
- return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS))
- .filter(deviceInfo -> deviceInfo.getId() == deviceId)
- .findFirst()
- .orElse(null);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS))
+ .filter(deviceInfo -> deviceInfo.getId() == deviceId)
+ .findFirst()
+ .orElse(null);
+ } else {
+ return null;
+ }
}
private static void registerAudioDeviceCallback() {
- AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
- audioManager.registerAudioDeviceCallback(mAudioDeviceCallback, null);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ audioManager.registerAudioDeviceCallback(mAudioDeviceCallback, null);
+ }
}
private static void unregisterAudioDeviceCallback(Context context) {
- AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
- audioManager.unregisterAudioDeviceCallback(mAudioDeviceCallback);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+ audioManager.unregisterAudioDeviceCallback(mAudioDeviceCallback);
+ }
}
/**
* This method is called by SDL using JNI.
*/
public static int[] getAudioOutputDevices() {
- AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
- return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)).mapToInt(AudioDeviceInfo::getId).toArray();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)).mapToInt(AudioDeviceInfo::getId).toArray();
+ } else {
+ return NO_DEVICES;
+ }
}
/**
* This method is called by SDL using JNI.
*/
public static int[] getAudioInputDevices() {
- AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
- return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).mapToInt(AudioDeviceInfo::getId).toArray();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).mapToInt(AudioDeviceInfo::getId).toArray();
+ } else {
+ return NO_DEVICES;
+ }
}
/**
@@ -332,6 +360,11 @@ public class SDLAudioManager {
return;
}
+ if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
+ Log.e(TAG, "Attempted to make an incompatible audio call with uninitialized audio! (floating-point output is supported since Android 5.0 Lollipop)");
+ return;
+ }
+
for (int i = 0; i < buffer.length;) {
int result = mAudioTrack.write(buffer, i, buffer.length - i, AudioTrack.WRITE_BLOCKING);
if (result > 0) {
@@ -410,7 +443,11 @@ public class SDLAudioManager {
/** This method is called by SDL using JNI. */
public static int captureReadFloatBuffer(float[] buffer, boolean blocking) {
- return mAudioRecord.read(buffer, 0, buffer.length, blocking ? AudioRecord.READ_BLOCKING : AudioRecord.READ_NON_BLOCKING);
+ if (Build.VERSION.SDK_INT < 23) {
+ return 0;
+ } else {
+ return mAudioRecord.read(buffer, 0, buffer.length, blocking ? AudioRecord.READ_BLOCKING : AudioRecord.READ_NON_BLOCKING);
+ }
}
/** This method is called by SDL using JNI. */