Store an audio file

I have made my coding But now i wonder how can I save my audio file that i have recorded.How can we do that?can somebody help me please?

[code]
module(…, package.seeall)

function new()
local localGroup = display.newGroup()
local ui = require(“ui”)

local r
local recButton

local status = false

local bgimage = display.newImageRect(“background2.jpg”,1024,768)
bgimage.x = 512
bgimage.y = 384
localGroup:insert(bgimage)

local isAndroid = “Android” == system.getInfo(“platformName”)
local isXcodeSimulator = “iPhone Simulator” == system.getInfo(“model”)

if(isAndroid or isXcodeSimulator) then
local alert = native.showAlert( “Information”, “Camera API not available on Android or iOS Simulator.”, { “OK”})
end

local dataFileName = “testfile”
if “simulator” == system.getInfo(“environment”) then
dataFileName = dataFileName … “.wav”
else
local platformName = system.getInfo( “platformName” )
if “iPhone OS” == platformName then
dataFileName = dataFileName … “.wav”
elseif “Android” == platformName then
dataFileName = dataFileName … “.pcm”
else
print("Unknown OS " … platformName )
end
end
print (dataFileName)

local text = ui.newLabel{
bounds = { 350, 610, 300, 40 },
text = “Take a Picture”,
font = system.defaultFont,
textColor = {255 , 255,255, 255 },
size = 25,
align = “center”
}

local btn = display.newRoundedRect( 400, 600,200,50,16)
btn:setFillColor( 0, 0, 0,125 )
local sessionComplete = function(event)
local image = event.target
print( "Camera ", ( image and “returned an image” ) or “session was cancelled” )
print( "event name: " … event.name )
print( "target: " … tostring( image ) )

if image then
image.x = display.contentWidth/2
image.y = display.contentHeight/2
local w = image.width
local h = image.height
print( “w,h = “… w …”,” … h )
end
end

local listener = function( event )
media.show( media.Camera, sessionComplete )
return true
end

btn:addEventListener( “tap”, listener )

local s = ui.newLabel{
bounds = { 350, 530, 300, 40 },
text = " ",
textColor = { 255,255 , 255, 255 },
size = 32,
align = “center”
}

local roundedRect = display.newRoundedRect( 397, 522, 200, 50, 8 )
roundedRect:setFillColor( 0, 0, 0, 170 )
local function updateStatus ()
local statusText = " "
if r then
local recString = “”
local fRecording = r:isRecording ()
if fRecording then
recString = “Recording”
else
recString = “Idle”
end
statusText = recString
end
s:setText (statusText)
end

local function update ()
local statusText = " "
if r then
local recString = “”
if fSoundPlaying then
recString = “Playing”
elseif fSoundPaused then
recString = “Paused”
else
recString = “Idle”
end
statusText = recString
end
s:setText (statusText)
end

local function recButtonPress ( event )

if r then
if r:isRecording () then
local rec=display.newImage(“rec.png”)
rec.x=330
rec.y=545
r:stopRecording()
local filePath = system.pathForFile( dataFileName, system.DocumentsDirectory )
local file = io.open( filePath, “r” )
if file then
io.close( file )
fSoundPlaying = false
fSoundPaused = false
playbackSoundHandle = audio.loadStream( dataFileName, system.DocumentsDirectory )
end
else
fSoundPlaying = false
fSoundPaused = false
local rec=display.newImage(“recstop.png”)
rec.x=330
rec.y=545
audio.dispose(playbackSoundHandle)
r:startRecording()
end

end
updateStatus ()
end

local fSoundPlaying = false
local fSoundPaused = false

local function playButtonPress(event)

audio.play( playbackSoundHandle )
fSoundPlaying = true
fSoundPaused = false
update ()

end
recButton = ui.newButton{
default = “rec.png”,
over = “rec.png”,
onPress = recButtonPress,
emboss = true
}

recButton.x = 330
recButton.y = 545

playButton = ui.newButton{
default = “play.png”,
over = “play.png”,
onPress = playButtonPress,
emboss = true
}

