Let's simulate our own little music player by creating user interface buttons that will control the audio calls as follows:
In the
Chapter 6
folder, copy theControlling Audio
project folder to your desktop. You will notice several art assets, aui.lua
library,config.lua
file, and asong2.mp3
file inside. You can download the project files accompanying this book from the Packt Publishing website.In the same project folder, create a brand new
main.lua
file.Load the audio file via
loadStream()
, name itmusic
, and call the UI library. Also add it in a local variable calledmyMusic
:local ui = require("ui") local music = audio.loadStream( "song2.mp3" ) local myMusicChannel
Create a local function called
onPlayTouch()
with anevent
parameter to play the audio file. Add anif
statement that containsevent.phase == "release"
so that the music starts playing when the button releases. Apply theplayBtn
display object as a new UI button:local onPlayTouch = function...