Skip to content

Commit

Permalink
Bind device notification listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
fgnm committed Dec 18, 2023
1 parent 0cd5a97 commit af3092b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix iOS native crash when file doesn't exists
- Workaround AAudio issues, limit this backend to Android >= 12
- Fix Android VFS native memory leaks
- Bind native Device Notification listeners (allow to detect interruptions or rerouting)

[0.3]
- Full MiniAudio engine customization
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package games.rednblack.miniaudio;

public interface MADeviceNotificationListener {
void onNotification(MADeviceNotificationType type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package games.rednblack.miniaudio;

public enum MADeviceNotificationType {
STARTED(0),
STOPPED(1),
REROUTED(2),
INTERRUPTION_BEGAN(3),
INTERRUPTION_ENDED(4),
UNLOCKED(5);

public final int code;

MADeviceNotificationType(int code) {
this.code = code;
}

public static MADeviceNotificationType decode (int code) {
switch (code) {
case 0:
return MADeviceNotificationType.STARTED;
case 1:
return MADeviceNotificationType.STOPPED;
case 2:
return MADeviceNotificationType.REROUTED;
case 3:
return MADeviceNotificationType.INTERRUPTION_BEGAN;
case 4:
return MADeviceNotificationType.INTERRUPTION_ENDED;
case 5:
return MADeviceNotificationType.UNLOCKED;
default:
return null;
}
}
}
27 changes: 26 additions & 1 deletion src/main/java/games/rednblack/miniaudio/MiniAudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin
static jobject jMiniAudio;
static jmethodID jon_native_sound_end;
static jmethodID jon_native_log;
static jmethodID jon_native_notification;
// Helper macro for platform-specific thread attachment
#ifdef MA_ANDROID
Expand Down Expand Up @@ -111,12 +112,19 @@ void ma_log_callback_jni(void* pUserData, ma_uint32 level, const char* pMessage)
env->DeleteLocalRef(javaMessage);
DETACH_ENV()
}
void notification_callback_jni(const ma_device_notification* pNotification) {
ATTACH_ENV()
env->CallVoidMethod(jMiniAudio, jon_native_notification, pNotification->type);
DETACH_ENV()
}
*/

private long engineAddress = 0;
private final MASound endCallbackSound;
private MASoundEndListener endListener;
private MALogCallback logCallback;
private MADeviceNotificationListener deviceNotificationListener;

/**
* Create a new MiniAudio Engine Instance
Expand Down Expand Up @@ -180,6 +188,7 @@ public void initEngine(int listenerCount, long playbackId, long captureId, int c
jclass handlerClass = env->GetObjectClass(jMiniAudio);
jon_native_sound_end = env->GetMethodID(handlerClass, "on_native_sound_end", "(J)V");
jon_native_log = env->GetMethodID(handlerClass, "on_native_log", "(ILjava/lang/String;)V");
jon_native_notification = env->GetMethodID(handlerClass, "on_native_notification", "(I)V");
ma_result res = ma_context_init(NULL, 0, NULL, &context);
if (res != MA_SUCCESS) return res;
Expand Down Expand Up @@ -357,6 +366,7 @@ public MADeviceInfo[] enumerateDevices() {
deviceConfig.playback.channels = channels;
deviceConfig.sampleRate = sampleRate;
deviceConfig.dataCallback = data_callback;
deviceConfig.notificationCallback = notification_callback_jni;
deviceConfig.performanceProfile = lowLatency ? ma_performance_profile_low_latency : ma_performance_profile_conservative;
deviceConfig.periodSizeInFrames = bufferPeriodFrames;
deviceConfig.periodSizeInMilliseconds = bufferPeriodMillis;
Expand Down Expand Up @@ -1955,7 +1965,7 @@ public void on_native_sound_end(long soundAddress) {
/**
* Set a custom log callback function. If null, default libGDX logging will be used.
*
* @param logCallback custom log callback
* @param logCallback {@link MALogCallback} custom log callback
*/
public void setLogCallback(MALogCallback logCallback) {
this.logCallback = logCallback;
Expand All @@ -1982,4 +1992,19 @@ public void on_native_log(int level, String message) {
break;
}
}

/**
* Set a listener to be notified when the device change its state, i.e. when an interruption begins.
*
* @param deviceNotificationListener {@link MADeviceNotificationListener}
*/
public void setDeviceNotificationListener(MADeviceNotificationListener deviceNotificationListener) {
this.deviceNotificationListener = deviceNotificationListener;
}

public void on_native_notification(int type) {
if (deviceNotificationListener != null) {
deviceNotificationListener.onNotification(MADeviceNotificationType.decode(type));
}
}
}
6 changes: 6 additions & 0 deletions testApp/src/main/java/games/rednblack/miniaudio/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public static void main(String[] args) {
public void create() {
//miniAudio = new MiniAudio(1, 1, 0, 256, 44100);
miniAudio = new MiniAudio(null, false);
miniAudio.setDeviceNotificationListener(new MADeviceNotificationListener() {
@Override
public void onNotification(MADeviceNotificationType type) {
System.out.println(type);
}
});
MADeviceInfo[] devices = miniAudio.enumerateDevices();
for (MADeviceInfo info : devices) {
System.out.println(info.isCapture + " " + info.idAddress + " . " + info.name);
Expand Down
Binary file modified testApp/src/main/resources/libgdx-miniaudio64.so
Binary file not shown.

0 comments on commit af3092b

Please sign in to comment.