playButton.x = 670
playButton.y = 545
local filePath = system.pathForFile( dataFileName, system.DocumentsDirectory )
r = media.newRecording(filePath)
updateStatus ()

return localGroup
end
[/code] [import]uid: 82446 topic_id: 16992 reply_id: 316992[/import]

Looks like your code was pulled from here:
http://developer.anscamobile.com/content/simple-audio-recorder

It is also included in the Corona sample files under “Media/SimpleAudioRecorder/…”

I took a look at this file and opened the sandbox [Corona: File/Show Project Sandbox] and looked what is happening when you hit the record button. The first time you record it creates the audio file (aif in simulator mode), which is stored in the sandbox. Subsequently, each time you press the record button it will record over that file. If you relaunch the simulator, or if you close it, the file is still there in the sandbox.

I have not tested this, but I think if you want to record several files you will have to name them differently so each one is saved in a separate file (audio1.aif, audio2.aif, etc.), maybe with different record buttons, or with different pages, or a single page that you pass a variable to so it knows to which file to write the recording. Then you just have to load whichever file you want with audio.loadStream and a play button. Like I said, did not test this, but based on my perusal of the “simple recording” sample file, I think it would work. I need to do this on an upcoming project. I can post what I find out then when I have more time to code it.
[import]uid: 32178 topic_id: 16992 reply_id: 64307[/import]

yes That is from where I took it.I have tested it but it is not saving…I don’t know but why?I again did a bit of modification to test that but its not saving the audio file.please help me ?here’s the same code

[code]
local ui = require(“ui”)
local background = display.newImage(“carbonfiber.jpg”, true)
background.x = display.contentWidth/2
background.y = display.contentHeight/2

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
print (dataFileName)

local t1 = ui.newLabel{
bounds = { 10, 25, 300, 40 },
text = “Echo”,
font = “AmericanTypewriter-Bold”,
textColor = { 255, 204, 102, 255 },
size = 40,
align = “center”
}
local t2 = ui.newLabel{
bounds = { 10, 75, 300, 40 },
text = “Simple Audio Recorder”,
font = “AmericanTypewriter-Bold”,
textColor = { 255, 204, 102, 255 },
size = 22,
align = “center”
}
local roundedRect = display.newRoundedRect( 10, 160, 300, 80, 8 )
roundedRect:setFillColor( 0, 0, 0, 170 )
local s = ui.newLabel{
bounds = { 10, 180, 300, 40 },
text = " ",
textColor = { 10, 240, 102, 255 },
size = 32,
align = “center”
}

local s2 = ui.newLabel{
bounds = { 10, 380, 300, 40 },
text = " ",
textColor = { 130, 200, 255, 255 },
size = 14,
align = “center”
}

local r
local recButton
local fSoundPlaying = false
local fSoundPaused = false
local function setButtonText( button, text )
button[3].text = text
button[4].text = text
button[5].text = text
end
local function updateStatus ()
local statusText = " "
local statusText2 = " "
if r then
local recString = “”
local fRecording = r:isRecording ()
if fRecording then
recString = “RECORDING”
setButtonText (recButton, “Stop recording”)
elseif fSoundPlaying then
recString = “Playing”
setButtonText (recButton, “Pause playback”)
elseif fSoundPaused then
recString = “Paused”
setButtonText (recButton, “Resume playback”)
else
recString = “Idle”
setButtonText (recButton, “Record”)
end

statusText = recString
statusText2 = "Sampling rate: " … tostring (r:getSampleRate() … “Hz”)
end
s:setText (statusText)
s2:setText (statusText2)
end

local function recButtonPress ( event )
if fSoundPlaying then
fSoundPlaying = false
fSoundPaused = true
audio.pause()
elseif fSoundPaused then
fSoundPlaying = true
fSoundPaused = false
audio.resume()
else
if r then
if r:isRecording () then
r:stopRecording()
local filePath = system.pathForFile( dataFileName, system.DocumentsDirectory )

local file = io.open( filePath, “r” )
if file then
io.close( file )
fSoundPlaying = true
fSoundPaused = false

