Saving Recorded Audio

I can record audio and play it back but i can’t figure out how to save it in a folder within the ResourceDirectory. 

If I have a folder called “audioRec” in the folder with the main.lua file, how can I record and save the audio in that folder.

Just to note, I tried changing  system.DocumentsDirectory  to  system.ResourceDirectory but recording doesn’t work.  So, I’m still not sure how the whole Save audio thing works.

[lua]

audio.isChannelActive=false

local button = display.newRect( 200, 300, 75, 75 )

button:setFillColor( 0, 1, 0 )

local buttonStop = display.newRect( 200, 400, 75, 75 )

buttonStop:setFillColor( 1, 0, 0 )

local filePath = system.pathForFile( ‘record1.aif’, system.DocumentsDirectory )

    

    local r = media.newRecording( filePath)

    

    local audioHandle = nil

    

    local audioChannel = nil

    

    local function recordAudio(  )

    

        if (audioHandle ~= nil) then

            

            --stop audio before disposing it

            if (audio.isChannelActive(audioChannel)) then

                audio.stop(audioChannel)

            end

                    

            audio.dispose(audioHandle)

        end    

        

                r:startRecording()

        

    end

    local function stopAndPlaybackRecording( )

        

        r:stopRecording()

                

        audioHandle = audio.loadSound( “record1.aif”, system.DocumentsDirectory )

        

        audioChannel = audio.play(audioHandle)

    end

button:addEventListener( “tap”, recordAudio )

buttonStop:addEventListener( “tap”, stopAndPlaybackRecording )

[/lua]

Hi @rshanlon,

You can’t write/save to the Resources directory. This is a restriction of the OS for security reasons.

Take care,

Brent

Gotcha.  So how do I save the audio then?

Appreciate your help on this. 

Hmmm… it should save to the Documents directory as shown in the documentation… does it not?

Brent, I’m not sure where that directory is on my apple computer.  I’m just testing on the Simulator now.  The audio clearly gets recorded and plays back but I see no audio file in the folder where my main.lua resides.

If that folder isn’t the Docs directory then I’m not sure where it resides on the computer.

Thanks for your help.

With the simulator up and running choose the File > Show Project Sandbox menu item. Drill down in the folder that shows up – the Documents folder is the system.DocumentsDirectory location.

 Jay

Thank you, Jay.  Found it.

Funny that you were the one to answer the question because my wife just told me she saw your video tut about using that directory to keep scores in a game.  I knew I had to go see that to find the answer.  Then you posted.

Thanks again. 

Another interesting thing about the recorded audio is I have recorded aif, wav, mp3, m4a and the mp3 and m4a are not smaller files.  They are coming in about the same size as the wav and aif files

I was under the impression that compressed audio couldn’t be recorded with this API.  Maybe it’s not really compressed but it’s still taking on the mp3 and m4a extensions since that’s what I’m telling it to save it as.  They’re playing back because those extensions are recognized as audio formats.

Hmmm.  Not sure.

Hi @rshanlon,

You can’t write/save to the Resources directory. This is a restriction of the OS for security reasons.

Take care,

Brent

Gotcha.  So how do I save the audio then?

Appreciate your help on this. 

Hmmm… it should save to the Documents directory as shown in the documentation… does it not?

Brent, I’m not sure where that directory is on my apple computer.  I’m just testing on the Simulator now.  The audio clearly gets recorded and plays back but I see no audio file in the folder where my main.lua resides.

If that folder isn’t the Docs directory then I’m not sure where it resides on the computer.

Thanks for your help.

With the simulator up and running choose the File > Show Project Sandbox menu item. Drill down in the folder that shows up – the Documents folder is the system.DocumentsDirectory location.

 Jay

Thank you, Jay.  Found it.

Funny that you were the one to answer the question because my wife just told me she saw your video tut about using that directory to keep scores in a game.  I knew I had to go see that to find the answer.  Then you posted.

Thanks again. 

Another interesting thing about the recorded audio is I have recorded aif, wav, mp3, m4a and the mp3 and m4a are not smaller files.  They are coming in about the same size as the wav and aif files

I was under the impression that compressed audio couldn’t be recorded with this API.  Maybe it’s not really compressed but it’s still taking on the mp3 and m4a extensions since that’s what I’m telling it to save it as.  They’re playing back because those extensions are recognized as audio formats.

Hmmm.  Not sure.