apocalexxnow Posted July 9, 2020 Share Posted July 9, 2020 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? Quote Link to comment Share on other sites More sharing options...
b10b Posted July 9, 2020 Share Posted July 9, 2020 @apocalexxnow quick (untested) suggestion ... try "find":const sound2 = pixiSound.default.Sound.find('explosionSound'); Quote Link to comment Share on other sites More sharing options...
bigtimebuddy Posted July 9, 2020 Share Posted July 9, 2020 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. apocalexxnow 1 Quote Link to comment Share on other sites More sharing options...
apocalexxnow Posted July 10, 2020 Author Share Posted July 10, 2020 (edited) 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 July 10, 2020 by apocalexxnow Quote Link to comment Share on other sites More sharing options...
b10b Posted July 10, 2020 Share Posted July 10, 2020 There's risk of overcomplicating things. Just use "find" instead of "from" in your original code. https://pixijs.io/pixi-sound/docs/PIXI.sound.html#find Quote Link to comment Share on other sites More sharing options...
apocalexxnow Posted July 13, 2020 Author Share Posted July 13, 2020 On 7/10/2020 at 6:25 PM, b10b said: There's risk of overcomplicating things. Just use "find" instead of "from" in your original code. https://pixijs.io/pixi-sound/docs/PIXI.sound.html#find Yeah, I tried that, but it didn't work either without the proper import of the file. Thank you for trying anyway Quote Link to comment Share on other sites More sharing options...
b10b Posted July 13, 2020 Share Posted July 13, 2020 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.