Corona SDK - 列出项目中的(音频)资源文件(我认为应该位于system.ResourceDirectory中)

我在我的 Corona 项目中使用了 .mp3 和 .wav 资源文件。当我打开我的 Corona 项目 Sandbox 的 Resource 目录时,我只看到用于应用程序图标等的 .png 文件,而不是音频文件 (.mp3 和 .wav)。 但我有一个方便的函数来检查目录中文件的存在:

function doesFileExist( fname, path )

    local results = false

    local filePath = system.pathForFile( fname, path )

    -- 如果文件不存在并且路径是 ResourceDirectory,则 filePath 将为 nil
    --
    if filePath then
        filePath = io.open( filePath, "r" )
    end

    if  filePath then
        print( "文件找到 -> " .. fname )
        -- 清除文件
        filePath:close()
        results = true
    else
        print( "文件不存在 -> " .. fname )
    end

    print()

    return results
end

当我使用

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

我得到一个“true”的返回值,这证实我在那里有这个文件。并且我能够播放音乐等。 我有很多这样的音频文件,我想使用 for 循环将每个音频文件复制到 system.DocumentsDirectory 中。

我不想手动编码每个文件。

有没有办法在 Corona 中列出项目中的音频资源?

我原以为这些资源文件会在 system.ResourceDirectory 中,但如果我打开 Sandbox,我没有看到它们,如果我使用此代码列出它们,则不会包括音频文件:

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 是当前文件或目录名称
    print( "资源目录 - 找到文件: " .. file )

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

    if(exists( loadTypes[ "sound" ].extensions , tempext) or exists( loadTypes[ "stream" ].extensions , tempext)) then
        print('发现一个要添加的')

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

end

因此,如果我使用上面的doesFileExist函数,就会发现音频文件,并且“可见”,但如果我使用上面的代码检查 system.ResourceDirectory 中的文件,则找不到音频文件。如果我打开项目的 Sandbox,我也看不到音频文件。

如何列出我在 Corona 项目中包含的所有资源(音频资源)?

谢谢...

点赞
用户2524586
用户2524586

你的问题似乎有些混淆,让我澄清一些事情。

system.ResourceDirectory = 你的项目文件位置(例如 main.lua 等) system.DocumentsDirectory = 沙盒(例如,“展示项目沙盒”)

system.DocumentsDirectory:

在 Corona 模拟器中,它将位于以每个应用程序为基础的沙盒文件夹中。你可以通过文件 - 显示项目沙盒查看目录/文件。

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

现在,有了这个说法,我们需要找到你的 MP3 文件。我可能会创建一个文件夹 (%MY_PROJECT%/assets/audio/),把所有的 MP3 文件复制到 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,则不会覆盖该文件,因此在上面的片段中没有任何“是否存在文件”的逻辑,因为它在 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
2016-01-04 21:09:47