Audio issues on Android devices

Not sure what my problem is here but I’ve had a LOT of Android users complain that the audio is not working correctly in my app - specifically using the hardware buttons to control the audio doesn’t seem to work.

I’m using the audio.load / .play functions and they seem to work correctly on iOS, I’m also setting the min and max volumes like so

 audio.setMinVolume(0); audio.setMaxVolume(1); for i=1,32 do audio.setVolume(1,{channel = i}) end; audio.setVolume( 1 )

at the start of my app.

I added the following code to detect / respond to the hardware buttons (and show a volume slider on the screen)

local volumeUp, volumeDown = "volumeUp", "volumeDown" if \_TARGET\_ENVIRONMENT == "SIMULATOR" then volumeUp, volumeDown = "up","down" end local volumeGroup, volumeButton, volumeFade = nil,nil, false local adjustmentDelta, lastPhase, keyRepeater local function showVolumeGroup(volume) local width = \_W \* 0.75 local left = \_CX - (width / 2) if volumeGroup == nil then volumeGroup = display.newGroup() local bar = display.newRoundedRect(volumeGroup, \_CX, \_CY, width, 8, 2) bar.strokeWidth = 2 bar:setFillColor(0.98,0.92,0.84) bar:setStrokeColor(0) volumeButton = wtf.image({parent=volumeGroup, asset="volume\_button",xScale=1.25,yScale=1.25}) -- ,tint={0.25,1,0.25}}) end volumeButton.x = left + (volume \* width) if volumeFade then transition.cancel(volumeGroup) end volumeGroup.alpha = 1; volumeFade = true transition.to(volumeGroup,{alpha=0,time=250,delay=500,onComplete=function() volumeFade=false end}) end local function adjustvolume(delta) local masterVolume = audio.getVolume(); -- print( "volume:", masterVolume ) masterVolume = masterVolume + delta if masterVolume \< 0 then masterVolume = 0 elseif masterVolume \> 1 then masterVolume = 1 end audio.setVolume(masterVolume) showVolumeGroup(masterVolume) audio.play(NOTE\_SAMPLES.C4) timer.performWithDelay(200,function() if keyRepeater then adjustvolume(delta) end end) end function ON\_KEY\_EVENT( event ) local phase = event.phase local keyName = event.keyName -- ------------------------------------------------------- -- Have this work an on screen volume slider... if phase == "up" then adjustmentDelta, lastPhase, keyRepeater = 0,phase,false elseif phase == "down" then if lastPhase ~= "down" then if keyName == volumeUp then adjustmentDelta, lastPhase, keyRepeater = 0.05,phase,true elseif keyName == volumeDown then adjustmentDelta, lastPhase, keyRepeater = -0.05,phase,true end if keyRepeater then adjustvolume(adjustmentDelta) end end end return true end

Whilst this DOES respond to key events (and the slider works) it sounds like the audio range goes from silent to the currently set system master volume - if I set the master volume to full outside of my app then the volume works as expected, but if I set the system master volume to 50% then that’s the max range inside the app.

I’ve seen other Android / Kindle Corona apps that appear to work correctly and actually set the device master volume but for the life of me I can’t work out how they do it.

On a related note ( I know this is a daft question but…) I get a fair number of users on iOS who complain that they can’t hear any audio and it turns out that the mute button on the side of the device is turned on - again I’m sure I’ve seen other apps that ignore this setting and can play audio regardless.

Any help would be gratefully appreciated.

Hi @techdojo,

I didn’t deepy inspect your code, but are you trying to exactly “sync” your app’s volume level with the device’s hardware-level volume level? According to the docs for “audio.setVolume()”, “the master volume is not necessarily the device’s ringer volume. However, all volumes are scaled proportionally to the ringer volume.” (ringer volume meaning the device’s hardware-level volume).

Next, on the topic of you considering “ignoring” the iOS mute switch, I’ve expressed my opinion on this recently: if any app… Corona-built or otherwise… tried to override my mute switch and blast music at me during a “respect quiet time” (meeting, on a plane, etc.) I would instantly give it a scorching 1-star review… and I think many other users would as well. The mute switch, like it or not, exists for a reason and it should not be overridden. Now, if you simply want to detect if that mute switch is on, and display a pop-up or dialog to the user, then Scott Harrison’s More Info plugin can help you.

https://marketplace.coronalabs.com/plugin/more-info

Best regards,

Brent

Hey Brent thanks for the response, you’re spot on I’m trying to sync my app audio with the devices hardware level and then control the device level via the hardware buttons. For what it’s worth I 100% agree about honouring the mute switch, but you’d be amazed at the number of customer support queries we get that are resolved by telling people to check the mute switch, I wasn’t aware of Scott’s more info plugin - but I’ll definitely be adding it in asap.

Hmmm… I haven’t worked with key events recently, but you’re not actually able to read the specific hardware-side device’s volume level as a readable Corona value, are you? Perhaps you can?

In my app I do not track key events for volume yet my app responds correctly to Android volume up and down.

I set my sounds to be between 0.35 and 0.7 on different channels and Corona seems to make that relative to the devices volume level.

