Playing sounds using C API

I am trying to play sounds using the C API.

However, I don't understand how it works.

This is a code showing what I am doing:

    FilePlayer* fp = pd->sound->fileplayer->newPlayer();
    pd->sound->fileplayer->loadIntoPlayer(fp, path);
    pd->system->logToConsole("%f", pd->sound->fileplayer->getLength(fp)); 
    pd->sound->fileplayer->play(fp, 1);

I make a new FilePlayer.
I then load in the audio using loadIntoPlayer.

I couldn't find in the docs how to check if the audio loaded correctly, are there any way to check this?
I tried to check by logging the length of the audio, but it only outputs: 0.000000

When I try to play, nothing happens.

Is this the correct way to load audio using the C API, or am I missing something?

Anyone know how to do this? Still stuck

Sorry for the lag! Still catching up here.. There's a new behavior in 1.9.3 where we're waiting to open the file until absolutely necessary, to avoid the case where we'd be doing a bunch of disk i/o when setting up the fileplayers. getLength() is returning 0 there because it hasn't looked at the file yet. That's not the intended behavior--but it's an easy fix, we just need to prepare the file when getLength is called. As a workaround until we push an update with the fix you can call pd->sound->fileplayer->setBufferSize(fp, 0.25), which opens the file in order to pre-load a buffer full of output data.

I'm not sure why the file isn't playing, though. My guess is that the file doesn't exist. Because of the delayed opening getLength() also returns 0 in that case instead of an error code. I've fixed that to return -1. And it looks like play() also returns 1 if the file doesn't exist, even though the underlying function now has an error return. I'll fix that, too.

2 Likes