diff --git a/src/common/android.cpp b/src/common/android.cpp index 872ec2d42..638162647 100644 --- a/src/common/android.cpp +++ b/src/common/android.cpp @@ -225,6 +225,22 @@ bool createStorageDirectories() return true; } +bool hasBackgroundMusic() +{ + JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv(); + jobject activity = (jobject) SDL_AndroidGetActivity(); + + jclass clazz(env->GetObjectClass(activity)); + jmethodID method_id = env->GetMethodID(clazz, "hasBackgroundMusic", "()Z"); + + jboolean result = env->CallBooleanMethod(activity, method_id); + + env->DeleteLocalRef(activity); + env->DeleteLocalRef(clazz); + + return result; +} + } // android } // love diff --git a/src/common/android.h b/src/common/android.h index 1e4e62a39..7a7f33dd0 100644 --- a/src/common/android.h +++ b/src/common/android.h @@ -66,6 +66,8 @@ bool mkdir(const char *path); bool createStorageDirectories(); +bool hasBackgroundMusic(); + } // android } // love diff --git a/src/modules/audio/wrap_Audio.cpp b/src/modules/audio/wrap_Audio.cpp index e00b0eb4a..2869b3adc 100644 --- a/src/modules/audio/wrap_Audio.cpp +++ b/src/modules/audio/wrap_Audio.cpp @@ -307,16 +307,6 @@ int w_setMixMode(lua_State *) return 0; } -int w_getShouldBeSilenced(lua_State *L) -{ -#ifdef LOVE_IOS - lua_pushboolean(L, love::ios::audioShouldBeSilenced()); -#else - lua_pushboolean(L, 0); -#endif - return 1; -} - // List of functions to wrap. static const luaL_Reg functions[] = { @@ -343,7 +333,6 @@ static const luaL_Reg functions[] = { "setDistanceModel", w_setDistanceModel }, { "getDistanceModel", w_getDistanceModel }, { "setMixMode", w_setMixMode }, - { "getShouldBeSilenced", w_getShouldBeSilenced }, { 0, 0 } }; diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 5ad391188..5951449f6 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -184,6 +184,17 @@ void System::vibrate(double seconds) const #endif } +bool System::hasBackgroundMusic() const +{ +#if defined(LOVE_ANDROID) + return love::android::hasBackgroundMusic(); +#elif defined(LOVE_IOS) + return love::ios::audioShouldBeSilenced(); +#else + throw love::Exception("Unsupported platform."); +#endif +} + bool System::getConstant(const char *in, System::PowerState &out) { return powerStates.find(in, out); diff --git a/src/modules/system/System.h b/src/modules/system/System.h index 42bbc077f..5efa3abc9 100644 --- a/src/modules/system/System.h +++ b/src/modules/system/System.h @@ -106,6 +106,14 @@ public: */ virtual void vibrate(double seconds) const; + /** + * Gets if the user is playing music on background. + * Throws an exception on unsupported platforms. + * + * @return Whether a music is playing on background. + **/ + bool hasBackgroundMusic() const; + static bool getConstant(const char *in, PowerState &out); static bool getConstant(PowerState in, const char *&out); diff --git a/src/modules/system/wrap_System.cpp b/src/modules/system/wrap_System.cpp index a299583f8..fd2cee7aa 100644 --- a/src/modules/system/wrap_System.cpp +++ b/src/modules/system/wrap_System.cpp @@ -93,6 +93,19 @@ int w_vibrate(lua_State *L) return 0; } +int w_hasBackgroundMusic(lua_State *L) +{ + try + { + lua_pushboolean(L, instance()->hasBackgroundMusic()); + } + catch (love::Exception &) + { + lua_pushnil(L); + } + return 1; +} + static const luaL_Reg functions[] = { { "getOS", w_getOS }, @@ -102,6 +115,7 @@ static const luaL_Reg functions[] = { "getPowerInfo", w_getPowerInfo }, { "openURL", w_openURL }, { "vibrate", w_vibrate }, + { "hasBackgroundMusic", w_hasBackgroundMusic }, { 0, 0 } };