android: Fixed audio device detection.

main
Ryan C. Gordon 2023-07-29 19:43:24 -04:00
parent 82ce05ad01
commit 1c074e8d97
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
3 changed files with 19 additions and 4 deletions

View File

@ -309,6 +309,16 @@ public class SDLAudioManager {
public static void registerAudioDeviceCallback() { public static void registerAudioDeviceCallback() {
if (Build.VERSION.SDK_INT >= 24 /* Android 7.0 (N) */) { if (Build.VERSION.SDK_INT >= 24 /* Android 7.0 (N) */) {
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
// get an initial list now, before hotplug callbacks fire.
for (AudioDeviceInfo dev : audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)) {
if (dev.getType() == AudioDeviceInfo.TYPE_TELEPHONY) {
continue; // Device cannot be opened
}
addAudioDevice(dev.isSink(), dev.getProductName().toString(), dev.getId());
}
for (AudioDeviceInfo dev : audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)) {
addAudioDevice(dev.isSink(), dev.getProductName().toString(), dev.getId());
}
audioManager.registerAudioDeviceCallback(mAudioDeviceCallback, null); audioManager.registerAudioDeviceCallback(mAudioDeviceCallback, null);
} }
} }

View File

@ -128,6 +128,7 @@ static void ANDROIDAUDIO_CloseDevice(SDL_AudioDevice *device)
static SDL_bool ANDROIDAUDIO_Init(SDL_AudioDriverImpl *impl) static SDL_bool ANDROIDAUDIO_Init(SDL_AudioDriverImpl *impl)
{ {
// !!! FIXME: if on Android API < 24, DetectDevices and Deinitialize should be NULL and OnlyHasDefaultOutputDevice and OnlyHasDefaultCaptureDevice should be SDL_TRUE, since audio device enum and hotplug appears to require Android 7.0+.
impl->ThreadInit = Android_AudioThreadInit; impl->ThreadInit = Android_AudioThreadInit;
impl->DetectDevices = Android_StartAudioHotplug; impl->DetectDevices = Android_StartAudioHotplug;
impl->Deinitialize = Android_StopAudioHotplug; impl->Deinitialize = Android_StopAudioHotplug;

View File

@ -1006,10 +1006,14 @@ JNIEXPORT void JNICALL
SDL_JAVA_AUDIO_INTERFACE(addAudioDevice)(JNIEnv *env, jclass jcls, jboolean is_capture, SDL_JAVA_AUDIO_INTERFACE(addAudioDevice)(JNIEnv *env, jclass jcls, jboolean is_capture,
jstring name, jint device_id) jstring name, jint device_id)
{ {
SDL_assert(SDL_GetCurrentAudioDriver() != NULL); // should have been started by Android_StartAudioHotplug inside an audio driver. if (SDL_GetCurrentAudioDriver() != NULL) {
const char *utf8name = (*env)->GetStringUTFChars(env, name, NULL); void *handle = (void *)((size_t)device_id);
SDL_AddAudioDevice(is_capture ? SDL_TRUE : SDL_FALSE, SDL_strdup(utf8name), NULL, (void *)((size_t)device_id)); if (!SDL_FindPhysicalAudioDeviceByHandle(handle)) {
(*env)->ReleaseStringUTFChars(env, name, utf8name); const char *utf8name = (*env)->GetStringUTFChars(env, name, NULL);
SDL_AddAudioDevice(is_capture ? SDL_TRUE : SDL_FALSE, SDL_strdup(utf8name), NULL, handle);
(*env)->ReleaseStringUTFChars(env, name, utf8name);
}
}
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL