diff --git a/es-app/src/guis/GuiMenu.cpp b/es-app/src/guis/GuiMenu.cpp index abf48674f..923eb8a06 100644 --- a/es-app/src/guis/GuiMenu.cpp +++ b/es-app/src/guis/GuiMenu.cpp @@ -1176,6 +1176,26 @@ void GuiMenu::openSoundOptions() { auto s = new GuiSettings(_("SOUND SETTINGS")); +#if defined(__ANDROID__) + // Audio driver. + auto audioDriver = std::make_shared>(getHelpStyle(), + _("AUDIO DRIVER"), false); + std::string selectedDriver {Settings::getInstance()->getString("AudioDriver")}; + audioDriver->add("OPENSL ES", "openslES", selectedDriver == "openslES"); + audioDriver->add("AAUDIO", "AAudio", selectedDriver == "AAudio"); + // If there are no objects returned, then there must be a manually modified entry in the + // configuration file. Simply set the audio driver to "openslES" in this case. + if (audioDriver->getSelectedObjects().size() == 0) + audioDriver->selectEntry(0); + s->addWithLabel(_("AUDIO DRIVER (REQUIRES RESTART)"), audioDriver); + s->addSaveFunc([audioDriver, s] { + if (audioDriver->getSelected() != Settings::getInstance()->getString("AudioDriver")) { + Settings::getInstance()->setString("AudioDriver", audioDriver->getSelected()); + s->setNeedsSaving(); + } + }); +#endif + // TODO: Implement system volume support for macOS and Android. #if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(__FreeBSD__) && !defined(__HAIKU__) // System volume. diff --git a/es-app/src/main.cpp b/es-app/src/main.cpp index 4b2e59fcb..7c91a7cf1 100644 --- a/es-app/src/main.cpp +++ b/es-app/src/main.cpp @@ -1027,6 +1027,15 @@ int main(int argc, char* argv[]) Utils::Platform::Android::setupFontFiles(); Utils::Platform::Android::setupLocalizationFiles(); } + + { + std::string audioDriver {Settings::getInstance()->getString("AudioDriver")}; + if (audioDriver != "openslES" && audioDriver != "AAudio") + audioDriver = "openslES"; + + setenv("SDL_AUDIODRIVER", audioDriver.c_str(), 1); + } + #endif Utils::Localization::setLocale(); diff --git a/es-core/src/AudioManager.cpp b/es-core/src/AudioManager.cpp index e3f38e1b0..3ac130208 100644 --- a/es-core/src/AudioManager.cpp +++ b/es-core/src/AudioManager.cpp @@ -36,10 +36,28 @@ void AudioManager::init() { LOG(LogInfo) << "Setting up AudioManager..."; +#if defined(__ANDROID__) if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) { - LOG(LogError) << "Error initializing SDL audio!\n" << SDL_GetError(); + if (Settings::getInstance()->getString("AudioDriver") != "AAudio") { + LOG(LogWarning) << "Requested OpenSL ES audio driver does not seem to be available, " + "reverting to AAudio"; + setenv("SDL_AUDIODRIVER", "AAudio", 1); + if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) { + LOG(LogError) << "Couldn't initialize SDL audio: " << SDL_GetError(); + return; + } + } + else { + LOG(LogError) << "Couldn't initialize SDL audio: " << SDL_GetError(); + return; + } + } +#else + if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) { + LOG(LogError) << "Couldn't initialize SDL audio: " << SDL_GetError(); return; } +#endif LOG(LogInfo) << "Audio driver: " << SDL_GetCurrentAudioDriver(); diff --git a/es-core/src/Settings.cpp b/es-core/src/Settings.cpp index 9fcf6e284..ced874ceb 100644 --- a/es-core/src/Settings.cpp +++ b/es-core/src/Settings.cpp @@ -233,6 +233,9 @@ void Settings::setDefaults() mBoolMap["ShowHelpPrompts"] = {true, true}; // Sound settings. +#if defined(__ANDROID__) + mStringMap["AudioDriver"] = {"openslES", "openslES"}; +#endif mIntMap["SoundVolumeNavigation"] = {70, 70}; mIntMap["SoundVolumeVideos"] = {80, 80}; mBoolMap["ViewsVideoAudio"] = {true, true};