How do I detect if a piece of music exists?

Hello,
Either I am going crazy, or this functionality isn’t here, but is there was way to test if a piece of music exists in the asset packs? There isn’t quite a readMusic(). I supposed I might try using file IO, but a simpler way would be great.
Thank you!

Here is a simple way to check if a music pack was downloaded:

function setup()
    print(downloadedPack("A Hero's Quest"))
end

function downloadedPack(pack)
    local rt = false
    local dir = io.open(os.getenv("HOME") .. "/Library/AssetPackDownloads/" .. pack .. ".assetpack", "r")
    if dir ~= nil then 
        rt = true
        io.close(dir)
    end
    
    return rt
end

Checking an individual file is more complicated, because you must know the extension (AFAICT)

I spoke too soon, i realized that I could check all possible extensions and see if any of them exist. Here is the updated code:

function setup()
    print(musicExists("Dropbox:Amazing Plan"))
end

function downloadedPack(pack)
    local rt = false
    local dir = io.open(os.getenv("HOME") .. "/Library/AssetPackDownloads/" .. pack .. ".assetpack", "r")
    if dir ~= nil then 
        rt = true 
        io.close(dir) 
    end
    
    return rt
end

function musicExists(file)
    local pack, asset = file:match("([%a%s%p]*):([%a%s%p]*)")
    if pack ~= "Documents" and pack ~= "Dropbox" and not downloadedPack(pack) then
        return false
    end
    
    local root
    if pack == "Documents" then
        root = os.getenv("HOME") .. "/Documents/"
    elseif pack == "Dropbox" then
        root = os.getenv("HOME") .. "/Documents/Dropbox.assetpack/"
    else
        root = os.getenv("HOME") .. "/Library/AssetPackDownloads/" .. pack .. ".assetpack/"
    end
    
    local extensions = { ".m4a", ".mp3" }
    local rt = false
    for _, ext in ipairs(extensions) do
        local dir = io.open(root .. asset .. ext, "r")
        if dir ~= nil then
            rt = true
            io.close(dir)
        end
    end
    
    return rt
end

This could easily be adapted to work with sound effects if you add the necessary extensions

EDIT: Updated to support files in the Documents or Dropbox directories.

@JakAttak I though sound effects only used .wav? (You say extensions) Also, he requested a way that doesn’t use file IO.

@SkyTheCoder, he said a simpler way would be great, but this way all he has to do is call a function. Same as if it were built in. Also, sound effects use .wav or .caf and there may be other supported ones

I’ve updated my above code to support Documents and Dropbox files.

Here’s a little trick that will print an error message if you try to play music that doesn’t exist:

local mt = getmetatable(music)
local _music = mt.__call
mt.__call = function(a, file, ...)
    if musicExists(file) then
        _music(a, file, ...)
    else
        print("Music '" .. file .."' doesn't exist.")
    end
end

@JakAttak - So in short, there is no sweet alternative to file IO. Thanks for wring the code for me and saving me the hassle. :wink:

@Zoyt, pretty much. No problem, I may need this code at some point anyways.