-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.dart
163 lines (149 loc) · 4.28 KB
/
main.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
159
160
161
162
163
import 'package:audioplayers/audioplayers.dart';
import 'package:example/load_audio_data.dart';
import 'package:example/waveforms_dashboard.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_audio_waveforms/flutter_audio_waveforms.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Audio Waveforms',
home: WaveformsDashboard(),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
late Duration maxDuration;
late Duration elapsedDuration;
late AudioCache audioPlayer;
late List<double> samples;
late int totalSamples;
late List<String> audioData;
List<List<String>> audioDataList = [
[
'assets/dm.json',
'dance_monkey.mp3',
],
[
'assets/soy.json',
'shape_of_you.mp3',
],
[
'assets/sp.json',
'surface_pressure.mp3',
],
];
Future<void> parseData() async {
final json = await rootBundle.loadString(audioData[0]);
Map<String, dynamic> audioDataMap = {
"json": json,
"totalSamples": totalSamples,
};
final samplesData = await compute(loadparseJson, audioDataMap);
await audioPlayer.load(audioData[1]);
await audioPlayer.play(audioData[1]);
// maxDuration in milliseconds
await Future.delayed(const Duration(milliseconds: 200));
int maxDurationInmilliseconds =
await audioPlayer.fixedPlayer!.getDuration();
maxDuration = Duration(milliseconds: maxDurationInmilliseconds);
setState(() {
samples = samplesData["samples"];
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
// Change this value to number of audio samples you want.
// Values between 256 and 1024 are good for showing [RectangleWaveform] and [SquigglyWaveform]
// While the values above them are good for showing [PolygonWaveform]
totalSamples = 1000;
audioData = audioDataList[0];
audioPlayer = AudioCache(
fixedPlayer: AudioPlayer(),
);
samples = [];
maxDuration = const Duration(milliseconds: 1000);
elapsedDuration = const Duration();
parseData();
audioPlayer.fixedPlayer!.onPlayerCompletion.listen((_) {
setState(() {
elapsedDuration = maxDuration;
});
});
audioPlayer.fixedPlayer!.onAudioPositionChanged
.listen((Duration timeElapsed) {
setState(() {
elapsedDuration = timeElapsed;
});
});
}
@override
Widget build(BuildContext context) {
const sizedBox = SizedBox(
height: 30,
width: 30,
);
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: const Text('Flutter Audio Waveforms'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
PolygonWaveform(
maxDuration: maxDuration,
elapsedDuration: elapsedDuration,
samples: samples,
height: 300,
width: MediaQuery.of(context).size.width,
),
sizedBox,
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
audioPlayer.fixedPlayer!.pause();
},
child: const Icon(
Icons.pause,
),
),
sizedBox,
ElevatedButton(
onPressed: () {
audioPlayer.fixedPlayer!.resume();
},
child: const Icon(Icons.play_arrow),
),
sizedBox,
ElevatedButton(
onPressed: () {
setState(() {
audioPlayer.fixedPlayer!
.seek(const Duration(milliseconds: 0));
});
},
child: const Icon(Icons.replay_outlined),
),
],
)
],
),
);
}
}