I’m wondering how can I retrieve a music file from the internet to use in a game?
I tried something like this but did nothing
http.request("http://www.magicmusicx.com/uploads/4/2/7/2/4272634/16._lovin_on_dire_docks.mp3", function(data, status, headers) music(data) end)
music() doesn’t play MP3s I don’t think. You could try something like saveLocalData(“song”, data), but I don’t know.
@NewToonie Try this code. It will read the file you give in tune
and then save it in the Documents folder as the name you put in mp3Name
. It takes about 22 seconds to download your mp3 file and then will start to play. It’s in the Documents folder where you can use it whenever you want. Remember to change the mp3Name or the file will overwrite a previous file.
function setup()
-- change mp3Name to what you want, it will be saved in the Documents folder.
mp3Name="lovin"
-- change tune to the mp3 file you want to download
tune="http://www.magicmusicx.com/uploads/4/2/7/2/4272634/16._lovin_on_dire_docks.mp3"
print("reading mp3 file: "..mp3Name)
http.request(tune,didLoadMusic)
end
function didLoadMusic(data,status,headers)
writeFile(mp3Name,data)
music("Documents:"..mp3Name)
end
function writeFile(fileName, data)
local file = os.getenv("HOME") .. "/Documents/" .. fileName..".mp3"
wFd, err = io.open(file, "w")
if wFd == nil then
error("\
\
Error creating/editing file " .. fileName .. "\
\
Error: " .. err)
return
end
wFd:write(data)
wFd:close()
print(fileName..".mp3\
saved in Documents folder.")
end
@dave1707 Wow it worked! Thanks alot!!