How Do I Play a Sound When Object is Pressed (My Code is Failing)

Hey all,

I have been trying to have my app play a short mp3 file called bubbletap.mp3 whenever I press the balloon. Here is my code…can you help me?

local soundID = media.newEventSound( "bubbletap.mp3" )  
   
local playBeep = function()  
 media.playSound( "bubbletap.mp3" )  
end  
  
balloon:addEventListener("touch", playBeep )  

Is it a problem that the file is really short (It is less than 1 second)?

Thanks,
Matt [import]uid: 19514 topic_id: 5263 reply_id: 305263[/import]

I haven’t programmed sounds yet but just at a glance it looks like you should be using soundID to play, not the filename. As in

media.playSound(soundID)  

Incidentally, mp3 is not a great format to use for short sounds. Mp3 is great for long sound files (like music) because of it’s great compression, but for short sounds (like most sound effects) the compression is too much overhead and you’re better off using something like .caf

http://developer.anscamobile.com/content/multimedia-features [import]uid: 12108 topic_id: 5263 reply_id: 17412[/import]

Thats how i did it.

-- Add sound here  
local soundID = media.newEventSound( "Pressed.caf" ) -- your sound file here  
-- Listen to an event  
local function gobuttonpressed(event)  
 if not (event.phase == "ended") then return end  
 media.playSound("Pressed.caf") -- here your sound file  
  
  
  
gobutton:addEventListener("touch", gobuttonpressed)  

Hope that helps
[import]uid: 11559 topic_id: 5263 reply_id: 17975[/import]