我在Corona项目中使用.mp3和.wav资产。 当我打开Corona项目的Sandbox资源目录时,我看到的是用于应用程序图标等的.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。
我宁愿不必每个文件对这个MANUALLY进行编码。
有没有办法列出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
函数,音频文件“找到”和“可见”,但如果我使用上面的代码检查system.ResourceDirectory中的文件,那么音频文件不是发现....如果我打开项目的沙箱,我也看不到那里的音频文件......
如何列出我在日冕项目中包含的所有资产(我的情况下的音频资产)???
谢谢...
答案 0 :(得分:1)
你的问题似乎有些混乱,所以让我澄清一些事情。
system.ResourceDirectory
=项目文件的位置(即main.lua等)
system.DocumentsDirectory
=沙箱(即“显示项目沙箱”)
system.Documentsdirectory:
在Corona Simulator中,它将位于a上的沙盒文件夹中 每个应用程序的基础。您可以通过文件→查看目录/文件 显示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并确保它可以访问didFileExist。如果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