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. 