-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.dart
154 lines (148 loc) · 4.1 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
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:metronome/metronome.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _metronomePlugin = Metronome();
bool isplaying = false;
int bpm = 120;
int vol = 50;
String metronomeIcon = 'assets/metronome-left.png';
String metronomeIconRight = 'assets/metronome-right.png';
String metronomeIconLeft = 'assets/metronome-left.png';
final List wavs = [
'base',
'claves',
'hihat',
'snare',
'sticks',
'woodblock_high'
];
@override
void initState() {
super.initState();
_metronomePlugin.init(
'assets/audio/snare44_wav.wav',
bpm: bpm,
volume: vol,
enableTickCallback: true,
);
_metronomePlugin.onListenTick((_) {
if (kDebugMode) {
print('tick');
}
setState(() {
if (metronomeIcon == metronomeIconRight) {
metronomeIcon = metronomeIconLeft;
} else {
metronomeIcon = metronomeIconRight;
}
});
});
}
@override
void dispose() {
_metronomePlugin.destroy();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Metronome example'),
),
body: Container(
padding: const EdgeInsets.all(10),
child: ListView(
children: [
Image.asset(
metronomeIcon,
height: 100,
gaplessPlayback: true,
),
Text(
'BPM:$bpm',
style: const TextStyle(fontSize: 20),
),
Slider(
value: bpm.toDouble(),
min: 30,
max: 300,
divisions: 270,
onChangeEnd: (val) {
_metronomePlugin.setBPM(bpm);
},
onChanged: (val) {
bpm = val.toInt();
setState(() {});
},
),
Text(
'Volume:$vol%',
style: const TextStyle(fontSize: 20),
),
Slider(
value: vol.toDouble(),
min: 0,
max: 100,
divisions: 100,
onChangeEnd: (val) {
_metronomePlugin.setVolume(vol);
},
onChanged: (val) {
vol = val.toInt();
setState(() {});
},
),
SizedBox(
width: 200,
height: 350,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: wavs
.map(
(wav) => ElevatedButton(
child: Text(wav),
onPressed: () {
_metronomePlugin.setAudioAssets(
'assets/audio/${wav}44_wav.wav');
},
),
)
.toList(),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
if (isplaying) {
_metronomePlugin.pause();
isplaying = false;
} else {
_metronomePlugin.setVolume(vol);
_metronomePlugin.play(bpm);
isplaying = true;
}
// int? bpm2 = await _metronomePlugin.getBPM();
// print(bpm2);
// int? vol2 = await _metronomePlugin.getVolume();
// print(vol2);
setState(() {});
},
child: Icon(isplaying ? Icons.pause : Icons.play_arrow),
),
),
);
}
}