playbackSoundHandle = audio.loadStream( dataFileName, system.DocumentsDirectory )
audio.play( playbackSoundHandle)
end
else
fSoundPlaying = false
fSoundPaused = false
r:startRecording()
end
end
end
updateStatus ()
end

local rateUpButtonPress = function( event )
local theRates = {8000, 11025, 16000, 22050, 44100}
if not r:isRecording () and not fSoundPlaying and not fSoundPaused then

local f = r:getSampleRate()

local i, v = next (theRates, nil)
while i do
if v <= f then
i, v = next (theRates, i)
else
i = nil
end
end
if v then
r:setSampleRate(v)
else
r:setSampleRate(theRates[1])
end

end
updateStatus()
end
local rateDownButtonPress = function( event )
local theRates = {44100, 22050, 16000, 11025, 8000}
if not r:isRecording () and not fSoundPlaying and not fSoundPaused then

local f = r:getSampleRate()

local i, v = next (theRates, nil)
while i do
if v >= f then
i, v = next (theRates, i)
else
i = nil
end
end
if v then
r:setSampleRate(v)
else
r:setSampleRate(theRates[1])
end

end
updateStatus()
end

local function fplay(event)
print(“i am here”)
audio.play( playbackSoundHandle)

end
recButton = ui.newButton{
default = “buttonRed.png”,
over = “buttonRedOver.png”,
onPress = recButtonPress,
text = “Record”,
emboss = true
}
local rateUpButton = ui.newButton{
default = “buttonArrowUp.png”,
over = “buttonArrowUpOver.png”,
onPress = rateUpButtonPress,
id = “arrowUp”
}
local rateDownButton = ui.newButton{
default = “buttonArrowDwn.png”,
over = “buttonArrowDwnOver.png”,
onPress = rateDownButtonPress,
id = “arrowDwn”
}
local playbtn = ui.newButton{
default = “buttonArrowUpOver.png”,
onPress = fplay,
id = “playbtn”
}
playbtn.x=100
playbtn.y=100
recButton.x = 160; recButton.y = 290
rateUpButton.x = 190; rateUpButton.y = 420
rateDownButton.x = 140; rateDownButton.y = 420
local filePath = system.pathForFile( dataFileName, system.DocumentsDirectory )
r = media.newRecording(filePath)
updateStatus ()

[/code] [import]uid: 82446 topic_id: 16992 reply_id: 64563[/import]

What version of Corona are you using? I suggest that you open the audio-recorder sample supplied with Corona. Open this sample file in the simulator. If you can push the record button and hear it play back to you the recording then it is definitely storing the audio file.

Find the Audio-Recording sample here: …/CoronaSDK/Media/SimpleAudioRecorder/

(1) Do you have the Audio-Recording sample with your version of Corona?
(2) Does it allow you to record and then does it playback that recording?

If not, perhaps you don’t have a sound-card or a microphone connected to your system.

If so, then with the CoronaSDK application opened and selected, you should find the stored audio file by clicking on: File/Show Project Sandbox

This will open a folder (the “sandbox”). Inside this folder there should be a “Documents” folder. Open the “Documents” folder, and there should be the AIF/WAV file that the app stored after you hit the record button.

Again, if it is not recording, maybe it’s because you don’t have a sound-card, or it doesn’t have a mic input, or your don’t have a mic connected to your PC/Mac. Without more, I don’t know what to tell you. Good luck!

[import]uid: 32178 topic_id: 16992 reply_id: 64906[/import]

You know what Problem i am facing with the Audio-Recording sample…

In the above code I have placed one more button (playbtn)to play the stored audio.This button is playing the audio when i record and then play but when I relaunch the sample that same button does not plays the audio but in the sandbox that file exists.
Hope you got my Problem now. Could you suggest me a solution for that?
[import]uid: 82446 topic_id: 16992 reply_id: 64987[/import]

Try loading the audio before playing it:

[code]
local function fplay(event)

print(“i am here”)
playbackSoundHandle = audio.loadStream( dataFileName, system.DocumentsDirectory )
audio.play( playbackSoundHandle)

end

[/code] [import]uid: 32178 topic_id: 16992 reply_id: 69750[/import]