-
Notifications
You must be signed in to change notification settings - Fork 823
/
sources.dart
146 lines (133 loc) · 4.69 KB
/
sources.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import 'dart:io';
import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers_example/components/btn.dart';
import 'package:audioplayers_example/components/tab_wrapper.dart';
import 'package:audioplayers_example/components/tgl.dart';
import 'package:audioplayers_example/utils.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
const useLocalServer = bool.fromEnvironment('USE_LOCAL_SERVER');
final localhost = kIsWeb || !Platform.isAndroid ? 'localhost' : '10.0.2.2';
final host = useLocalServer ? 'http://$localhost:8080' : 'https://luan.xyz';
final _wavUrl1 = '$host/files/audio/coins.wav';
final _wavUrl2 = '$host/files/audio/laser.wav';
final _mp3Url1 = '$host/files/audio/ambient_c_motion.mp3';
final _mp3Url2 = '$host/files/audio/nasa_on_a_mission.mp3';
final _m3u8StreamUrl = useLocalServer
? '$host/files/live_streams/nasa_power_of_the_rovers.m3u8'
: 'https://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/nonuk/sbr_low/ak/bbc_radio_one.m3u8';
const _mpgaStreamUrl = 'https://timesradio.wireless.radio/stream';
const _asset1 = 'laser.wav';
const _asset2 = 'nasa_on_a_mission.mp3';
class SourcesTab extends StatefulWidget {
final AudioPlayer player;
const SourcesTab({super.key, required this.player});
@override
State<SourcesTab> createState() => _SourcesTabState();
}
enum InitMode {
setSource,
play,
}
class _SourcesTabState extends State<SourcesTab>
with AutomaticKeepAliveClientMixin<SourcesTab> {
Future<void> setSource(Source source) async {
if (initMode == InitMode.setSource) {
await widget.player.setSource(source);
toast(
'Completed setting source.',
textKey: const Key('toast-source-set'),
);
} else {
await widget.player.stop();
await widget.player.play(source);
}
}
InitMode initMode = InitMode.setSource;
@override
Widget build(BuildContext context) {
super.build(context);
return TabWrapper(
children: [
EnumTgl(
options: {for (var e in InitMode.values) 'initMode-${e.name}': e},
selected: initMode,
onChange: (InitMode m) => setState(() {
initMode = m;
}),
),
Btn(
key: const Key('setSource-url-remote-wav-1'),
txt: 'Remote URL WAV 1 - coins.wav',
onPressed: () => setSource(UrlSource(_wavUrl1)),
),
Btn(
key: const Key('setSource-url-remote-wav-2'),
txt: 'Remote URL WAV 2 - laser.wav',
onPressed: () => setSource(UrlSource(_wavUrl2)),
),
Btn(
key: const Key('setSource-url-remote-mp3-1'),
txt: 'Remote URL MP3 1 - ambient_c_motion.mp3',
onPressed: () => setSource(UrlSource(_mp3Url1)),
),
Btn(
key: const Key('setSource-url-remote-mp3-2'),
txt: 'Remote URL MP3 2 - nasa_on_a_mission.mp3',
onPressed: () => setSource(UrlSource(_mp3Url2)),
),
Btn(
key: const Key('setSource-url-remote-m3u8'),
txt: 'Remote URL M3U8 3 - BBC stream',
onPressed: () => setSource(UrlSource(_m3u8StreamUrl)),
),
Btn(
key: const Key('setSource-url-remote-mpga'),
txt: 'Remote URL MPGA 4 - Times stream',
onPressed: () => setSource(UrlSource(_mpgaStreamUrl)),
),
Btn(
key: const Key('setSource-asset-wav'),
txt: 'Asset 1 - laser.wav',
onPressed: () => setSource(AssetSource(_asset1)),
),
Btn(
key: const Key('setSource-asset-mp3'),
txt: 'Asset 2 - nasa.mp3',
onPressed: () => setSource(AssetSource(_asset2)),
),
Btn(
key: const Key('setSource-bytes-local'),
txt: 'Bytes - Local - laser.wav',
onPressed: () async {
final bytes = await AudioCache.instance.loadAsBytes(_asset1);
setSource(BytesSource(bytes));
},
),
Btn(
key: const Key('setSource-bytes-remote'),
txt: 'Bytes - Remote - ambient.mp3',
onPressed: () async {
final bytes = await readBytes(Uri.parse(_mp3Url1));
setSource(BytesSource(bytes));
},
),
Btn(
key: const Key('setSource-url-local'),
txt: 'Pick local file',
onPressed: () async {
final result = await FilePicker.platform.pickFiles();
final path = result?.files.single.path;
if (path != null) {
setSource(DeviceFileSource(path));
}
},
),
],
);
}
@override
bool get wantKeepAlive => true;
}