Skip to content

Commit 8a3a7ef

Browse files
committed
Callback add
1 parent ec4a873 commit 8a3a7ef

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

src/AudioTools/AudioPlayer.h

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ namespace audio_tools {
4545
return nullptr;
4646
}
4747

48+
/// Returns previous audio stream
49+
virtual Stream* previousStream(int offset) = 0;
50+
4851
/// Provides the timeout which is triggering to move to the next stream
4952
virtual int timeoutMs() {
5053
return 500;
@@ -80,11 +83,16 @@ namespace audio_tools {
8083
return nextStreamCallback == nullptr ? nullptr : nextStreamCallback();
8184
}
8285

83-
/// Returns next audio stream
86+
/// Returns selected audio stream
8487
virtual Stream* selectStream(int index) {
8588
return indexStreamCallback == nullptr ? nullptr : indexStreamCallback(index);
8689
}
8790

91+
/// Returns previous stream
92+
virtual Stream* previousStream(int offset) {
93+
LOGD(LOG_METHOD);
94+
return previousStreamCallback == nullptr ? nullptr : previousStreamCallback();
95+
}
8896

8997
void setCallbackOnStart(void (*callback)()) {
9098
onStartCallback = callback;
@@ -98,11 +106,15 @@ namespace audio_tools {
98106
indexStreamCallback = callback;
99107
}
100108

109+
void setCallbackPreviousStream(Stream* (*callback)()) {
110+
previousStreamCallback = callback;
111+
}
112+
101113
protected:
102114
void (*onStartCallback)() = nullptr;
103115
Stream* (*nextStreamCallback)() = nullptr;
104116
Stream* (*indexStreamCallback)(int index) = nullptr;
105-
117+
Stream* (*previousStreamCallback)() = nullptr;
106118
};
107119

108120
#ifdef USE_SDFAT
@@ -286,14 +298,20 @@ namespace audio_tools {
286298
}
287299
return actual_stream;
288300
}
289-
/// Opens the next url from the array
301+
302+
/// Opens the selected url from the array
290303
Stream* selectStream(int Station) {
291304
//pos += offset;
292305
pos = Station;
293-
if (pos < 0 || pos >= max) {
306+
if (pos < 0) {
294307
pos = 0;
308+
LOGI("array out of limits: %d -> %s", Station, pos);
295309
}
296-
LOGI("nextStream: %d -> %s", pos, urlArray[pos]);
310+
if (pos >= max) {
311+
pos = max-1;
312+
LOGI("array out of limits: %d -> %s", Station, pos);
313+
}
314+
LOGI("selectStream: %d -> %s", pos, urlArray[pos]);
297315
if (Station != 0 || actual_stream == nullptr) {
298316
if (started) actual_stream->end();
299317
actual_stream->begin(urlArray[pos], mime);
@@ -302,6 +320,20 @@ namespace audio_tools {
302320
return actual_stream;
303321
}
304322

323+
/// Opens the Previous url from the array
324+
Stream* previousStream(int offset) {
325+
pos -= offset;
326+
if (pos < 0 || pos >= max) {
327+
pos = max - 1;
328+
}
329+
LOGI("previousStream: %d -> %s", pos, urlArray[pos]);
330+
if (offset != 0 || actual_stream == nullptr) {
331+
if (started) actual_stream->end();
332+
actual_stream->begin(urlArray[pos], mime);
333+
started = true;
334+
}
335+
return actual_stream;
336+
}
305337

306338
void setTimeoutMs(int millisec) {
307339
timeout = millisec;
@@ -532,13 +564,20 @@ namespace audio_tools {
532564
return active;
533565
}
534566

535-
/// moves to next file
567+
/// moves to selected file
536568
virtual bool setIndex(int idx) {
537569
LOGD(LOG_METHOD);
538570
active = setStream(*(p_source->selectStream(idx)));
539571
return active;
540572
}
541573

574+
/// moves to previous file
575+
virtual bool previous() {
576+
LOGD(LOG_METHOD);
577+
active = setStream(*(p_source->previousStream(+1)));
578+
return active;
579+
}
580+
542581
/// start selected input stream
543582
virtual bool setStream(Stream &input) {
544583
end();
@@ -553,7 +592,6 @@ namespace audio_tools {
553592
return p_input_stream != nullptr;
554593
}
555594

556-
557595
/// determines if the player is active
558596
virtual bool isActive() {
559597
return active;
@@ -611,7 +649,6 @@ namespace audio_tools {
611649
volume_out.setVolumeControl(vc);
612650
}
613651

614-
615652
protected:
616653
bool active = false;
617654
AudioSource* p_source = nullptr;
@@ -628,7 +665,6 @@ namespace audio_tools {
628665
uint32_t timeout = 0;
629666
float current_volume = -1; // illegal value which will trigger an update
630667

631-
632668
/// Callback implementation which writes to metadata
633669
static void decodeMetaData(void* obj, void* data, size_t len) {
634670
LOGD(LOG_METHOD);

0 commit comments

Comments
 (0)