Corona SDK - 列出项目中的(音频)资源文件(我认为应该位于system.ResourceDirectory中)
2016-1-4 14:54:37
收藏:0
阅读:104
评论:1
我在我的 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 项目中包含的所有资源(音频资源)?
谢谢...
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
你的问题似乎有些混淆,让我澄清一些事情。
system.ResourceDirectory= 你的项目文件位置(例如 main.lua 等)system.DocumentsDirectory= 沙盒(例如,“展示项目沙盒”)system.DocumentsDirectory:
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