-
Notifications
You must be signed in to change notification settings - Fork 823
/
streams.dart
158 lines (143 loc) · 4.11 KB
/
streams.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
147
148
149
150
151
152
153
154
155
156
157
158
import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers_example/components/btn.dart';
import 'package:audioplayers_example/components/pad.dart';
import 'package:audioplayers_example/components/player_widget.dart';
import 'package:audioplayers_example/components/tab_wrapper.dart';
import 'package:flutter/material.dart';
class StreamsTab extends StatefulWidget {
final AudioPlayer player;
const StreamsTab({super.key, required this.player});
@override
State<StreamsTab> createState() => _StreamsTabState();
}
class _StreamsTabState extends State<StreamsTab>
with AutomaticKeepAliveClientMixin<StreamsTab> {
Duration? position, duration;
PlayerState? state;
Source? source;
late List<StreamSubscription> streams;
Duration? streamDuration, streamPosition;
PlayerState? streamState;
@override
void initState() {
super.initState();
streams = <StreamSubscription>[
widget.player.onDurationChanged
.listen((it) => setState(() => streamDuration = it)),
widget.player.onPlayerStateChanged
.listen((it) => setState(() => streamState = it)),
widget.player.onPositionChanged
.listen((it) => setState(() => streamPosition = it)),
];
}
@override
void setState(VoidCallback fn) {
// Subscriptions only can be closed asynchronously,
// therefore events can occur after widget has been disposed.
if (mounted) {
super.setState(fn);
}
}
@override
void dispose() {
super.dispose();
streams.forEach((it) => it.cancel());
}
Future<void> getPosition() async {
final position = await widget.player.getCurrentPosition();
setState(() => this.position = position);
}
Future<void> getDuration() async {
final duration = await widget.player.getDuration();
setState(() => this.duration = duration);
}
Future<void> getPlayerState() async {
setState(() => state = widget.player.state);
}
Future<void> getSource() async {
setState(() => source = widget.player.source);
}
@override
Widget build(BuildContext context) {
super.build(context);
return TabWrapper(
children: [
Row(
children: [
Btn(
key: const Key('getDuration'),
txt: 'Get Duration',
onPressed: getDuration,
),
const Pad(width: 8.0),
Text(
duration?.toString() ?? '-',
key: const Key('durationText'),
),
],
),
Row(
children: [
Btn(
key: const Key('getPosition'),
txt: 'Get Position',
onPressed: getPosition,
),
const Pad(width: 8.0),
Text(
position?.toString() ?? '-',
key: const Key('positionText'),
),
],
),
Row(
children: [
Btn(
key: const Key('getPlayerState'),
txt: 'Get State',
onPressed: getPlayerState,
),
const Pad(width: 8.0),
Text(
state?.toString() ?? '-',
key: const Key('playerStateText'),
),
],
),
Row(
children: [
Btn(
key: const Key('getSource'),
txt: 'Get Source',
onPressed: getSource,
),
const Pad(width: 8.0),
Text(
source?.toString() ?? '-',
key: const Key('sourceText'),
),
],
),
const Divider(color: Colors.black),
const Text('Streams'),
Text(
'Stream Duration: $streamDuration',
key: const Key('onDurationText'),
),
Text(
'Stream Position: $streamPosition',
key: const Key('onPositionText'),
),
Text(
'Stream State: $streamState',
key: const Key('onStateText'),
),
const Divider(color: Colors.black),
PlayerWidget(player: widget.player),
],
);
}
@override
bool get wantKeepAlive => true;
}