2

I need to load an mp3 file from an URL in Lua.

I've tried this but it doesn't work.

require "socket.http"

local resp, stat, hdr = socket.http.request{
  url     = "https://www.dropbox.com/s/hfrdbncfgbsarou/hello.mp3?dl=1",
}

local audioFile = audio.loadSound(resp)
audio.play(audioFile)

Any ideas?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

1 Answers1

1

The request function is "overloaded" (in the terminology of other languages). As detailed in the documentation, it has three signatures:

local responsebodystring, statusnumber, headertable, statusstring 
  = request( urlstring ) -- GET

local responsebodystring, statusnumber, headertable, statusstring 
  = request( urlstring, requestbodystring ) -- POST

local success, statusnumber, headertable, statusstring 
  = request( requestparametertable ) -- depends on parameters

See the documentation for details, especially concerning error results.

For the last form, Lua syntax allow a function to be called with a table constructor instead of a single table parameter in parentheses. That's the form and syntax you are using. But, you incorrectly expect the first return value to be a response body. The response body is passed to a "sink" function optionally indicated in the request parameter table, which you don't have.

Try the first form:

local resp, stat, hdr 
  = socket.http.request("https://www.dropbox.com/s/hfrdbncfgbsarou/hello.mp3?dl=1")
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72