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.

You will know how to make actions in Kwik panel to interacte with the buttons.

Overview

act_hide_starting

when user press Record button 3 sec countdown starts. When the count down ends, it triggers act_hide_starting action.

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

act_init

this action is called with the timer when the page is loaded. ( The timer is set with 0 sec ).

but_record

starts 3 sec countdown before recording.

but_stop

stops recording

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

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

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