From 324a290040d76c01e7a4d7db23b1ae4649b22b73 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 26 Dec 2018 11:36:23 -0400 Subject: [PATCH] The code related to t.audio.mixwithsystem=true is run before the audio module is loaded. Fixes issue #1430. --- src/common/ios.h | 2 +- src/common/ios.mm | 7 +++++-- src/modules/audio/Audio.cpp | 3 +-- src/modules/audio/Audio.h | 2 +- src/modules/audio/wrap_Audio.cpp | 2 +- src/modules/love/love.cpp | 23 +++++++++++++++++++++++ src/scripts/boot.lua | 8 ++++---- src/scripts/boot.lua.h | 13 +++++++------ 8 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/common/ios.h b/src/common/ios.h index 5ff272d5d..ddb9611dc 100644 --- a/src/common/ios.h +++ b/src/common/ios.h @@ -71,7 +71,7 @@ void vibrate(); /** * Enable mix mode (e.g. with background music apps) and playback with a muted device. **/ -void setAudioMixWithOthers(bool mixEnabled); +bool setAudioMixWithOthers(bool mixEnabled); /** * Returns whether another application is playing audio. diff --git a/src/common/ios.mm b/src/common/ios.mm index 337ba4788..cc9a620ba 100644 --- a/src/common/ios.mm +++ b/src/common/ios.mm @@ -347,7 +347,7 @@ void vibrate() } } -void setAudioMixWithOthers(bool mixEnabled) +bool setAudioMixWithOthers(bool mixEnabled) { @autoreleasepool { @@ -357,8 +357,11 @@ void setAudioMixWithOthers(bool mixEnabled) if (mixEnabled) category = AVAudioSessionCategoryAmbient; - if (![[AVAudioSession sharedInstance] setCategory:category error:&err]) + bool success = [[AVAudioSession sharedInstance] setCategory:category error:&err]; + if (!success) NSLog(@"Error in AVAudioSession setCategory: %@", [err localizedDescription]); + + return success; } } diff --git a/src/modules/audio/Audio.cpp b/src/modules/audio/Audio.cpp index 9092af8ec..3a4483f31 100644 --- a/src/modules/audio/Audio.cpp +++ b/src/modules/audio/Audio.cpp @@ -33,8 +33,7 @@ namespace audio bool Audio::setMixWithSystem(bool mix) { #ifdef LOVE_IOS - love::ios::setAudioMixWithOthers(mix); - return true; + return love::ios::setAudioMixWithOthers(mix); #else LOVE_UNUSED(mix); return false; diff --git a/src/modules/audio/Audio.h b/src/modules/audio/Audio.h index 2f3fe6e10..817975a98 100644 --- a/src/modules/audio/Audio.h +++ b/src/modules/audio/Audio.h @@ -261,7 +261,7 @@ public: * Sets whether audio from other apps mixes with love.audio or is muted, * on supported platforms. **/ - bool setMixWithSystem(bool mix); + static bool setMixWithSystem(bool mix); private: diff --git a/src/modules/audio/wrap_Audio.cpp b/src/modules/audio/wrap_Audio.cpp index 19423f6e1..539ecf2f6 100644 --- a/src/modules/audio/wrap_Audio.cpp +++ b/src/modules/audio/wrap_Audio.cpp @@ -526,7 +526,7 @@ int w_isEffectsSupported(lua_State *L) int w_setMixWithSystem(lua_State *L) { - luax_pushboolean(L, instance()->setMixWithSystem(luax_checkboolean(L, 1))); + luax_pushboolean(L, Audio::setMixWithSystem(luax_checkboolean(L, 1))); return 1; } diff --git a/src/modules/love/love.cpp b/src/modules/love/love.cpp index 89b5ac0a9..a7230225b 100644 --- a/src/modules/love/love.cpp +++ b/src/modules/love/love.cpp @@ -69,6 +69,11 @@ extern "C" # include "graphics/Graphics.h" #endif +// For love::audio::Audio::setMixWithSystem. +#ifdef LOVE_ENABLE_AUDIO +# include "audio/Audio.h" +#endif + // Scripts #include "scripts/nogame.lua.h" #include "scripts/boot.lua.h" @@ -305,6 +310,19 @@ static int w__setGammaCorrect(lua_State *L) return 0; } +static int w__setAudioMixWithSystem(lua_State *L) +{ + bool success = false; + +#ifdef LOVE_ENABLE_AUDIO + bool mix = love::luax_checkboolean(L, 1); + success = love::audio::Audio::setMixWithSystem(mix); +#endif + + love::luax_pushboolean(L, success); + return 1; +} + static int w_love_setDeprecationOutput(lua_State *L) { bool enable = love::luax_checkboolean(L, 1); @@ -362,6 +380,11 @@ int luaopen_love(lua_State *L) lua_pushcfunction(L, w__setGammaCorrect); lua_setfield(L, -2, "_setGammaCorrect"); + // Exposed here because we need to be able to call it before the audio + // module is initialized. + lua_pushcfunction(L, w__setAudioMixWithSystem); + lua_setfield(L, -2, "_setAudioMixWithSystem"); + lua_newtable(L); for (int i = 0; love::VERSION_COMPATIBILITY[i] != nullptr; i++) diff --git a/src/scripts/boot.lua b/src/scripts/boot.lua index 0debcfe78..39676e847 100644 --- a/src/scripts/boot.lua +++ b/src/scripts/boot.lua @@ -468,6 +468,10 @@ function love.init() love._setGammaCorrect(c.gammacorrect) end + if love._setAudioMixWithSystem and c.audio then + love._setAudioMixWithSystem(c.audio.mixwithsystem) + end + -- Gets desired modules. for k,v in ipairs{ "data", @@ -545,10 +549,6 @@ function love.init() end end - if love.audio then - love.audio.setMixWithSystem(c.audio.mixwithsystem) - end - -- Our first timestep, because window creation can take some time if love.timer then love.timer.step() diff --git a/src/scripts/boot.lua.h b/src/scripts/boot.lua.h index 60ae757b2..8bfa24685 100644 --- a/src/scripts/boot.lua.h +++ b/src/scripts/boot.lua.h @@ -867,6 +867,13 @@ const unsigned char boot_lua[] = 0x72, 0x72, 0x65, 0x63, 0x74, 0x28, 0x63, 0x2e, 0x67, 0x61, 0x6d, 0x6d, 0x61, 0x63, 0x6f, 0x72, 0x72, 0x65, 0x63, 0x74, 0x29, 0x0a, 0x09, 0x65, 0x6e, 0x64, 0x0a, + 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x5f, 0x73, 0x65, 0x74, 0x41, 0x75, 0x64, 0x69, 0x6f, + 0x4d, 0x69, 0x78, 0x57, 0x69, 0x74, 0x68, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x61, 0x6e, 0x64, 0x20, + 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, + 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x5f, 0x73, 0x65, 0x74, 0x41, 0x75, 0x64, 0x69, 0x6f, 0x4d, 0x69, + 0x78, 0x57, 0x69, 0x74, 0x68, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x28, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, + 0x6f, 0x2e, 0x6d, 0x69, 0x78, 0x77, 0x69, 0x74, 0x68, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x29, 0x0a, + 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x2d, 0x2d, 0x20, 0x47, 0x65, 0x74, 0x73, 0x20, 0x64, 0x65, 0x73, 0x69, 0x72, 0x65, 0x64, 0x20, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x2e, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x6b, 0x2c, 0x76, 0x20, 0x69, 0x6e, 0x20, 0x69, 0x70, 0x61, 0x69, 0x72, 0x73, @@ -1002,12 +1009,6 @@ const unsigned char boot_lua[] = 0x77, 0x2e, 0x69, 0x63, 0x6f, 0x6e, 0x29, 0x29, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x65, 0x6e, 0x64, 0x0a, - 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x74, 0x68, 0x65, - 0x6e, 0x0a, - 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x73, 0x65, 0x74, 0x4d, 0x69, - 0x78, 0x57, 0x69, 0x74, 0x68, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x28, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, - 0x6f, 0x2e, 0x6d, 0x69, 0x78, 0x77, 0x69, 0x74, 0x68, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x29, 0x0a, - 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x2d, 0x2d, 0x20, 0x4f, 0x75, 0x72, 0x20, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x65, 0x70, 0x2c, 0x20, 0x62, 0x65, 0x63, 0x61, 0x75, 0x73, 0x65, 0x20, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x20, 0x63, 0x72, 0x65, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x63, 0x61, 0x6e, 0x20, 0x74, 0x61,