audio wont play on android

hi
i have a problem, im not very good at lua/corona yet

i have made an app to android where you can tap 5 buttons and it will play a short sound

it works in the simulator but now when i upload to my android htc phone it wont work, and its driving me nuts
please help.

i have placed the main.lua code here, could not attach it
please



– main.lua


– Your code here

local background = display.newImage(“clouds.png”)

– text til forside
local textObject = display.newText( “Tap a box”, 82, 50, nil, 30 ) textObject:setTextColor( 255,255,255 )

local button = display.newImage( “button.png” )
button.x = display.contentWidth / 4
button.y = display.contentHeight - 50

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
end

button:addEventListener( “tap”, button )

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
media.playEventSound( “sound1.mp3” )
end


–lyd/knap 2–

local button = display.newImage( “button2.png” )
button.x = display.contentWidth / 1.40
button.y = display.contentHeight - 50

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
end

button:addEventListener( “tap”, button2 )

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
media.playEventSound( “sound2.wav” )
end


–lyd/knap 3–

local button = display.newImage( “button3.png” )
button.x = display.contentWidth / 1.40
button.y = display.contentHeight - 250

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
end

button:addEventListener( “tap”, button3 )

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
media.playEventSound( “sound3.wav” )
end


–lyd/knap 4–

local button = display.newImage( “button4.png” )
button.x = display.contentWidth / 4
button.y = display.contentHeight - 250

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
end

button:addEventListener( “tap”, button4 )

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
media.playEventSound( “sound4.wav” )
end


–lyd/knap 5–

local button = display.newImage( “button4.png” )
button.x = display.contentWidth / 2.09
button.y = display.contentHeight - 150

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
end

button:addEventListener( “tap”, button5 )

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
media.playEventSound( “sound5.mp3” )
end


–lyd/knap 6–

local button = display.newImage( “button6.png” )
button.x = display.contentWidth / 2.09
button.y = display.contentHeight - 350

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
end

button:addEventListener( “tap”, button6 )

function button:tap( event )
local r = math.random( 0, 255 )
local g = math.random( 0, 255 )
local b = math.random( 0, 255 )

textObject:setTextColor( r, g, b )
media.playEventSound( “sound-dont-touch-phone.mp3” )

If one of you know a bettere way to make the app/program please write to me, I really want to learn and understand lua/corona
end

I don’t see anything attached. The first question that comes to mind is what type of audio file are you using? WAV? MP3?

if you have any other ideas to me please just write to me :smiley:

You are referencing functions/objects like button3, button4 but you never create them.  It looks like you’re writing over top of previous handlers, functions and objects.

but how can i get it to work?

can someone please help me, how to play the audio in this code. it works in the simulator on the pc but not on the phone. cant find any tutorials/guides to do it. please

As I said earlier, unless each of those:


–lyd/knap 6–

blocks are separate .lua files, you are writing over each button because you name it the same.  Look at this re-write of one of your buttons:

[lua]


–lyd/knap 4–


local button4 = display.newImage( “button4.png” )
button4.x = display.contentWidth / 4
button4.y = display.contentHeight - 250

function button4:tap( event )
    local r = math.random( 0, 255 )
    local g = math.random( 0, 255 )
    local b = math.random( 0, 255 )
    textObject:setTextColor( r, g, b )
    media.playEventSound( “sound4.wav” )
end

button4:addEventListener( “tap”, button4 )

–[[ get rid of this block
function button:tap( event )
    local r = math.random( 0, 255 )
    local g = math.random( 0, 255 )
    local b = math.random( 0, 255 )
    textObject:setTextColor( r, g, b )

    media.playEventSound( “sound4.wav” )
end

]]–

[/lua]

The additions I made are in red.  Delete the magenta block.  You were defining button:tap twice, each doing something a little different.  You also were naming the button “button” not button4.  Each display object needs to have it’s own unique variable name so you can reference it.  Without it, you are just loosing access to the previous ones since you were attaching the tap handler to the object “button” which was the last one.  This also is a prime example of a memory leak too.

Please when posting code, it’s very helpful to type in  [lua] (no spaces) paste your code in then close it with [/lua] (again no spaces between the brackets).  That way your indentions will be preserved and makes your code more readable.

I don’t see anything attached. The first question that comes to mind is what type of audio file are you using? WAV? MP3?

if you have any other ideas to me please just write to me :smiley:

You are referencing functions/objects like button3, button4 but you never create them.  It looks like you’re writing over top of previous handlers, functions and objects.

but how can i get it to work?

can someone please help me, how to play the audio in this code. it works in the simulator on the pc but not on the phone. cant find any tutorials/guides to do it. please

As I said earlier, unless each of those:


–lyd/knap 6–

blocks are separate .lua files, you are writing over each button because you name it the same.  Look at this re-write of one of your buttons:

[lua]


–lyd/knap 4–


local button4 = display.newImage( “button4.png” )
button4.x = display.contentWidth / 4
button4.y = display.contentHeight - 250

function button4:tap( event )
    local r = math.random( 0, 255 )
    local g = math.random( 0, 255 )
    local b = math.random( 0, 255 )
    textObject:setTextColor( r, g, b )
    media.playEventSound( “sound4.wav” )
end

button4:addEventListener( “tap”, button4 )

–[[ get rid of this block
function button:tap( event )
    local r = math.random( 0, 255 )
    local g = math.random( 0, 255 )
    local b = math.random( 0, 255 )
    textObject:setTextColor( r, g, b )

    media.playEventSound( “sound4.wav” )
end

]]–

[/lua]

The additions I made are in red.  Delete the magenta block.  You were defining button:tap twice, each doing something a little different.  You also were naming the button “button” not button4.  Each display object needs to have it’s own unique variable name so you can reference it.  Without it, you are just loosing access to the previous ones since you were attaching the tap handler to the object “button” which was the last one.  This also is a prime example of a memory leak too.

Please when posting code, it’s very helpful to type in  [lua] (no spaces) paste your code in then close it with [/lua] (again no spaces between the brackets).  That way your indentions will be preserved and makes your code more readable.