How do I import audio files and play them back on runtime?

Get help using Construct 2

Post » Fri Sep 25, 2015 9:35 pm

Hi Community,

I am working on a Quiz project that needs to work with 1000's of sounds (i will not be able to import all these sounds for obvious memory and loading time reasons). Since this quiz game will be played on iOS devices, the "streaming audio plugin" or "play by url"option will not work due to the playback delay caused by safari when you stream outside audio. My best option would be to first import select sounds ON RUNTIME AND INTO the game and play them as standard imported local audio.

@Ashley, @newt, How would i be able to achieve this?

Thank you in advanced for the assistance.
B
107
S
20
G
8
Posts: 155
Reputation: 11,752

Post » Fri Sep 25, 2015 10:28 pm

Unless the file sizes are miniscule "thousands" really won't be feasible.
Pode had a base64 plug, to load sounds from string, but the compression is not as good as the standards, and the plug was very basic.
Image ImageImage
B
172
S
50
G
184
Posts: 8,444
Reputation: 116,107

Post » Fri Sep 25, 2015 10:46 pm

Animate wrote:Hi Community,

I am working on a Quiz project that needs to work with 1000's of sounds (i will not be able to import all these sounds for obvious memory and loading time reasons). Since this quiz game will be played on iOS devices, the "streaming audio plugin" or "play by url"option will not work due to the playback delay caused by safari when you stream outside audio. My best option would be to first import select sounds ON RUNTIME AND INTO the game and play them as standard imported local audio.

@Ashley, @newt, How would i be able to achieve this?

Thank you in advanced for the assistance.

There are no good way to do this as far as I know as you can't access the source of audio object for some weird reasons. So you need to make some workarounds to make it work.

The only way I have figured out to do this, is to setup a variable path in the audio object and then you use this as the path to your audio files.

However since the source requires a non extension path meaning the file can't be "sound.ogg" for instant, as C2 wont accept that it needs to be just "sound". So you need to make a functionality that extract the filename without the extension and then you add it together with the rest of the path so you get the complete path to the file.

So the source when you are done should look like this:

Code: Select all
Path = "C:\Sounds\"  <--- Variable where you store the path to the files.
Filename = "Sound1"  <--- This being the filename with the extension removed.

In the Audio object source you add it together:
Source: Path & Filename   <--- Read as (C:\Sounds\Sound1)
 


So every time you want to load an audio file you have to update these two variable or just add it all in one depending on what you prefer. Its not really elegant but can't see any other way to do it.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Mon Sep 28, 2015 1:10 pm

Just import them all to C2, and set "Preload sounds" to "No". It won't download or decode any of the sounds on startup, it will load them on demand as you play them for the first time.
Scirra Founder
B
403
S
238
G
89
Posts: 24,659
Reputation: 196,165

Post » Mon Sep 28, 2015 5:58 pm

@Ashley, the issue with this is that there over 10,000's sounds that sit on a private server. Currently the audio files are dynamically called to the game through an XML and played through the "audiostream" extension, which works by playing the audio through an URL, but currently with very bad lag on iOS devices.

Is there a way that i can grab selected audio files through this XML and import them to the game (maybe through the invoke download action) in order to play them just like normal audio files?

Is this possible?
B
107
S
20
G
8
Posts: 155
Reputation: 11,752

Post » Mon Sep 28, 2015 6:51 pm

@nimos100, where can i find the audio object source in order to change the path?
B
107
S
20
G
8
Posts: 155
Reputation: 11,752

Post » Mon Sep 28, 2015 7:22 pm

@Kyatric, is it possible to change the path of the sounds folder to another location (hosted in a server) in order to grab and play the audio files from here?

If not, how can i import audio files on runtime from a server location and play them on demand?

Many thanks for your continued assistance in these forums.
B
107
S
20
G
8
Posts: 155
Reputation: 11,752

Post » Mon Sep 28, 2015 7:55 pm

Animate wrote:@nimos100, where can i find the audio object source in order to change the path?

You can't that's the whole problem. So you have to make a "fictional" source to bypass it. That's why I wrote in the original post, that its a mystery why you can't access the source of an audio object, because it should be fairly easy to do.

But here is how you bypass it:

Image

The folder setting can be ignored, so doesn't matter if its "Sounds" or "Music" so don't worry about that.

Notice that in the variable Audio_filename = "Sound_effect.ogg", it actually need to be "Audio_filename = "Sound_effect" otherwise it wont work. I just added the ".ogg" so you could see it was the filename. Since you can't load files without the extension you will always get the filenames with it, so you have to extract just the filename from you sound files which you can do with Tokencount and Tokenat if the files are in different locations, or just with tokenat if all the sounds are in the same folder.

Since you say you have 10000 sounds, you of course have to add some functionality that can automate this for you. So you have to update the variables rather than trying to change the source of the audio object. But again its not a elegant solution, but the only way I have made it work.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Mon Sep 28, 2015 9:28 pm

@nimos100, I see that your new audio path points to your local C: drive. Will this solution work for a folder hosted online?

Thanks for your help...
B
107
S
20
G
8
Posts: 155
Reputation: 11,752

Post » Mon Sep 28, 2015 9:40 pm

Animate wrote:@nimos100, I see that your new audio path points to your local C: drive. Will this solution work for a folder hosted online?

Thanks for your help...

Not sure, so you would have to try to see if you can make the url so the filename can be added without the extension as that seems to be the important part when it comes to the source. But whether it will actually play it, even if you can get the filename and it will accept an non "<drive:>" format, is of course uncertain, as im pretty sure it weren't designed to work the way I did it in the first place, reading how its suppose to be used.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 25 guests