@@ -45,6 +45,9 @@ namespace audio_tools {
45
45
return nullptr ;
46
46
}
47
47
48
+ // / Returns previous audio stream
49
+ virtual Stream* previousStream (int offset) = 0;
50
+
48
51
// / Provides the timeout which is triggering to move to the next stream
49
52
virtual int timeoutMs () {
50
53
return 500 ;
@@ -80,11 +83,16 @@ namespace audio_tools {
80
83
return nextStreamCallback == nullptr ? nullptr : nextStreamCallback ();
81
84
}
82
85
83
- // / Returns next audio stream
86
+ // / Returns selected audio stream
84
87
virtual Stream* selectStream (int index) {
85
88
return indexStreamCallback == nullptr ? nullptr : indexStreamCallback (index);
86
89
}
87
90
91
+ // / Returns previous stream
92
+ virtual Stream* previousStream (int offset) {
93
+ LOGD (LOG_METHOD);
94
+ return previousStreamCallback == nullptr ? nullptr : previousStreamCallback ();
95
+ }
88
96
89
97
void setCallbackOnStart (void (*callback)()) {
90
98
onStartCallback = callback;
@@ -98,11 +106,15 @@ namespace audio_tools {
98
106
indexStreamCallback = callback;
99
107
}
100
108
109
+ void setCallbackPreviousStream (Stream* (*callback)()) {
110
+ previousStreamCallback = callback;
111
+ }
112
+
101
113
protected:
102
114
void (*onStartCallback)() = nullptr ;
103
115
Stream* (*nextStreamCallback)() = nullptr ;
104
116
Stream* (*indexStreamCallback)(int index) = nullptr ;
105
-
117
+ Stream* (*previousStreamCallback)() = nullptr ;
106
118
};
107
119
108
120
#ifdef USE_SDFAT
@@ -261,47 +273,58 @@ namespace audio_tools {
261
273
// / Setup Wifi URL
262
274
virtual void begin () {
263
275
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
- }
273
276
}
274
277
275
278
// / Opens the next url from the array
276
279
Stream* nextStream (int offset) {
277
280
pos += offset;
278
- if (pos < 0 || pos >= max) {
279
- pos = 0 ;
281
+ if (pos < 1 || pos > max) {
282
+ pos = 1 ;
280
283
}
281
- LOGI (" nextStream: %d -> %s" , pos, urlArray[pos]);
284
+ LOGI (" nextStream: %d -> %s" , String ( pos) + " / " + String (max) , urlArray[pos- 1 ]);
282
285
if (offset != 0 || actual_stream == nullptr ) {
283
286
if (started) actual_stream->end ();
284
- actual_stream->begin (urlArray[pos], mime);
287
+ actual_stream->begin (urlArray[pos- 1 ], mime);
285
288
started = true ;
286
289
}
287
290
return actual_stream;
288
291
}
289
- // / Opens the next url from the array
292
+
293
+ // / Opens the selected url from the array
290
294
Stream* selectStream (int Station) {
291
295
// pos += offset;
292
296
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);
295
300
}
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 ]);
297
306
if (Station != 0 || actual_stream == nullptr ) {
298
307
if (started) actual_stream->end ();
299
- actual_stream->begin (urlArray[pos], mime);
308
+ actual_stream->begin (urlArray[pos- 1 ], mime);
300
309
started = true ;
301
310
}
302
311
return actual_stream;
303
312
}
304
313
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
+ }
305
328
306
329
void setTimeoutMs (int millisec) {
307
330
timeout = millisec;
@@ -445,7 +468,7 @@ namespace audio_tools {
445
468
virtual bool begin (bool isActive = true ) {
446
469
LOGD (LOG_METHOD);
447
470
bool result = false ;
448
-
471
+
449
472
// start dependent objects
450
473
p_out_decoding->begin ();
451
474
p_source->begin ();
@@ -532,13 +555,20 @@ namespace audio_tools {
532
555
return active;
533
556
}
534
557
535
- // / moves to next file
558
+ // / moves to selected file
536
559
virtual bool setIndex (int idx) {
537
560
LOGD (LOG_METHOD);
538
561
active = setStream (*(p_source->selectStream (idx)));
539
562
return active;
540
563
}
541
564
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
+
542
572
// / start selected input stream
543
573
virtual bool setStream (Stream &input) {
544
574
end ();
@@ -553,7 +583,6 @@ namespace audio_tools {
553
583
return p_input_stream != nullptr ;
554
584
}
555
585
556
-
557
586
// / determines if the player is active
558
587
virtual bool isActive () {
559
588
return active;
@@ -611,7 +640,6 @@ namespace audio_tools {
611
640
volume_out.setVolumeControl (vc);
612
641
}
613
642
614
-
615
643
protected:
616
644
bool active = false ;
617
645
AudioSource* p_source = nullptr ;
@@ -628,7 +656,6 @@ namespace audio_tools {
628
656
uint32_t timeout = 0 ;
629
657
float current_volume = -1 ; // illegal value which will trigger an update
630
658
631
-
632
659
// / Callback implementation which writes to metadata
633
660
static void decodeMetaData (void * obj, void * data, size_t len) {
634
661
LOGD (LOG_METHOD);
0 commit comments