Advanced Recording
Advanced Recording
Sample project file
screenshots are using Kwik3’s ones but the project file is updated for Kwik4. Change is made to the external files.
This tutorial describes how to record user’s voice.
- Record button, Play button and Delete button are displayed.
- User press Record button, then 3 sec count down starts before recording begins.
- Recording begins for X seconds and X seconds count down starts again to let user know remaining recording time.

You will know how to make actions in Kwik panel to interacte with the buttons.
Overview
- action_hide_all hides all buttons, starting counter, recording sec counter.
- isRecordedData variable is a flag which tells Record button to show or Play/Delete buttons to show
- Record button starts startingCount countdown. When the countdown ends, it triggers act_hide_starting act_hide_starting starts recordingSec countdown and calls “myrecord.lua” as external code. When the countdown ends, it triggers act_hide_recording.
- act_hide_recording stops recording and shows Play/Delete button
- Play button calls external code “myplay.lua”
- Delete button justs show YES/NO buttons
- Yes button to call “mydelete.lua”. No buttons does nothing to hide YES/NO layers
act_hide_starting
when user press Record button 3 sec countdown starts. When the count down ends, it triggers act_hide_starting action.

- hide startingCnd which was finished
- show RecordingSec countdown layer
- Play RecordingSec countdown
- External code “myrecord.lua” to start recording
- Message1 “now recording ..” is displayed
act_hide_all

set this action first action in another action and you can set ‘Show’ with the target layers to be displayed.
act_hide_recording
When RecodingSec countdown ends, it triggers this action to stop Recording

- hide all layers
- hide RecodingSec countdown
- show Play button
- show Delete button
- hide “now recording ..” message
- External code “mystop.lua”
act_init
this action is called with the timer when the page is loaded. ( The timer is set with 0 sec ).

- External code “myinit.lua” that set isRecordedData true or false
- hide all layers
- if isRecordedData == true
- show Play
- show Delete
- else
- show Record
- show message “3 sec..”
- endif
but_record
starts 3 sec countdown before recording.

- Play startingCnt countdown
- show startingCnt layer
- show Stop button
- hide Record button
- hide “Wait 3 sec ..” message
but_stop
stops recording

- External code “mystop” to stop recording
- hide all layers
- if isRecordedData
- show Play button
- show Delete button
- else
- Recode button
- end
- hide “Wait 3 sec ..” message
but_play
plays the recorded voice by the external code “myplay.lua”

but_delete
shows up Yes/No buttons

but_yes
deletes the recorded voice

- hide all layers
- External Code “mydelete.lua”
- Show Record button
but_no
cancel “delete”

hides YES/No buttons and back to show Play/Delete buttons
Add Variable
isRecordedData to be used in the external code files

startingCnt countdown

- Countdown for 3 sec to read story for recording
- it triggers act_hide_starting
RecordingSec countdown
Coutdown while recording.

It triggers act_hide_recording Please set recording time in seconds. The sample is 5 seconds. You also set the time in external code too. See the external code below.
Timer Init
this timer is called once when page starts with 0 second

myinit.lua
it checks whether isRecodedData exist or not
print("myinit")
local filePath = system.pathForFile("page_"..UI.curPage..".pcm", system.DocumentsDirectory )
local file = io.open(filePath, "r")
if file then
UI.isRecordedData = true
end
myrecord.lua
Please set your recording time in the first line
local RecordingSecSeconds = 5
print("myrecord")
_K = require("Application")
local RecordingTime = 5000
UI.allAudios.playback = nil
local filePath = system.pathForFile("page_"..UI.curPage..".pcm", system.DocumentsDirectory )
UI.recodring = media.newRecording(filePath)
UI.recodring:startRecording( )
local audioClosure = function(event )
UI.recodring:stopRecording()
-- sceneGroup.alpha = 1
local file = io.open(filePath, "r")
if file then
UI.isRecordedData = true
end
end
_K.timerStash.recTimer = timer.performWithDelay(RecordingTime, audioClosure )
layer.RecordingSecSeconds = RecordingSecSeconds
UI.upTime_RecordingSec()
if (_K.timerStash.RecordingSec) then
timer.cancel(_K.timerStash.RecordingSec)
_K.timerStash.RecordingSec = nil
end
_K.timerStash.RecordingSec = timer.performWithDelay(1000, UI.upTime_RecordingSec, RecordingSecSeconds+1 )
myplay.lua
print("myplay")
local filePath = system.pathForFile("page_"..UI.curPage..".pcm", system.DocumentsDirectory )
local file = io.open(filePath, "r")
if file then
io.close(file)
UI.allAudios.playback = audio.loadStream( "page_"..UI.curPage..".pcm", system.DocumentsDirectory )
audio.play(UI.allAudios.playback, {channel=31})
print("recoded length:"..audio.getDuration(UI.allAudios.playback))
end
mystop.lua
print("mystop")
_K = require("Application")
if _K.timerStash.RecordingSec then
timer.cancel(_K.timerStash.RecordingSec)
end
if UI.recodring then
UI.recodring:stopRecording()
end
if _K.timerStash.recTimer then
timer.cancel(_K.timerStash.recTimer)
end
layer.startingCntSeconds = -2
_K.timerStash.RecordingSec = nil
sceneGroup.alpha = 1
local filePath = system.pathForFile("page_"..UI.curPage..".pcm", system.DocumentsDirectory )
local file = io.open(filePath, "r")
if file then
UI.isRecordedData = true
end
mydelete.lua
print("mydelete")
local filePath = system.pathForFile("page_"..UI.curPage..".pcm", system.DocumentsDirectory )
local destDir = system.DocumentsDirectory -- Location where the file is stored
local result, reason = os.remove( filePath )
if result then
print( "File removed" )
else
print( "File does not exist", reason ) --> File does not exist apple.txt: No such file or directory
end
UI.isRecordedData = false
if you want to use the recording controls to other pages too, please read page controls/menu overlay