Developing iPhone/iPad/Android Apps with Corona: Adding Sound Effects

Posted by Dr. Burton on July 9, 2010 in Android, Corona, iPad, iPhone, Mobile |

What’s a game without sound?  Sound and music can turn a boring humdrum game or movie into a riveting adventure, if done correctly!

In this tutorial we are going to show the process to add a sound effect to a Corona app.

When working with Corona, you are encouraged to use the CAF file type for your small audio files.  CAF was developed by Apple for use with it’s newer operating system to replace the wav and AIFF file formats.

Place your sound effect file in the same folder as your main.lua and png graphic files.

I’m starting with the main.lua file previously created for fading in and out:

local textObj = display.newText(“Hello World!”, 50, 50, nil, 24)
textObj:setTextColor( 255, 255, 255)

local mybutton = display.newImage( “button.png” )
mybutton.x = display.contentWidth/2
mybutton.y = display.contentHeight-75

local h = textObj.height
local w = textObj.width

function mybutton:tap( event )
textObj.alpha =0
textObj.x = math.random(w/2, display.contentWidth – (w/2))
textObj.y = math.random(h/2, display.contentHeight -(100 + h/2))
transition.to (textObj, {time=3000, alpha=1})
end

mybutton:addEventListener( “tap”, mybutton )

We are going to add one line of code right after the function:
media.playEventSound(“beep.caf”)

So our final function looks like:

function mybutton:tap( event )
media.playEventSound(“beep.caf”)
textObj.alpha =0
textObj.x = math.random(w/2, display.contentWidth – (w/2))
textObj.y = math.random(h/2, display.contentHeight -(100 + h/2))
transition.to (textObj, {time=3000, alpha=1})
end

See the process demonstrated:

Android/iPhone/iPad Development with Corona-sound effects
Runtime
2:28
View count
4,137

Note: I have changed display.stageWidth and display.stageWidth to display.contentWidth & display.contentHeight for version 2.0 of Corona.  As of 2.0 stageHeight & stageWidth have been depreciated.

Tags: , , , , , ,

4 Comments

  • Braxton Gregory Smith says:

    Hey I was wondering if you could help me with this error I am getting which states:

    main.lua:1: in main chunk
    Runtime error: bad argument #2 to ‘_newText’ (number expected, got nil)
    stack traceback:
    [C]: ?
    [C]: in function ‘_newText’
    ?: in function ‘newText’

    My code for the application is:

    local textObj = display.newText( “Hello World!”, x, y, native.systemFont, 24 )
    textObj:setTextColor( 255, 255, 255 )

    local myButton = display.newImage( “button.png” )
    myButton.x = display.contentWidth/2.0
    myButton.y = display.contentHeight – 75

    function myButton:tap( event )
    local w = textObj.width / 2
    local h = textObj.height / 2
    local tempT = 1000
    local tempX = math.random( h, display.contentHeight – h )
    local tempY = math.random( w, display.contentWidth – w )
    local params = { time = tempT, x = tempX, y = tempY }
    transition.to( textObj, params )
    end

    myButton:addEventListener( “tap”, myButton )

  • Rahul Singh says:

    can anybody tell me how to add backgroung audio with volume level settings…

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree

Copyright © 2010-2012 Burtons Media Group All rights reserved.
Desk Mess Mirrored version 1.9.1 theme from BuyNowShop.com.