Help adding sound

I know l sound kind of like a noob, but im having troubles adding sound into a game im making. Well not really a game more like a joke. But anyway, when the Inside Playdate document said playdate.sound.fileplayer:play([repeatCount]) how do you add repeatCount? It will not accept nothing, but i just dont know how to do it.
Thanks in advance!

So in theory you just have to load your sound when you game starts or your level load with
mySound = playdate.sound.fileplayer.new( "sound_file" )

And when you want to trigger the sound you just call
mySound:play()

And if you want to repeat the sound two times
mySound:play( 2 )

Does that help? If not you could share the error you get and the code that trigger the sound, that would help us figuring out the problem.

Thanks, ill try that.

The console says this: main.lua:4: attempt to index a nil value (global 'mySound')

stack traceback:

main.lua:4: in function main.lua:1

Never mind. Got it to work

1 Like

HOW ?! Please explain I'm getting it wrong !

Well actually I just removed .wav from the path and it worked... weird -_-

1 Like

How did you fix that, mine still doesn't work, thank you

Let's say you have a sound named "sound_file.wav" in your Source folder:

MyGame/
  Source/
    main.lua
    pdxinfo
    sound_file.wav

When you compile your game, the resulting pdx file will contain:

MyGame.pdx/
    main.pdz
    pdxinfo
    sound_file.pda

You can see that the compiler changes the extension of sound files to .pda.
So to load a sound, you have to write the file name without the extension because the SDK handle the extension change by adding .pda the the given file name:

local mySound = playdate.sound.fileplayer.new("sound_file")

And then you should be able to use mySound:play() to play the sound.

If this does not work for you, you should check:

  • Does your .pdx contains your sound? Maybe you didn't place it in the right folder, it should be in the same folder as your main.lua file or a in a subfolder.
  • How do you load the file? Maybe you have a problem in your variable handling or loading code?
2 Likes

Thank you for your help, now everything works how I wanted. I'm currently facing an issue where I want to play a sound each time an enemy is eliminated. However, as the number of enemies increases, the sound doesn't overlap. It only plays after the previous one has finished. Maybe you know how to fix that and thanl you again!

You have to use multiple sample players to play multiple sounds at the same time.

You can load your sound once with playdate.sound.sample.new(path), create a sample player for each enemy using playdate.sound.sampleplayer.new(sample) and call play() on the sample player of the destroyed enemy.

1 Like

You're crazy man, ty

1 Like