preston
preston

Reputation: 4357

Corona SDK - List (Audio) Asset Files of Project (that I thought would be in the system.ResourceDirectory)

I use .mp3 and .wav assets in my Corona project. When I open my Corona Project's Sandbox's Resource Directory, all I see are the .png files that are used for the app icons, etc. and not the audio files (.mp3s and .wavs).

However I have a handy function to check the existence of a file in a directory:

function doesFileExist( fname, path )

    local results = false

    local filePath = system.pathForFile( fname, path )

    -- filePath will be nil if file doesn't exist and the path is ResourceDirectory
    --
    if filePath then
        filePath = io.open( filePath, "r" )
    end

    if  filePath then
        print( "File found -> " .. fname )
        -- Clean up our file handles
        filePath:close()
        results = true
    else
        print( "File does not exist -> " .. fname )
    end

    print()

    return results
end

When I use

doesFileExist('filename.mp3', system.ResourceDirectory)

I got a true returned which confirms I have the file there. Also, I am able to play the music, etc.

I have quite a few of these audio files that I would like to copy to the system.DocumentsDirectory using a for loop listing each audio file and copying it to the DocumentsDirectory.

I would rather not have to encode this MANUALLY per file.

Is there any way to list the audio assets inside the projected in Corona???

I would have thought that these asset files would be in the system.ResourceDirectory, but if I open the sandbox, I don't see them and if I list it using this code, the audio files do not get included in the list:

local lfs = require "lfs"

local doc_path = system.pathForFile( "", system.ResourceDirectory )

print('doc_path', doc_path)

for file in lfs.dir(doc_path) do
    -- file is the current file or directory name
    print( "RESOURCE DIRECTORY - Found file: " .. file )


    print(GetFileExtension(file))
    local tempext = GetFileExtension(file)

    if(exists( loadTypes[ "sound" ].extensions , tempext) or exists( loadTypes[ "stream" ].extensions , tempext)) then
        print('FOUND ONE TO ADD')

    end
    -- if(file ~= '.' and file ~= '..' and file ~= dataFileName) then
    -- if(file ~= '.' and file ~= '..') then
    --  table.insert(audioFiles, file)
    -- end

end

Therefore if I use the doesFileExist function above, the audio files are 'found' and 'visible', but if I use the code above that checks the files inside system.ResourceDirectory, then the audio files are NOT FOUND....and if I open the project's sandbox, I also don't see the audio files there....

How do I list all the assets (audio assets in my case) I have included in my corona project???

Thank you...

Upvotes: 0

Views: 411

Answers (1)

Westerlund.io
Westerlund.io

Reputation: 2833

There seems to be some confusion in your question so let me clear some things up.

system.ResourceDirectory = Where you project files are (i.e. main.lua etc.) system.DocumentsDirectory = Sandbox (i.e. "Show Project Sandbox")

system.Documentsdirectory:

In the Corona Simulator, this will be in a sandboxed folder on a per-application basis. You can view the directories/files via File → Show Project Sandbox.

https://docs.coronalabs.com/api/library/system/DocumentsDirectory.html#system.documentsdirectory

Now, with that said, we need to find your MP3 files. I would probably create a folder (%MY_PROJECT%/assets/audio/) with all my MP3's and copy them over to system.Documentsdirectory if it doesn't exist however if you still insist on finding the MP3 file under the main folder here is some code that works:

local lfs  = require("lfs")
local path = system.pathForFile(nil, system.ResourceDirectory)
lfs.chdir(path)

-- Load in each file found
for file in lfs.dir(path) do
    local last_three = string.sub( file, #file - 2, #file)
    if last_three == "mp3" then
        -- LOGIC --
            copyFile( file, system.ResourceDirectory, file, system.DocumentsDirectory, false )

    end
end

Implement copyFile and make sure it has access to doesFileExist. If the overwrite parameter is set to false the file will not be overwritten so there is no need to have any "Does file exist" logic in the snippet above as it is implemented in copyFile:

function copyFile( srcName, srcPath, dstName, dstPath, overwrite )

    local results = false

    local fileExists = doesFileExist( srcName, srcPath )
    if ( fileExists == false ) then
        return nil  -- nil = Source file not found
    end

    -- Check to see if destination file already exists
    if not ( overwrite ) then
        if ( fileLib.doesFileExist( dstName, dstPath ) ) then
            return 1  -- 1 = File already exists (don't overwrite)
        end
    end

    -- Copy the source file to the destination file
    local rFilePath = system.pathForFile( srcName, srcPath )
    local wFilePath = system.pathForFile( dstName, dstPath )

    local rfh = io.open( rFilePath, "rb" )
    local wfh, errorString = io.open( wFilePath, "wb" )

    if not ( wfh ) then
        -- Error occurred; output the cause
        print( "File error: " .. errorString )
        return false
    else
        -- Read the file and write to the destination directory
        local data = rfh:read( "*a" )
        if not ( data ) then
            print( "Read error!" )
            return false
        else
            if not ( wfh:write( data ) ) then
                print( "Write error!" )
                return false
            end
        end
    end

    results = 2  -- 2 = File copied successfully!

    -- Close file handles
    rfh:close()
    wfh:close()

    return results
end

Upvotes: 1

Related Questions