From d34ab921d4fa72c82dad4694e5e57785ee9eeb5a Mon Sep 17 00:00:00 2001 From: fwcd Date: Fri, 2 Feb 2024 16:57:36 +0100 Subject: [PATCH] SoundManager: Set up AVAudioSession on iOS --- CMakeLists.txt | 1 + src/soundio/soundmanager.cpp | 8 ++++++++ src/soundio/soundmanagerios.h | 7 +++++++ src/soundio/soundmanagerios.mm | 31 +++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 src/soundio/soundmanagerios.h create mode 100644 src/soundio/soundmanagerios.mm diff --git a/CMakeLists.txt b/CMakeLists.txt index 834ece63a4c..9d7e2071af5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1544,6 +1544,7 @@ if(APPLE) if(IOS) target_sources(mixxx-lib PRIVATE + src/soundio/soundmanagerios.mm src/util/screensaverios.mm ) else() diff --git a/src/soundio/soundmanager.cpp b/src/soundio/soundmanager.cpp index f03558bd6a3..503dc05802b 100644 --- a/src/soundio/soundmanager.cpp +++ b/src/soundio/soundmanager.cpp @@ -4,6 +4,7 @@ #include #include +#include #include // for memcpy and strcmp #include "control/controlobject.h" @@ -22,6 +23,10 @@ #include "util/versionstore.h" #include "vinylcontrol/defs_vinylcontrol.h" +#ifdef Q_OS_IOS +#include "soundio/soundmanagerios.h" +#endif + typedef PaError (*SetJackClientName)(const char *name); namespace { @@ -249,6 +254,9 @@ void SoundManager::queryDevicesPortaudio() { if (!m_paInitialized) { #ifdef Q_OS_LINUX setJACKName(); +#endif +#ifdef Q_OS_IOS + mixxx::initializeAVAudioSession(); #endif err = Pa_Initialize(); m_paInitialized = true; diff --git a/src/soundio/soundmanagerios.h b/src/soundio/soundmanagerios.h new file mode 100644 index 00000000000..4ca6498d4da --- /dev/null +++ b/src/soundio/soundmanagerios.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mixxx { + +void initializeAVAudioSession(); + +}; // namespace mixxx diff --git a/src/soundio/soundmanagerios.mm b/src/soundio/soundmanagerios.mm new file mode 100644 index 00000000000..b00f0284476 --- /dev/null +++ b/src/soundio/soundmanagerios.mm @@ -0,0 +1,31 @@ +#include +#include + +#import + +namespace mixxx { + +void initializeAVAudioSession() { + AVAudioSession* session = AVAudioSession.sharedInstance; + AVAudioSessionCategory category = AVAudioSessionCategoryPlayback; + AVAudioSessionMode mode = AVAudioSessionModeDefault; + AVAudioSessionCategoryOptions options = + AVAudioSessionCategoryOptionMixWithOthers | + AVAudioSessionCategoryOptionAllowAirPlay | + AVAudioSessionCategoryOptionAllowBluetoothA2DP; + + NSError* error = nil; + [session setCategory:category mode:mode options:options error:&error]; + if (error != nil) { + qWarning() << "Could not initialize AVAudioSession:" + << error.localizedDescription; + } + + [session setActive:true error:&error]; + if (error != nil) { + qWarning() << "Could not activate AVAudioSession:" + << error.localizedDescription; + } +} + +}; // namespace mixxx