Corona SDK - 列出项目的(音频)资产文件(我认为会在 system.ResourceDirectory 中)

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

我在我的 Corona 项目中使用 .mp3 和 .wav 资源。 当我打开 Corona 项目的沙盒资源目录时,我看到的只是用于应用程序图标等的 .png 文件,而不是音频文件(.mp3s 和 .wavs)。

但是我有一个方便的功能来检查目录中是否存在文件:

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

当我使用

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

我收到 true 返回,确认我有文件。 还有,我可以播放音乐等等

我有很多这样的音频文件,我想将它们复制到 system.DocumentsDirectory,使用 for 循环列出每个音频文件并将其复制到 DocumentsDirectory。

我宁愿不必为每个文件手动编码。

有什么方法可以列出 Corona 投影中的音频资产???

我原以为这些资产文件会在 system.ResourceDirectory 中,但如果我打开沙盒,我看不到它们,如果我使用此代码列出它,音频文件也不会列入名单:

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

因此,如果我使用上面的 doesFileExist 函数,音频文件是 'found' 和 'visible',但是如果我使用上面的代码检查 system.ResourceDirectory 中的文件],然后找不到音频文件....如果我打开项目的沙箱,我也看不到那里的音频文件....

如何列出我的 corona 项目中包含的所有资产(在我的案例中是音频资产)???

谢谢...

你的问题似乎有些混乱,所以让我澄清一下。

system.ResourceDirectory = 项目文件所在的位置(即 main.lua 等) system.DocumentsDirectory = 沙盒(即 "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

现在,话虽如此,我们需要找到您的 MP3 文件。我可能会用我所有的 MP3 创建一个文件夹 (%MY_PROJECT%/assets/audio/) 并将它们复制到 system.Documentsdirectory 如果它不存在但是如果你仍然坚持在主文件夹下找到 MP3 文件这里是一些有效的代码:

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

实现 copyFile 并确保它可以访问 doesFileExist。如果 overwrite 参数设置为 false 文件将不会被覆盖,因此上面的代码片段中不需要任何 "Does file exist" 逻辑,因为它是在 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