Play url-loaded sound

To jump to the end, the actual problem I am trying to solve is getting text-to-speech, and am exploring using Amazon Polly or Google translate.

But the specific problem right now is how to get a loaded sound file onto a game object so that it can be played. I can load the file (sys.get_save_file, http.request, io.open(,“wb”), <>:write()) and the loaded version plays from the file system. (Image fetch works as well.) But I cannot figure out how to attach that file path into a game object to which I can send the message “play_sound” (either changing sound file or using a factory). (I will have the same problem with images loaded from URL, a key feature of many of my apps.) I assume I cannot save the files directly into my ‘images’ or ‘audio’ directory, right (and even then, how to insert images into the right atlas)?

What am I missing? Isn’t there a way to use dynamically-fetched sounds and images?

For texture you would use resource.set_texture(). For sound you probably can use sys.load_resource() and my OpenAL extension.

1 Like

Thanks, I will play with that, though all the examples show project paths, not absolute paths. I don’t know how to make it work with system file paths.

When I try this:
local cache_path = sys.get_save_file(sys.get_config(“project.title”), params.fname)
[… load http GET sound file into that path, which works fine…]
local datfile = sys.load_resource(cache_path)

I get this result:
DEBUG:SCRIPT: cache_path: /Users/[username]/Library/Application Support/Colors/cheering.wav
WARNING:SCRIPT: Failed to load resource: /Users/[username]/Library/Application Support/Colors/cheering.wav (-3)

The problem with set_texture() seems to be that you have to know the image before loading it, width and height. I have always done the opposite – load the resource, and then can read the width and height (for optimal scaling and preserving aspect ratio). I am not sure this will get me there, but I’ll keep playing with it.

For instance, you can open the image with io.open() and read it’s width/height. You will need a Lua lib for that.

Ah, of course. I had forgotten to look for that, always used the game framework built-ins. Thanks, just what I need, I’ll track it down.

You can use image.load() to load an image and get its dimensions. The loaded data can then be used to create a new texture and assign it to a gui node (and adjust the node size to match). Full example here: https://github.com/britzl/publicexamples/tree/master/examples/loadtexture

Or you can use Defold-PNG to load (and save) PNG images and set them as textures on sprites.

2 Likes

Images = Yes, see my other answer
Sound = No built in solution but you can use the OpenAL extension

1 Like

Thank you, perfect, I should have seen that. I had incorporated a Lua solution, which worked well for PNG files, not so well for jpg, but this works great for both, thanks.

1 Like