Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Requesting music files from internet

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)

Comments

  • music() doesn't play MP3s I don't think. You could try something like saveLocalData("song", data), but I don't know.

  • dave1707dave1707 Mod
    edited December 2016 Posts: 7,612

    @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("\n\nError creating/editing file " .. fileName .. "\n\nError: " .. err)
            return
        end
        wFd:write(data)
        wFd:close()
        print(fileName..".mp3\nsaved in Documents folder.")
    end
    
    
    
    
    
    
  • @dave1707 Wow it worked! Thanks alot!!

Sign In or Register to comment.