Monitoring

Overview

If you are building your own widget you might want to display a progress bar that displays the current recording or playback position.
The easiest way to do this is via the SoundPlayerUI/SoundRecordingUI widgets but if you want to write your own then you will want to use the dispositionStream with a StreamBuilder.
To use a dispositionStream you need to create an SoundPlayer or SoundRecorder instance.
1
class MyWidgetState
2
{
3
/// use .noUI() as you are going to build your own UI.
4
var player = SoundPlayer().noUI();
5
​
6
void initState()
7
{
8
super.initState();
9
​
10
}
11
​
12
void dispose()
13
{
14
player.release();
15
super.dispose();
16
}
17
​
18
Widget build() {
19
return Row(children:
20
[Button('Play' onTap: onPlay)
21
, StreamBuilder<PlaybackDisposition>(
22
stream: player.dispositionStream,
23
initialData: PlaybackDisposition.zero(),
24
builder: (context, snapshot) {
25
var disposition = snapshot.data;
26
return Slider(
27
max: disposition.duration.inMilliseconds.toDouble(),
28
value: disposition.position.inMilliseconds.toDouble(),
29
onChanged: (value) =>
30
player._seek(Duration(milliseconds: value.toInt())),
31
);
32
}
33
))
34
]);
35
},
36
));
37
​
38
/// you would wire this to a button
39
void onPlay()
40
{
41
player.play(Track.fromFile('sample.aac'));
42
}
43
​
44
/// you would wire this to a pause button
45
void onPause()
46
{
47
player.pause();
48
}
49
​
50
/// you would wire this to a button
51
void onResume()
52
{
53
player.resume();
54
}
55
}
Copied!
Last modified 1yr ago
Copy link