These have proven to be relative to the devices “play music” volume level. Tested on Android 5-7.

Here is my code (if it helps at all)

local function setVolumeLevels() --music audio.setVolume( 0.5, { channel = 1 } ) audio.setMaxVolume( 0.5, { channel = 1 } ) --background sounds audio.setVolume( 0.5, { channel = 2 } ) audio.setMaxVolume( 0.5, { channel = 2 } ) audio.setVolume( 0.5, { channel = 3 } ) audio.setMaxVolume( 0.5, { channel = 3 } ) --rain audio.setVolume( 0.3, { channel = 4 } ) audio.setMaxVolume( 0.3, { channel = 4 } ) --mini game audio.setVolume( 0.35, { channel = 5 } ) audio.setMaxVolume( 0.35, { channel = 5 } ) --sound effects for i = 6, audio.totalChannels do audio.setVolume( 0.75, { channel = i } ) audio.setMaxVolume( 0.75, { channel = i } ) end end

Thanks for the responses, I wonder if anyone has developed a plugin that has solved this issue?
Actually looks like Scott Harrison has got one specifically for audio that I’m going to try.

From what I can tell - if you use the media.* library functions then you CAN control the volume using the hardware buttons, but I’m not having any success using the audio.* library functions.

I need to use the audio.* functions as I need to play more than one sample at once.

I’m also having problems getting Scott’s volume control plugin to work as well :frowning:

@techdojo,

Did you contact Scott directly? Usually he responds almost instantly to any concerns or questions related to his plugins…

I did, I’m just waiting to hear back from him.

techdojo, did you ever hear back on this? Is the volume control plugin working for you?

Yeah, Scott got in touch with me - there were a couple of issues that needed ironing out but it works perfectly now! :slight_smile:

Nice, I just bought it and it works like a charm for me too!

Hi @techdojo,

I didn’t deepy inspect your code, but are you trying to exactly “sync” your app’s volume level with the device’s hardware-level volume level? According to the docs for “audio.setVolume()”, “the master volume is not necessarily the device’s ringer volume. However, all volumes are scaled proportionally to the ringer volume.” (ringer volume meaning the device’s hardware-level volume).

Next, on the topic of you considering “ignoring” the iOS mute switch, I’ve expressed my opinion on this recently: if any app… Corona-built or otherwise… tried to override my mute switch and blast music at me during a “respect quiet time” (meeting, on a plane, etc.) I would instantly give it a scorching 1-star review… and I think many other users would as well. The mute switch, like it or not, exists for a reason and it should not be overridden. Now, if you simply want to detect if that mute switch is on, and display a pop-up or dialog to the user, then Scott Harrison’s More Info plugin can help you.

https://marketplace.coronalabs.com/plugin/more-info

Best regards,

Brent

Hey Brent thanks for the response, you’re spot on I’m trying to sync my app audio with the devices hardware level and then control the device level via the hardware buttons. For what it’s worth I 100% agree about honouring the mute switch, but you’d be amazed at the number of customer support queries we get that are resolved by telling people to check the mute switch, I wasn’t aware of Scott’s more info plugin - but I’ll definitely be adding it in asap.

Hmmm… I haven’t worked with key events recently, but you’re not actually able to read the specific hardware-side device’s volume level as a readable Corona value, are you? Perhaps you can?

In my app I do not track key events for volume yet my app responds correctly to Android volume up and down.

I set my sounds to be between 0.35 and 0.7 on different channels and Corona seems to make that relative to the devices volume level.

These have proven to be relative to the devices “play music” volume level. Tested on Android 5-7.

Here is my code (if it helps at all)

local function setVolumeLevels() --music audio.setVolume( 0.5, { channel = 1 } ) audio.setMaxVolume( 0.5, { channel = 1 } ) --background sounds audio.setVolume( 0.5, { channel = 2 } ) audio.setMaxVolume( 0.5, { channel = 2 } ) audio.setVolume( 0.5, { channel = 3 } ) audio.setMaxVolume( 0.5, { channel = 3 } ) --rain audio.setVolume( 0.3, { channel = 4 } ) audio.setMaxVolume( 0.3, { channel = 4 } ) --mini game audio.setVolume( 0.35, { channel = 5 } ) audio.setMaxVolume( 0.35, { channel = 5 } ) --sound effects for i = 6, audio.totalChannels do audio.setVolume( 0.75, { channel = i } ) audio.setMaxVolume( 0.75, { channel = i } ) end end

Thanks for the responses, I wonder if anyone has developed a plugin that has solved this issue?
Actually looks like Scott Harrison has got one specifically for audio that I’m going to try.

From what I can tell - if you use the media.* library functions then you CAN control the volume using the hardware buttons, but I’m not having any success using the audio.* library functions.

I need to use the audio.* functions as I need to play more than one sample at once.

I’m also having problems getting Scott’s volume control plugin to work as well :frowning:

@techdojo,

Did you contact Scott directly? Usually he responds almost instantly to any concerns or questions related to his plugins…

I did, I’m just waiting to hear back from him.