Android: audio.getVolume() returns -1 after native.requestExit()

In my Android app I have created onKeyEvent() to “back”, “volumeUp” and “volumeDown” keys.

if (keyname=="back" and event.phase == "up") then native.requestExit() end

if ( keyname == "volumeUp" and event.phase == "down" ) then local masterVolume = audio.getVolume( { channel=1 } ) --here after app suspend masterVolume is equal to -1 if ( masterVolume \< 1.0 ) then masterVolume = masterVolume + 0.1 if audio.setVolume( masterVolume, { channel=1 } ) then end end return true elseif ( keyname == "volumeDown" and event.phase == "down" ) then local masterVolume = audio.getVolume( { channel=1 } ) if ( masterVolume \> 0.0 ) then masterVolume = masterVolume - 0.1 if audio.setVolume( masterVolume, { channel=1 } ) then end end return true end

Everything works. But if I press “back” key app suspends (but still in memory). In this case, if I run app again audio.getVolume() returns -1

Hi @any,

I’m a bit confused why you’re getting and setting channel #1 in your volume keys functions. Wouldn’t it be best to get and set the master volume in all cases?

Brent

Thank you, @Brent! I have edited my code to control master volume but audio.getVolume() still returns -1 after application suspended and ran again ((

native.requestExit() actually exits the app not suspends it.

Hi @any,

I’m a bit confused why you’re getting and setting channel #1 in your volume keys functions. Wouldn’t it be best to get and set the master volume in all cases?

Brent

Thank you, @Brent! I have edited my code to control master volume but audio.getVolume() still returns -1 after application suspended and ran again ((

native.requestExit() actually exits the app not suspends it.