Jerry Adkins
Jerry Adkins

Reputation: 148

Corona sdk allow device volume control

I am using a runtime listener in a Corona sdk game, and it works, however what I am really interested in is having my game not interfere with the user setting the volume on their droid phone or tablet by using the hardware volume control on their device. I couldn't find anything on the Corona Labs web site. Thanks for any advice.

Upvotes: 1

Views: 1463

Answers (2)

speeder
speeder

Reputation: 6296

This happened to me once, it was because I used the event listener "key"

To fix that, you need to return FALSE except for the key you are handling, when a "key" listener returns true, it means: "I did something with that key, so the OS should ignore it" and when it returns false, it means: "I did nothing with that key, so the OS should handle it"

So, why you cannot set the volume? It is because you are trapping the "key" event somewhere, and NOT returning false when the key pressed was the volume one (the easiest way to go is return "true" for what you want, and "false" for everything else).

When I had this problem, I had this code:

local downPress = false
function onBackButtonPressed(e)
    if (e.phase == "down" and e.keyName == "back") then
        downPress = true
    else
        if (e.phase == "up" and e.keyName == "back" and downPress) then
            storyboard.gotoScene( LastScene , "fade", 200 );
            Runtime:removeEventListener( "key", onBackButtonPressed );
        end
    end
end

It worked fine for what I wanted, but blocked the volume key. Notice there are no "return" statement on it.

The code now is this:

local downPress = false
function onBackButtonPressed(e)
    if (e.phase == "down" and e.keyName == "back") then
        downPress = true
        return true
    else
        if (e.phase == "up" and e.keyName == "back" and downPress) then
            storyboard.gotoScene( LastScene , "fade", 200 );
            Runtime:removeEventListener( "key", onBackButtonPressed );
            return true
        end
    end
    return false; --THE LINE YOU REALLY NEED IS THIS ONE!!!
end

So what I do is return true only when the back key is pressed and depressed (my intention was prevent the app from quitting when the back key is pressed, probably this is what you wanted too) and return false for everything else (volume keys included on that!)

Upvotes: 3

Luciano
Luciano

Reputation: 141

If you have a runtime listener like this (for going back with the back hard button in android):

Runtime:addEventListener( "key", handleBackButton )

Remember to add "return true" at the end of the listener.

local function handleBackButton( event)
    if (event.phase == "down") and (event.keyName =="back") then
        -- your code for going back
        return true
    end
end

And remember that the runtime listeners are global, you just need to register the listener once, and also remember to remove the listener when is not used anymore.

Hope this helps. cheers!

Upvotes: 0

Related Questions