Skip to content

Commit dc61033

Browse files
committed
倒计时添加音乐播放
1 parent d5c63ea commit dc61033

File tree

8 files changed

+92
-6
lines changed

8 files changed

+92
-6
lines changed

.vscode/settings.json

+7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22
"cSpell.words": [
33
"appbar",
44
"autofocus",
5+
"AUTOINCREMENT",
56
"bord",
67
"cupertino",
8+
"Draggable",
79
"fluttertoast",
10+
"initialise",
811
"listdata",
912
"listkey",
1013
"localizely",
1114
"LTRB",
15+
"mipmap",
1216
"navigatorkey",
1317
"Neumorphic",
18+
"nullsafety",
1419
"prefs",
20+
"prefsinstance",
1521
"RGBO",
1622
"screenutil",
1723
"sqflite",
24+
"todolist",
1825
"unfocus",
1926
"zefyr"
2027
]

android/app/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ android {
4444
defaultConfig {
4545
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4646
applicationId "com.example.simple_app"
47-
minSdkVersion flutter.minSdkVersion
47+
// minSdkVersion flutter.minSdkVersion
48+
minSdkVersion 18
4849
targetSdkVersion flutter.targetSdkVersion
4950
versionCode flutterVersionCode.toInteger()
5051
versionName flutterVersionName

android/app/src/main/AndroidManifest.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.example.simple_app">
33
<uses-permission android:name="android.permission.VIBRATE" />
4-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
4+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
5+
<uses-permission android:name="android.permission.RECORD_AUDIO" />
6+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
57
<application
68
android:label="simple_app"
79
android:name="${applicationName}"

assets/15199.mp3

8.48 KB
Binary file not shown.

ios/Runner/Info.plist

+6
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@
4343
</array>
4444
<key>UIViewControllerBasedStatusBarAppearance</key>
4545
<false/>
46+
<key>NSMicrophoneUsageDescription</key>
47+
<string>This sample uses the microphone to record your speech and convert it to text.</string>
48+
<key>UIBackgroundModes</key>
49+
<array>
50+
<string>audio</string>
51+
</array>
4652
</dict>
4753
</plist>

lib/page/count_down.dart

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import 'dart:async';
2+
13
import 'package:animated_flip_counter/animated_flip_counter.dart';
24
import 'package:flutter/cupertino.dart';
5+
import 'package:flutter/services.dart';
36
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
47
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';
511
import 'package:simple_app/common/color.dart';
612
import 'package:simple_app/components/base/build_base_app_bar.dart';
713
import 'package:simple_app/generated/l10n.dart';
8-
import 'package:provider/provider.dart';
914
import 'package:simple_app/provider/current_theme.dart';
10-
import 'dart:async';
11-
import 'package:percent_indicator/percent_indicator.dart';
1215
import 'package:simple_app/utils/show_dialog.dart';
1316
import 'package:simple_app/utils/show_toast.dart';
1417
import 'package:vibration/vibration.dart';
@@ -44,6 +47,10 @@ class _CountDownPageState extends State<CountDownPage> {
4447
Timer? timerId;
4548
// 计数器过渡动画
4649
final Duration _duration = const Duration(milliseconds: 250);
50+
51+
// 音频字节
52+
ByteData? bytes;
53+
FlutterSoundPlayer? _myPlayer = FlutterSoundPlayer();
4754
void handleStartClick() {
4855
if (pickerTime.inSeconds < 6) return;
4956
setState(() => show = true);
@@ -63,11 +70,18 @@ class _CountDownPageState extends State<CountDownPage> {
6370
second = s;
6471
hour = h == 0 ? null : h;
6572
});
73+
final buffer = bytes!.buffer;
74+
6675
Timer.periodic(timeout, (timer) async {
6776
startSecond++;
6877
int diffSecond = totalSecond - startSecond;
6978
timerId = timer;
7079
success = false;
80+
_myPlayer!.startPlayer(
81+
fromDataBuffer:
82+
buffer.asUint8List(bytes!.offsetInBytes, bytes!.lengthInBytes),
83+
codec: Codec.mp3,
84+
);
7185
var time = Duration(seconds: diffSecond);
7286
// 更新时间
7387
setState(() {
@@ -97,7 +111,7 @@ class _CountDownPageState extends State<CountDownPage> {
97111
});
98112
success = true;
99113
if (await Vibration.hasCustomVibrationsSupport() != null) {
100-
Vibration.vibrate(duration: 2500, amplitude: 128);
114+
Vibration.vibrate(duration: 2500, amplitude: 128);
101115
}
102116
showToast(S.of(context).timeOut);
103117
}
@@ -128,6 +142,16 @@ class _CountDownPageState extends State<CountDownPage> {
128142
super.dispose();
129143
// 页面卸载 取消定时器
130144
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+
});
131155
}
132156

133157
@override

pubspec.lock

+42
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,27 @@ packages:
256256
url: "https://pub.flutter-io.cn"
257257
source: hosted
258258
version: "1.2.0"
259+
flutter_sound:
260+
dependency: "direct main"
261+
description:
262+
name: flutter_sound
263+
url: "https://pub.flutter-io.cn"
264+
source: hosted
265+
version: "9.1.3"
266+
flutter_sound_platform_interface:
267+
dependency: transitive
268+
description:
269+
name: flutter_sound_platform_interface
270+
url: "https://pub.flutter-io.cn"
271+
source: hosted
272+
version: "9.1.3"
273+
flutter_sound_web:
274+
dependency: transitive
275+
description:
276+
name: flutter_sound_web
277+
url: "https://pub.flutter-io.cn"
278+
source: hosted
279+
version: "9.1.3"
259280
flutter_staggered_grid_view:
260281
dependency: "direct main"
261282
description:
@@ -315,6 +336,13 @@ packages:
315336
url: "https://pub.flutter-io.cn"
316337
source: hosted
317338
version: "1.0.1"
339+
logger:
340+
dependency: transitive
341+
description:
342+
name: logger
343+
url: "https://pub.flutter-io.cn"
344+
source: hosted
345+
version: "1.1.0"
318346
logging:
319347
dependency: transitive
320348
description:
@@ -497,6 +525,13 @@ packages:
497525
url: "https://pub.flutter-io.cn"
498526
source: hosted
499527
version: "2.1.0"
528+
recase:
529+
dependency: transitive
530+
description:
531+
name: recase
532+
url: "https://pub.flutter-io.cn"
533+
source: hosted
534+
version: "4.0.0"
500535
shared_preferences:
501536
dependency: "direct main"
502537
description:
@@ -705,6 +740,13 @@ packages:
705740
url: "https://pub.flutter-io.cn"
706741
source: hosted
707742
version: "2.0.2"
743+
uuid:
744+
dependency: transitive
745+
description:
746+
name: uuid
747+
url: "https://pub.flutter-io.cn"
748+
source: hosted
749+
version: "3.0.6"
708750
value_layout_builder:
709751
dependency: transitive
710752
description:

pubspec.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies:
3131
flutter_neumorphic: ^3.0.3
3232
percent_indicator: ^3.4.0
3333
vibration: ^1.7.4-nullsafety.0
34+
flutter_sound: ^9.1.3
3435
flutter_localizations:
3536
sdk: flutter
3637

@@ -41,6 +42,8 @@ dev_dependencies:
4142

4243
flutter:
4344
uses-material-design: true
45+
assets:
46+
- 15199.mp3
4447
flutter_intl:
4548
enabled: true
4649
localizely:
@@ -56,3 +59,4 @@ flutter_native_splash:
5659
web: false
5760
android_gravity: center
5861
fullerene: true
62+

0 commit comments

Comments
 (0)