Always Play Sound with click on and click off

Hi everybody

Im continue playing with Corona SDK.

I discover the  function media.playSound()

Im testing it and the sound play 2 times, first time when I clickOn one image (is testing) and a second time when I cilck off with my mouse

Is this normal?

Is neccessary more funtions for this?

My code (Im usind storyboards)

local function on_but_Touch( self, event )

    

    media.playSound( “mp3/test.mp3” )

    

end

I use this function with the listener:

but_UK:addEventListener ( “touch”, on_but_Touch)

Thanks in advance

My question is similar. Im looking for an idea on how to make a graphic act like a toggle. For example, you can finger click it once for sound on and finger click it again to turn it off (like a light switch). Here is what I have so far.

--Code below shows controls on my button that are supposed to stop current random song and play a new random song. -- reserve app too 2 audio channels audio.reserveChannels(2) -- function to stop current music and play on a new channel local isMusicPlaying = audio.isChannelPlaying( 1 ) if isChannelPlaying then audio.stop(2) audio.play(1) else audio.stop(1) audio.play(2) end -- turn graphic into a button and play a different song when button is used local function onObjectTap( event ) --print( "Tap event on: " .. event.target.name ) --return true -- Define songs array startMusic = {} -- load songs startMusic[1] = audio.loadStream("song1.mp3") startMusic[2] = audio.loadStream(("song2.mp3") startMusic[3] = audio.loadStream("song3.mp3") -- setup function to play random music function playStartMusic1() -- Get a random number between 1 and however many items are in the startMusic array local thisStartMusic = math.random(#startMusic) -- Play the audio file and when done playing, call a function audio.play(startMusic[thisStartMusic], {onComplete=playStartMusic2}) end -- setup function to play random music function playStartMusic2() -- Get a random number between 1 and however many items are in the startMusic array local thisStartMusic = math.random(#startMusic) -- Play the audio file and when done playing, call a function audio.play(startMusic[thisStartMusic], {onComplete=playStartMusic1}) end -- Start the playing of music playStartMusic1() end startBtn:addEventListener( "tap", onObjectTap )

It plays a random song (like its suppose to do), but when I finger click it again, it plays a new random song without stopping the first one, so as to run multiple songs together when finger clicked many times. My knowledge is still beginner, so im not sure what to do to fix it. Anyone help with a solution. I am trying to get it to play a new random song after it first stops whatever is currently playing. If you are so kind to give me a helpful answer, please comment the code, so I can learn what it is doing. I would like to benefit in knowledge and not just handed something in which I do not understand how it works… Thanks in advance. Appreciated. :smiley:

Hi Toy,

when you run a touch event you have different phases, one is began and one is ended. When you first touch the image (or whatever) the “began” phase is initialized. When you lift the finger the “ended” phase is initialized.

Therefore you should do something like this:

local function on\_but\_Touch( event ) if event.phase == "began" then -- The bubble.wav will be played when the touch event is first initialized media.playSound( "bubble.wav" ) elseif event.phase == "ended" then -- When the finger is lifted I don't want to do anything so I don't have anything here end end local rect = display.newRect(100, 100, 100, 100) rect:addEventListener ( "touch", on\_but\_Touch)

A little bit more information:

http://docs.coronalabs.com/api/event/touch/index.html

Best regards,

Tomas

Hi Joe,

I would recommend to start your own thread otherwise it might get confusing however here is a code that might suit your needs:

local songTable = {} songTable[1] = "bubble.wav" songTable[2] = "bubble.wav" songTable[3] = "bubble.wav" local playNextSong local function playSong(index) print("playSong - index: " .. index) media.playSound(songTable[index], playNextSong) end local function stopSong() print("stopSong") media.stopSound() end local function getRandomNumber(maxNumber) return math.random(maxNumber) end playNextSong = function() stopSong() local i = getRandomNumber(#songTable) playSong(i) end local function onTap(event) playNextSong() end local rect = display.newRect(100, 100, 100, 100) rect:addEventListener ( "tap", onTap)

Best regards,

Tomas

Hello and thanks tomaswesterlund for the code sample. Can you comment the lines so I know what the code is doing? Being a beginner, I’m not exactly proficient in reading code properly. In fact, I’d like to put this on one side and my old code on the other side and examine the two… It’s how I learn. :wink:

I noticed a static white square is on my screen, which I see is created toward the bottom of the code. I wanted to attach the music code to my graphic that is already bouncing across the screen, but having some trouble. I may need a lesson in code structure…Anyway, help is greatly appreciated. I would really like to get ahold of coding with corona sdk, even though Im not that good so far, I do like it! Thanks again for the help! :smiley:

Here is the code with some comments:

local songTable = {} songTable[1] = "bubble.wav" songTable[2] = "bubble.wav" songTable[3] = "bubble.wav" -- This variable needs to be declared because playSong is calling it local playNextSong -- Plays the soung based on the parameter ("index") that is coming in local function playSong(index) print("playSong - index: " .. index) media.playSound(songTable[index], playNextSong) end -- Stops playback of the extended sound currently opened by the previous call to media.playSound(). // http://docs.coronalabs.com/api/library/media/stopSound.html local function stopSong() print("stopSong") media.stopSound() end -- Generates and returns a random number. May not be necessary but I like to keep everythin in functions for future flexibility -- The parameter ("maxNumber") makes sure that this function will return a number from 1 to maxNumber. local function getRandomNumber(maxNumber) return math.random(maxNumber) end -- Plays next song. This function is called in the touch event. -- First it stops the current song being played by calling stopSong() -- Second it generates random number. #songTable (with the square in front) returns the length of the table songTable -- which means that if I add another song to the table the new length will be 4 instead of, as it is now, 3. This makes -- sure that all songs can be choosen and played when playSong is being called. -- Third it calls the playSong() function with the parameter i, which is set by getRandomNumber. This is now a number -- from 1 to #songTable. If i = 2 songTable[2] will be played. playNextSong = function() stopSong() local i = getRandomNumber(#songTable) playSong(i) end -- This is the tap event. When tapped playNextSong() is being called. local function onTap(event) playNextSong() end -- This is just a rectangle. It was the easiest way to create something that I could touch. -- You can change this to an image, for example a "Play" image. local rect = display.newRect(100, 100, 100, 100) -- Adds the tap event to the rectangle. rect:addEventListener ( "tap", onTap)

What I would do is create a new Corona project, add that code to my main.lua file, add three sound files to your base directory and change the songTable to those songs.

For example:

  1. Add song1.mp3, song2.mp3 and song.mp3 to the base directory (where main.lua is located)

  2. Change these values: songTable[1] = “song1.mp3”, songTable[2] = “song2.mp3” and songTable[3] = “song3.mp3”

and see what happens.

Best regards,

Tomas

Much obliged.  :slight_smile:

My question is similar. Im looking for an idea on how to make a graphic act like a toggle. For example, you can finger click it once for sound on and finger click it again to turn it off (like a light switch). Here is what I have so far.

--Code below shows controls on my button that are supposed to stop current random song and play a new random song. -- reserve app too 2 audio channels audio.reserveChannels(2) -- function to stop current music and play on a new channel local isMusicPlaying = audio.isChannelPlaying( 1 ) if isChannelPlaying then audio.stop(2) audio.play(1) else audio.stop(1) audio.play(2) end -- turn graphic into a button and play a different song when button is used local function onObjectTap( event ) --print( "Tap event on: " .. event.target.name ) --return true -- Define songs array startMusic = {} -- load songs startMusic[1] = audio.loadStream("song1.mp3") startMusic[2] = audio.loadStream(("song2.mp3") startMusic[3] = audio.loadStream("song3.mp3") -- setup function to play random music function playStartMusic1() -- Get a random number between 1 and however many items are in the startMusic array local thisStartMusic = math.random(#startMusic) -- Play the audio file and when done playing, call a function audio.play(startMusic[thisStartMusic], {onComplete=playStartMusic2}) end -- setup function to play random music function playStartMusic2() -- Get a random number between 1 and however many items are in the startMusic array local thisStartMusic = math.random(#startMusic) -- Play the audio file and when done playing, call a function audio.play(startMusic[thisStartMusic], {onComplete=playStartMusic1}) end -- Start the playing of music playStartMusic1() end startBtn:addEventListener( "tap", onObjectTap )

It plays a random song (like its suppose to do), but when I finger click it again, it plays a new random song without stopping the first one, so as to run multiple songs together when finger clicked many times. My knowledge is still beginner, so im not sure what to do to fix it. Anyone help with a solution. I am trying to get it to play a new random song after it first stops whatever is currently playing. If you are so kind to give me a helpful answer, please comment the code, so I can learn what it is doing. I would like to benefit in knowledge and not just handed something in which I do not understand how it works… Thanks in advance. Appreciated. :smiley:

Hi Toy,

when you run a touch event you have different phases, one is began and one is ended. When you first touch the image (or whatever) the “began” phase is initialized. When you lift the finger the “ended” phase is initialized.

Therefore you should do something like this:

local function on\_but\_Touch( event ) if event.phase == "began" then -- The bubble.wav will be played when the touch event is first initialized media.playSound( "bubble.wav" ) elseif event.phase == "ended" then -- When the finger is lifted I don't want to do anything so I don't have anything here end end local rect = display.newRect(100, 100, 100, 100) rect:addEventListener ( "touch", on\_but\_Touch)

A little bit more information:

http://docs.coronalabs.com/api/event/touch/index.html

Best regards,

Tomas

Hi Joe,

I would recommend to start your own thread otherwise it might get confusing however here is a code that might suit your needs:

local songTable = {} songTable[1] = "bubble.wav" songTable[2] = "bubble.wav" songTable[3] = "bubble.wav" local playNextSong local function playSong(index) print("playSong - index: " .. index) media.playSound(songTable[index], playNextSong) end local function stopSong() print("stopSong") media.stopSound() end local function getRandomNumber(maxNumber) return math.random(maxNumber) end playNextSong = function() stopSong() local i = getRandomNumber(#songTable) playSong(i) end local function onTap(event) playNextSong() end local rect = display.newRect(100, 100, 100, 100) rect:addEventListener ( "tap", onTap)

Best regards,

Tomas

Hello and thanks tomaswesterlund for the code sample. Can you comment the lines so I know what the code is doing? Being a beginner, I’m not exactly proficient in reading code properly. In fact, I’d like to put this on one side and my old code on the other side and examine the two… It’s how I learn. :wink:

I noticed a static white square is on my screen, which I see is created toward the bottom of the code. I wanted to attach the music code to my graphic that is already bouncing across the screen, but having some trouble. I may need a lesson in code structure…Anyway, help is greatly appreciated. I would really like to get ahold of coding with corona sdk, even though Im not that good so far, I do like it! Thanks again for the help! :smiley:

Here is the code with some comments:

local songTable = {} songTable[1] = "bubble.wav" songTable[2] = "bubble.wav" songTable[3] = "bubble.wav" -- This variable needs to be declared because playSong is calling it local playNextSong -- Plays the soung based on the parameter ("index") that is coming in local function playSong(index) print("playSong - index: " .. index) media.playSound(songTable[index], playNextSong) end -- Stops playback of the extended sound currently opened by the previous call to media.playSound(). // http://docs.coronalabs.com/api/library/media/stopSound.html local function stopSong() print("stopSong") media.stopSound() end -- Generates and returns a random number. May not be necessary but I like to keep everythin in functions for future flexibility -- The parameter ("maxNumber") makes sure that this function will return a number from 1 to maxNumber. local function getRandomNumber(maxNumber) return math.random(maxNumber) end -- Plays next song. This function is called in the touch event. -- First it stops the current song being played by calling stopSong() -- Second it generates random number. #songTable (with the square in front) returns the length of the table songTable -- which means that if I add another song to the table the new length will be 4 instead of, as it is now, 3. This makes -- sure that all songs can be choosen and played when playSong is being called. -- Third it calls the playSong() function with the parameter i, which is set by getRandomNumber. This is now a number -- from 1 to #songTable. If i = 2 songTable[2] will be played. playNextSong = function() stopSong() local i = getRandomNumber(#songTable) playSong(i) end -- This is the tap event. When tapped playNextSong() is being called. local function onTap(event) playNextSong() end -- This is just a rectangle. It was the easiest way to create something that I could touch. -- You can change this to an image, for example a "Play" image. local rect = display.newRect(100, 100, 100, 100) -- Adds the tap event to the rectangle. rect:addEventListener ( "tap", onTap)

What I would do is create a new Corona project, add that code to my main.lua file, add three sound files to your base directory and change the songTable to those songs.

For example:

  1. Add song1.mp3, song2.mp3 and song.mp3 to the base directory (where main.lua is located)

  2. Change these values: songTable[1] = “song1.mp3”, songTable[2] = “song2.mp3” and songTable[3] = “song3.mp3”

and see what happens.

Best regards,

Tomas

Much obliged.  :slight_smile: