I’ve been trying to build a “Talking Tom” clone App, but it fails to point that it does not record the first second of what we speak, causing it to become impracticable.
I dont know if thats a bug in Corona’s media API, or simply bad code design from me :\
Any tips or corrections are most welcome.
Here is the code:
[lua]display.setStatusBar( display.HiddenStatusBar )
– iOS 5 -only workaround for known Apple bug (Bug ID:9948362 and 10508829)
local platVersion = system.getInfo( “platformVersion” )
platVersion = string.sub( platVersion, 1, 1 )
if system.getInfo( “platformName” ) == “iPhone OS” and platVersion == “5” then
if audio.supportsSessionProperty then
audio.setSessionProperty(audio.MixMode, audio.PlayAndRecordMixMode)
end
end
local dataFileName = “testfile”
if “simulator” == system.getInfo(“environment”) then
dataFileName = dataFileName … “.aif”
else
local platformName = system.getInfo( “platformName” )
if “iPhone OS” == platformName then
dataFileName = dataFileName … “.aif”
elseif “Android” == platformName then
dataFileName = dataFileName … “.pcm”
else
print("Unknown OS " … platformName )
end
end
local fSoundPlaying = false – sound playback state
local filePath = system.pathForFile( dataFileName, system.DocumentsDirectory )
local recPlay = media.newRecording(filePath) – object for recording and playing the audio
local recVol = media.newRecording() – object for volume measurement
recVol:startRecording(); recVol:startTuner() – init the Volume measurement
– Create an object to hold display items
local g = display.newGroup()
– status label
local volume = display.newText( “INIT”, 0, 0, nil, 22 )
volume:setReferencePoint( display.TopLeftReferencePoint)
volume:setTextColor( 255,0,0, 200 )
volume.x = display.contentWidth/2 - 20
volume.y = display.contentHeight/2
g:insert(volume)
– quando acaba de tocar
local function onCompleteSound (event)
fSoundPlaying = false
– Free the audio memory and close the file now that we are done playing it.
audio.dispose(event.handle)
end
local function startRecording ()
if recPlay:isRecording () then
–do nothing
else
fSoundPlaying = false
recPlay:startRecording()
end
end
local function startPlaying ()
if recPlay:isRecording () then
recPlay:stopRecording()
– Play back the recording
local file = io.open( filePath, “recPlay” )
if file then
io.close( file )
fSoundPlaying = true
playbackSoundHandle = audio.loadStream( dataFileName, system.DocumentsDirectory )
echoChannel, echoSource = audio.play( playbackSoundHandle, { onComplete=onCompleteSound } )
al.Source(echoSource, al.PITCH, 1.5 );
end
else
– do nothing
end
end
– volume measurement
function volume:enterFrame( event )
local v = 20*math.log(recVol:getTunerVolume())
if fSoundPlaying then
volume.text = “PLAYING”
else
if (v > -20) then
volume.text = “RECORDING @ " … string.format(”%4.2f", v )
startRecording ()
else
volume.text = “IDLE @ " … string.format(”%4.2f", v )
startPlaying()
end
end
end
Runtime:addEventListener( “enterFrame”, volume );[/lua] [import]uid: 123238 topic_id: 21642 reply_id: 321642[/import]