Skip to content

Commit 21b6926

Browse files
authored
Merge pull request #33 from podaen/main
Replaces #32
2 parents ec4a873 + 7bbf2b9 commit 21b6926

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

src/AudioTools/AudioPlayer.h

Lines changed: 52 additions & 25 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
@@ -261,47 +273,58 @@ namespace audio_tools {
261273
/// Setup Wifi URL
262274
virtual void begin() {
263275
LOGD(LOG_METHOD);
264-
setPos(-1);
265-
}
266-
267-
/// Defines the actual position
268-
void setPos(int newPos) {
269-
pos = newPos;
270-
if (pos >= max || pos < 0) {
271-
pos = -1;
272-
}
273276
}
274277

275278
/// Opens the next url from the array
276279
Stream* nextStream(int offset) {
277280
pos += offset;
278-
if (pos < 0 || pos >= max) {
279-
pos = 0;
281+
if (pos < 1 || pos > max) {
282+
pos = 1;
280283
}
281-
LOGI("nextStream: %d -> %s", pos, urlArray[pos]);
284+
LOGI("nextStream: %d -> %s", String(pos) + "/" + String(max), urlArray[pos-1]);
282285
if (offset != 0 || actual_stream == nullptr) {
283286
if (started) actual_stream->end();
284-
actual_stream->begin(urlArray[pos], mime);
287+
actual_stream->begin(urlArray[pos-1], mime);
285288
started = true;
286289
}
287290
return actual_stream;
288291
}
289-
/// Opens the next url from the array
292+
293+
/// Opens the selected url from the array
290294
Stream* selectStream(int Station) {
291295
//pos += offset;
292296
pos = Station;
293-
if (pos < 0 || pos >= max) {
294-
pos = 0;
297+
if (pos < 1) {
298+
pos = 1;
299+
LOGI("url array out of limits: %d -> %d", Station, pos);
295300
}
296-
LOGI("nextStream: %d -> %s", pos, urlArray[pos]);
301+
if (pos > max) {
302+
pos = max;
303+
LOGI("url array out of limits: %d -> %d", Station, pos);
304+
}
305+
LOGI("selectStream: %s -> %s", String(pos) + "/" + String(max), urlArray[pos-1]);
297306
if (Station != 0 || actual_stream == nullptr) {
298307
if (started) actual_stream->end();
299-
actual_stream->begin(urlArray[pos], mime);
308+
actual_stream->begin(urlArray[pos-1], mime);
300309
started = true;
301310
}
302311
return actual_stream;
303312
}
304313

314+
/// Opens the Previous url from the array
315+
Stream* previousStream(int offset) {
316+
pos -= offset;
317+
if (pos < 1 || pos > max) {
318+
pos = max;
319+
}
320+
LOGI("previousStream: %s -> %s", String(pos) + "/" + String(max), urlArray[pos-1]);
321+
if (offset != 0 || actual_stream == nullptr) {
322+
if (started) actual_stream->end();
323+
actual_stream->begin(urlArray[pos-1], mime);
324+
started = true;
325+
}
326+
return actual_stream;
327+
}
305328

306329
void setTimeoutMs(int millisec) {
307330
timeout = millisec;
@@ -445,7 +468,7 @@ namespace audio_tools {
445468
virtual bool begin(bool isActive = true) {
446469
LOGD(LOG_METHOD);
447470
bool result = false;
448-
471+
449472
// start dependent objects
450473
p_out_decoding->begin();
451474
p_source->begin();
@@ -532,13 +555,20 @@ namespace audio_tools {
532555
return active;
533556
}
534557

535-
/// moves to next file
558+
/// moves to selected file
536559
virtual bool setIndex(int idx) {
537560
LOGD(LOG_METHOD);
538561
active = setStream(*(p_source->selectStream(idx)));
539562
return active;
540563
}
541564

565+
/// moves to previous file
566+
virtual bool previous() {
567+
LOGD(LOG_METHOD);
568+
active = setStream(*(p_source->previousStream(+1)));
569+
return active;
570+
}
571+
542572
/// start selected input stream
543573
virtual bool setStream(Stream &input) {
544574
end();
@@ -553,7 +583,6 @@ namespace audio_tools {
553583
return p_input_stream != nullptr;
554584
}
555585

556-
557586
/// determines if the player is active
558587
virtual bool isActive() {
559588
return active;
@@ -611,7 +640,6 @@ namespace audio_tools {
611640
volume_out.setVolumeControl(vc);
612641
}
613642

614-
615643
protected:
616644
bool active = false;
617645
AudioSource* p_source = nullptr;
@@ -628,7 +656,6 @@ namespace audio_tools {
628656
uint32_t timeout = 0;
629657
float current_volume = -1; // illegal value which will trigger an update
630658

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

0 commit comments

Comments
 (0)