Skip to content

Instantly share code, notes, and snippets.

@frankkienl
Created July 15, 2020 12:47
Show Gist options
  • Save frankkienl/2fa6baa9db3292369b12c5e801e696fe to your computer and use it in GitHub Desktop.
Save frankkienl/2fa6baa9db3292369b12c5e801e696fe to your computer and use it in GitHub Desktop.
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Soundboard',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Soundboard'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var repeatCount = 1;
var currentPlays = 0;
@override
Widget build(BuildContext context) {
var listChildren = [_buildRepeatButtons()];
listChildren.addAll(_buildSoundsButtons());
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(children: listChildren),
);
}
var sounds = [
"T01|0x01 TONE_CONNECT",
"T02|0x02 TONE_CONNECT_CLASSIC",
"T03|0x03 TONE_DISCONNECT",
"T04|0x04 TONE_CLICK",
"T05|0x05 TONE_NOTIFICATION",
"T06|0x06 TONE_SIREN",
"T07|0x07 TONE_ALARM_SWEEP_1",
"T08|0x08 TONE_ALARM_SWEEP_2",
"T09|0x09 TONE_ALARM_SWEEP_3",
"T0A|0x0A TONE_ALARM_SWEEP_4",
"T0B|0x0B TONE_ALARM_BEEP_SHORT",
"T0C|0x0C TONE_ALARM_BEEP_MEDIUM",
"T0D|0x0D TONE_ALARM_BEEP_LONG",
"T0E|0x0E TONE_ALARM_BEEP_SHORT_2",
"T0F|0x0F TONE_ALARM_BEEP_MEDIUM_2",
"T10|0x10 TONE_ALARM_BEEP_LONG_2",
"T11|0x11 TONE_DISCONNECT_2",
"T12|0x12 TONE_CLICK_2",
"T13|0x13 TONE_NOTIFICATION_2",
"T14|0x14 TONE_NOT_CONNECTED_CLASSIC",
];
repeatCountDecrease() {
setState(() {
repeatCount--;
});
}
repeatCountIncrease() {
setState(() {
repeatCount++;
});
}
Widget _buildRepeatButtons() {
if (kIsWeb){
//audio looping werkt niet op web :C
return Container();
}
var row = Row(
children: [
RaisedButton(
child: Text(" - "),
onPressed: () {
repeatCountDecrease();
},
),
Expanded(child: Center(child: Text("$repeatCount"))),
RaisedButton(
child: Text(" + "),
onPressed: () {
repeatCountIncrease();
},
)
],
);
return row;
}
List<Widget> _buildSoundsButtons() {
var soundWidgets = sounds
.map((e) => RaisedButton(
child: Text(e.substring(4)),
onPressed: () {
playSound("sounds/" + e.substring(0, 3) + ".mp3");
},
))
.toList();
return soundWidgets;
}
playSound(name) async {
var audioCache = AudioCache();
currentPlays = 0;
var player = await audioCache.play(name);
player.setReleaseMode(ReleaseMode.STOP);
player.onPlayerCompletion.listen((event) {
currentPlays++;
print("audio onPlayerCompletion current=$currentPlays repeat=$repeatCount");
if (currentPlays < repeatCount) {
player.resume();
} else {
player.stop();
player.release();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment