Controlling Audio With Icons
Controlling Audio With Icons

you can download the following sample which button to show ‘stop audio button’ or ‘start audio button’ by detecting audio playing when the page switches.
It contains a sample BGM koto_bgm.mp3. It plays in all pages when each page starts

Let’s make audio control with play/stop buttons
-
Create a button to play audio and set with layer hide for play icon and layer show action for stop icon.
-
layer:audioOn
- Hide audioOn
- Show audioOff
- Resume Audio
-
-
Create a button to pause audio and set with layer hide for stop icon and layer show action for play icon.
- layer:audioOff
- Hide audioOFF
- Show audioOn
- Pause Audio
- Hide audioOFF
- layer:audioOff
Now use an external code to detect audio playing or not.
-
add an external code with mycode.lua which should be executed after buttons and actions
-
mycode.lua
-
After button and actions
-
mycode.lua uses timer with 50 msec to detect the audio playing or not. It is a race case of detecting or audio playing which is faster when the app starts.
timer.performWithDelay(50, function()
if audio.isChannelPlaying(1) then
layer.audioOff.alpha = 1
layer.audioOn.alpha = 0
else
layer.audioOff.alpha = 0
layer.audioOn.alpha = 1
end
end)
Accessing each layer
as you see the code above. Each layer on page is stored in the layer table. layer.audioOff and layer.audioOn are displayObjects of Solar2D.
please visit https://coronalabs.com for more information about the API.