Skip to content

Commit

Permalink
Fix music to stop playing when unfocusing on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
Drauthius committed Nov 30, 2019
1 parent b801c27 commit 5019ecc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/game/gui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ function GUI:back()
end
end

function GUI:showMenu()
if self.infoPanel:isShown() then
self:_closeInfoPanel()
end
GameState.push(InGameMenu)
end

function GUI:update(dt)
if self.menuButton:isPressed() then
local x, y = screen:getCoordinate(love.mouse.getPosition())
Expand Down
15 changes: 12 additions & 3 deletions src/game/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,17 @@ function Game:leave()
self:_save()
Timer.clear()
self.engine.systemRegistry["SoundSystem"]:stopAll()
soundManager:stopAll()
end

function Game:pause()
self.engine.systemRegistry["SoundSystem"]:pauseAll()
soundManager:pauseAll()
end

function Game:resume()
self.engine.systemRegistry["SoundSystem"]:playAll()
soundManager:resumeAll()
end

function Game:quit()
Expand Down Expand Up @@ -598,9 +601,15 @@ end

function Game:focus(focused)
local os = love.system.getOS()
-- Focus is lost on mobile when switching out of the app, so best to save now.
if not focused and (os == "Android" or os == "iOS") then
self.saveGameTimer:update(self.saveGameHandle.limit - self.saveGameHandle.time)
if os == "Android" or os == "iOS" then
if focused then
soundManager:playMusic()
else
-- Focus is lost on mobile when switching out of the app, so best to save now.
self.saveGameTimer:update(self.saveGameHandle.limit - self.saveGameHandle.time)
self.gui:showMenu()
soundManager:pauseMusic()
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions src/ingamemenu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ function InGameMenu:leave()
soundManager:playEffect("main_menu_closed")
end

function InGameMenu:focus(f)
self.oldState:focus(f)
end

function InGameMenu:update(dt)
for _,button in ipairs(self.buttons) do
if button:isPressed() then
Expand Down
3 changes: 3 additions & 0 deletions src/mainmenu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local Button = require "src.game.gui.button"
local ScaledSprite = require "src.game.scaledsprite"

local screen = require "src.screen"
local soundManager = require "src.soundmanager"
local spriteSheet = require "src.game.spritesheet"

local MainMenu = {}
Expand Down Expand Up @@ -115,6 +116,8 @@ end
function MainMenu:enter(previous, init)
self:reevaluate()

soundManager:playMusic()

if init and self.latest then
return GameState.switch(Game, self.latest)
end
Expand Down
33 changes: 32 additions & 1 deletion src/soundmanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ function SoundManager:initialize()

self.music = love.audio.newSource("asset/sfx/"..SoundManager.MUSIC, "stream")
self.music:setLooping(true)
self.music:play()

self:setEffectVolume(self:getEffectVolume())
self:setMusicVolume(self:getMusicVolume())
Expand Down Expand Up @@ -160,6 +159,38 @@ function SoundManager:playEffect(effect, gi, gj)
source:play()
end

function SoundManager:playMusic()
self.music:play()
end

function SoundManager:pauseMusic()
self.music:pause()
end

function SoundManager:pauseAll()
for _,source in ipairs(self.sources) do
if source:isPlaying() then
source:pause()
end
end
end

function SoundManager:resumeAll()
for _,source in ipairs(self.sources) do
if source:isPlaying() then
source:play()
end
end
end

function SoundManager:stopAll()
for _,source in ipairs(self.sources) do
if source:isPlaying() then
source:stop()
end
end
end

function SoundManager:_getFreeSource()
for _,source in ipairs(self.sources) do
if source:getFreeBufferCount() > 0 then
Expand Down

0 comments on commit 5019ecc

Please sign in to comment.