1
+ import 'dart:async' ;
2
+
1
3
import 'package:animated_flip_counter/animated_flip_counter.dart' ;
2
4
import 'package:flutter/cupertino.dart' ;
5
+ import 'package:flutter/services.dart' ;
3
6
import 'package:flutter_neumorphic/flutter_neumorphic.dart' ;
4
7
import 'package:flutter_screenutil/flutter_screenutil.dart' ;
8
+ import 'package:flutter_sound/flutter_sound.dart' ;
9
+ import 'package:percent_indicator/percent_indicator.dart' ;
10
+ import 'package:provider/provider.dart' ;
5
11
import 'package:simple_app/common/color.dart' ;
6
12
import 'package:simple_app/components/base/build_base_app_bar.dart' ;
7
13
import 'package:simple_app/generated/l10n.dart' ;
8
- import 'package:provider/provider.dart' ;
9
14
import 'package:simple_app/provider/current_theme.dart' ;
10
- import 'dart:async' ;
11
- import 'package:percent_indicator/percent_indicator.dart' ;
12
15
import 'package:simple_app/utils/show_dialog.dart' ;
13
16
import 'package:simple_app/utils/show_toast.dart' ;
14
17
import 'package:vibration/vibration.dart' ;
@@ -44,6 +47,10 @@ class _CountDownPageState extends State<CountDownPage> {
44
47
Timer ? timerId;
45
48
// 计数器过渡动画
46
49
final Duration _duration = const Duration (milliseconds: 250 );
50
+
51
+ // 音频字节
52
+ ByteData ? bytes;
53
+ FlutterSoundPlayer ? _myPlayer = FlutterSoundPlayer ();
47
54
void handleStartClick () {
48
55
if (pickerTime.inSeconds < 6 ) return ;
49
56
setState (() => show = true );
@@ -63,11 +70,18 @@ class _CountDownPageState extends State<CountDownPage> {
63
70
second = s;
64
71
hour = h == 0 ? null : h;
65
72
});
73
+ final buffer = bytes! .buffer;
74
+
66
75
Timer .periodic (timeout, (timer) async {
67
76
startSecond++ ;
68
77
int diffSecond = totalSecond - startSecond;
69
78
timerId = timer;
70
79
success = false ;
80
+ _myPlayer! .startPlayer (
81
+ fromDataBuffer:
82
+ buffer.asUint8List (bytes! .offsetInBytes, bytes! .lengthInBytes),
83
+ codec: Codec .mp3,
84
+ );
71
85
var time = Duration (seconds: diffSecond);
72
86
// 更新时间
73
87
setState (() {
@@ -97,7 +111,7 @@ class _CountDownPageState extends State<CountDownPage> {
97
111
});
98
112
success = true ;
99
113
if (await Vibration .hasCustomVibrationsSupport () != null ) {
100
- Vibration .vibrate (duration: 2500 , amplitude: 128 );
114
+ Vibration .vibrate (duration: 2500 , amplitude: 128 );
101
115
}
102
116
showToast (S .of (context).timeOut);
103
117
}
@@ -128,6 +142,16 @@ class _CountDownPageState extends State<CountDownPage> {
128
142
super .dispose ();
129
143
// 页面卸载 取消定时器
130
144
timerId? .cancel ();
145
+ _myPlayer? .closePlayer ();
146
+ _myPlayer = null ;
147
+ }
148
+
149
+ @override
150
+ void initState () {
151
+ super .initState ();
152
+ _myPlayer? .openPlayer ().then ((value) async {
153
+ bytes = await rootBundle.load ("assets/15199.mp3" );
154
+ });
131
155
}
132
156
133
157
@override
0 commit comments