4

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:
[tubepress video=M9p6XKk0kLo]

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 )

  • Braxton Gregory Smith says:

    meant to put this on the transition tutorial

  • Rahul Singh says:

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

  • Dr. Burton says:

    Audio isn’t really background with Corona. You have 32 channels available for audio and control the maximum level of each channel through the audio api. I have a chapter on it in my forth-coming book!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © 2010-2019 Burtons Media Group All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.