Jump to content

pixi sound preloading - what am I doing wrong?


apocalexxnow
 Share

Recommended Posts

Hi :)

I am trying to preload a sound and then play it, but I'm failing miserably and I don't know what I'm missing. Can you help me?

import * as pixiSound from 'pixi-sound'

export class Resources {
    public load() {
        const pixiLoader = new PIXI.loaders.Loader()
        pixiLoader.add('explosionSound', './assets/sounds/Explosion.mp3')
        pixiLoader.on('complete', () => {
            const sound = pixiSound.default.Sound.from('./assets/sounds/Explosion.mp3')
            const sound2 = pixiSound.default.Sound.from('explosionSound'); // DOES NOT!
            sound.play() // The sound plays, but it's downloaded from the url, it's not the preloaded one
            sound2.play() // GET 404 Not found error
        })
        pixiLoader.load()

Meanwhile, using the Loader in the exact same way to preload Textures for Sprite creation works perfectly. What could I be missing?

Link to comment
Share on other sites

I see the confusion. Sound.from isn't exactly analogous to Texture.from. Sound.from API only handles remote URLs.

If you want to play a sound that's already been loaded to this:

pixiSound.default.play('explosionSound')

Here's the documentation: https://pixijs.io/pixi-sound/docs/PIXI.sound.html

Keep in mind that pixiSound.default is the same as PIXI.sound in the docs.

Link to comment
Share on other sites

OMG, thanks bigtimebuddy :) You were right about my confusion. Now i do

pixiLoader.add(
            pixiSound.default.add('explosionSound'pixiSound.default.Sound.from('./assets/sounds/Explosion.mp3'))
        )

and from then on I can always play

pixiSound.default.play('explosionSound'

Do you think that is enough for the sounds to be preloaded and for the game to start only after all sounds have been worked like that?

Edited by apocalexxnow
Link to comment
Share on other sites

4 hours ago, apocalexxnow said:

Yeah, I tried that, but it didn't work either without the proper import of the file. Thank you for trying anyway :)

Thanks for reply.  Curious ... find() works well for me - I add the audio files to the loader queue (as with any other asset file, no special treatment).  Then use PIXI.sound.find() to retrieve them (which plays them from the preloaded asset as expected).  I'm loading the libraries as self contained js in the html (rather than using imports) so perhaps this reveals an issue in the loader middleware / initialisation for your scenario?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...