I am making a simple audio player for Playdate to play some of my favorite songs. I wanted to add a progress bar but I can't figure out how to get progress through audio. I tried getOffset
but that just always returned 4.
fileplayer:getOffset() is indeed the way. I use it in my games, most recently today.
It's difficult to say what you're doing wrong but I'd encourage you to post some code or try again.
Yeah Here's the code for now. It keeps outputting negative values until I start a song then it constantly says 4. The audio files are in the Songs
folder and use the .wav.pda
extension because of the way I converted them.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pd_api.h"
static PlaydateAPI *pd = NULL;
const char *fontpath = "/System/Fonts/Asheville-Sans-14-Bold.pft";
LCDFont *font = NULL;
void setPDPtr(PlaydateAPI *p) { pd = p; }
FilePlayer *player;
const char *songs[100] = {};
int fileIndex = 0;
int selectedSong = 0;
static int update(void *userdata);
static void redraw();
static void addSong(const char *file, void *userdata);
static void onSongEnd(SoundSource *c, void *userdata);
static int handleButtons(PDButtons button, int down, uint32_t when,
void *userdata);
#ifdef _WINDLL
__declspec(dllexport)
#endif
int eventHandler(PlaydateAPI *pd, PDSystemEvent event, uint32_t arg) {
(void)arg;
setPDPtr(pd);
if (event == kEventInit) {
const char *err;
font = pd->graphics->loadFont(fontpath, &err);
if (font == NULL)
pd->system->error("%s:%i Couldn't load font %s: %s", __FILE__, __LINE__,
fontpath, err);
player = pd->sound->fileplayer->newPlayer();
pd->sound->fileplayer->setFinishCallback(player, onSongEnd, pd);
pd->file->listfiles("Songs", addSong, pd, 1);
pd->system->setAutoLockDisabled(1);
pd->system->setButtonCallback(handleButtons, pd, 5);
pd->system->setUpdateCallback(update, pd);
redraw();
}
if (event == kEventTerminate) {
pd->sound->fileplayer->freePlayer(player);
}
return 0;
}
static int update(void *userdata) {
pd->system->logToConsole("%d", pd->sound->fileplayer->getOffset(player));
return 1;
}
static void redraw() {
pd->graphics->clear(kColorWhite);
pd->graphics->setFont(font);
pd->graphics->drawText(
songs[selectedSong], strlen(songs[selectedSong]), kASCIIEncoding,
(pd->display->getWidth() -
pd->graphics->getTextWidth(font, songs[selectedSong],
strlen(songs[selectedSong]), kASCIIEncoding,
0)) /
2,
10);
}
static void addSong(const char *file, void *userdata) {
PlaydateAPI *pd = userdata;
char *song = strdup(file);
song[strlen(song) - 8] = '\0';
songs[fileIndex] = song;
if (fileIndex == 0) {
char song[500] = "Songs/";
strcat(song, file);
pd->sound->fileplayer->loadIntoPlayer(player, song);
}
fileIndex++;
}
static int handleButtons(PDButtons button, int down, uint32_t when,
void *userdata) {
PlaydateAPI *pd = userdata;
if (down == 0)
return 0;
if (button == kButtonRight) {
pd->sound->fileplayer->stop(player);
selectedSong++;
if (selectedSong == fileIndex)
selectedSong = 0;
char song[500] = "Songs/";
strcat(song, songs[selectedSong]);
strcat(song, ".wav.pda");
pd->sound->fileplayer->loadIntoPlayer(player, song);
redraw();
return 0;
}
if (button == kButtonLeft) {
pd->sound->fileplayer->stop(player);
selectedSong--;
if (selectedSong < 0)
selectedSong = fileIndex - 1;
char song[500] = "Songs/";
strcat(song, songs[selectedSong]);
strcat(song, ".wav.pda");
pd->sound->fileplayer->loadIntoPlayer(player, song);
redraw();
return 0;
}
if (button == kButtonA) {
if (pd->sound->fileplayer->isPlaying(player)) {
pd->sound->fileplayer->pause(player);
return 0;
}
pd->sound->fileplayer->play(player, 1);
return 0;
}
if (button == kButtonB)
pd->sound->fileplayer->stop(player);
return 0;
}
static void onSongEnd(SoundSource *c, void *userdata) {
PlaydateAPI *pd = userdata;
selectedSong++;
if (selectedSong == fileIndex)
selectedSong = 0;
pd->sound->fileplayer->stop(player);
char song[500] = "Songs/";
strcat(song, songs[selectedSong]);
strcat(song, ".wav.pda");
pd->sound->fileplayer->loadIntoPlayer(player, song);
pd->sound->fileplayer->play(player, 1);
}
I wonder if it has something to do with the .wav.pda
thing and that I should rename it to just .pda
.
I tried renaming them to .pda
files but it made no difference. I'm still having the same problem.
Sadly, I only use Lua ...so I can't personally help any further.
I guess you could put together a quick example in Lua to confirm it's not the file?
I don't know why but outputting to screen as text and console gave different outputs but when I plugged it into some code that renders a rectangle symbolizing progress it works. I think this might be some kind of sdk bug so I might create a post over there as well. Drawing as a rect was what I was eventually trying to do so that is now fixed for my use cases but this still seems to be some kind of bug.