Skip to content

Commit 0fcbd18

Browse files
author
lyy
committed
feat: 新增定时关闭
1 parent d40fd09 commit 0fcbd18

File tree

7 files changed

+174
-7
lines changed

7 files changed

+174
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
## 1.0.2
1+
## 1.1.0
22

33
- [feat] 新增数据导入导出
44
- [feat] 歌单源完善,支持自定义文件
55
- [feat] 播放列表添加高亮选中
6+
- [feat] 新增定时关闭
7+
- [feat] 云同步功能完善
68

79
## 1.0.1
810

lib/modules/player/card.dart

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22

33
import 'package:bbmusic/modules/download/model.dart';
4+
import 'package:bot_toast/bot_toast.dart';
45
import 'package:cached_network_image/cached_network_image.dart';
56
import 'package:flutter/material.dart';
67
import 'package:bbmusic/modules/player/player.dart';
@@ -65,7 +66,7 @@ class PlayerCard extends StatelessWidget {
6566
),
6667
),
6768
const SizedBox(height: 10),
68-
// 下载,添加到歌单
69+
// 下载,定时播放
6970
Row(
7071
mainAxisAlignment: MainAxisAlignment.center,
7172
children: [
@@ -84,7 +85,9 @@ class PlayerCard extends StatelessWidget {
8485
message: "定时关闭",
8586
child: IconButton(
8687
iconSize: 30,
87-
onPressed: () {},
88+
onPressed: () {
89+
autoClose(context);
90+
},
8891
icon: const Icon(Icons.alarm),
8992
),
9093
)
@@ -200,3 +203,72 @@ class _PlayerProgressState extends State<PlayerProgress> {
200203
});
201204
}
202205
}
206+
207+
class AutoCloseItem {
208+
final String label;
209+
final Duration value;
210+
211+
AutoCloseItem({required this.label, required this.value});
212+
}
213+
214+
final List<AutoCloseItem> AutoCloseList = [
215+
AutoCloseItem(label: "1 分钟", value: const Duration(minutes: 1)),
216+
AutoCloseItem(label: "5 分钟", value: const Duration(minutes: 5)),
217+
AutoCloseItem(label: "10 分钟", value: const Duration(minutes: 10)),
218+
AutoCloseItem(label: "15 分钟", value: const Duration(minutes: 15)),
219+
AutoCloseItem(label: "30 分钟", value: const Duration(minutes: 30)),
220+
AutoCloseItem(label: "60 分钟", value: const Duration(minutes: 60)),
221+
];
222+
223+
// 定时关闭
224+
autoClose(BuildContext context) {
225+
showModalBottomSheet(
226+
context: context,
227+
showDragHandle: true,
228+
builder: (BuildContext ctx) {
229+
return SafeArea(
230+
bottom: true,
231+
child: Container(
232+
color: Theme.of(context).cardTheme.color,
233+
padding: const EdgeInsets.all(10),
234+
height: 180,
235+
width: double.infinity,
236+
child: Column(
237+
children: [
238+
Wrap(
239+
spacing: 10,
240+
runSpacing: 18,
241+
children: AutoCloseList.map((item) {
242+
return OutlinedButton(
243+
child: Text(item.label),
244+
onPressed: () {
245+
Navigator.of(context).pop();
246+
BotToast.showText(text: "${item.label}后自动关闭");
247+
Provider.of<PlayerModel>(context, listen: false)
248+
.autoCloseHandler(item.value);
249+
},
250+
);
251+
}).toList(),
252+
),
253+
const SizedBox(height: 20),
254+
Row(
255+
mainAxisAlignment: MainAxisAlignment.center,
256+
children: [
257+
Consumer<PlayerModel>(builder: (context, player, child) {
258+
return Checkbox(
259+
value: player.playDoneAutoClose,
260+
onChanged: (e) {
261+
player.togglePlayDoneAutoClose();
262+
},
263+
);
264+
}),
265+
const Text("当前歌曲播放完成后再关闭"),
266+
],
267+
),
268+
],
269+
),
270+
),
271+
);
272+
},
273+
);
274+
}

lib/modules/player/instance.dart

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ class BBPlayer {
3737
// 播放模式
3838
PlayerMode playerMode = PlayerMode.listLoop;
3939

40+
late AutoCloseMusic autoClose;
41+
4042
Future<void> init() async {
43+
autoClose = AutoCloseMusic(onPause: () {
44+
pause();
45+
});
4146
await _initLocalStorage();
4247
var throttleEndNext = Throttle(const Duration(seconds: 1));
4348

@@ -51,7 +56,13 @@ class BBPlayer {
5156
}
5257
if (state.processingState == ProcessingState.completed) {
5358
// 会重复触发,添加节流方法
54-
throttleEndNext.call(() => endNext());
59+
throttleEndNext.call(() {
60+
if (autoClose.isPlayDoneAutoClose) {
61+
autoClose.isPlayDoneAutoClose = false;
62+
return pause();
63+
}
64+
return endNext();
65+
});
5566
}
5667
// notifyListeners();
5768
});
@@ -406,3 +417,49 @@ class BBPlayer {
406417
// notifyListeners();
407418
}
408419
}
420+
421+
/// 定时关闭
422+
class AutoCloseMusic {
423+
bool openPlayDoneAutoClose = false; // 是否开启等待播放完成后再关闭
424+
bool isPlayDoneAutoClose = false;
425+
DateTime? closeTime;
426+
Timer? autoCloseTimer;
427+
428+
final Function onPause;
429+
430+
AutoCloseMusic({
431+
required this.onPause,
432+
});
433+
434+
void togglePlayDoneAutoClose() {
435+
openPlayDoneAutoClose = !openPlayDoneAutoClose;
436+
}
437+
438+
// 自动关闭
439+
void close(Duration duration) {
440+
isPlayDoneAutoClose = false;
441+
if (autoCloseTimer != null) {
442+
autoCloseTimer!.cancel();
443+
}
444+
445+
// 设置时间为 5 min 后
446+
final now = DateTime.now();
447+
closeTime = now.add(duration);
448+
449+
autoCloseTimer = Timer(duration, () {
450+
if (openPlayDoneAutoClose) {
451+
isPlayDoneAutoClose = true;
452+
} else {
453+
onPause();
454+
}
455+
});
456+
}
457+
458+
void cancel() {
459+
closeTime = null;
460+
isPlayDoneAutoClose = false;
461+
if (autoCloseTimer != null) {
462+
autoCloseTimer!.cancel();
463+
}
464+
}
465+
}

lib/modules/player/model.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ class PlayerModel extends ChangeNotifier {
9696
notifyListeners();
9797
}
9898

99+
void togglePlayDoneAutoClose() {
100+
_playerHandler?.player.autoClose.togglePlayDoneAutoClose();
101+
notifyListeners();
102+
}
103+
104+
get playDoneAutoClose {
105+
return _playerHandler?.player.autoClose.openPlayDoneAutoClose ?? false;
106+
}
107+
108+
autoCloseHandler(Duration duration) {
109+
return _playerHandler?.player.autoClose.close(duration);
110+
}
111+
99112
// 监听播放进度
100113
StreamSubscription<Duration>? listenPosition(
101114
void Function(Duration)? onData,

lib/modules/user_music_order/github/github.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,32 @@ class UserMusicOrderForGithub implements UserMusicOrderOrigin {
5959
}
6060
}
6161

62+
// 判断文件是否存在,不存在则创建
63+
_initFile() async {
64+
try {
65+
await dio.get(_path.toString(), queryParameters: {'ref': branch});
66+
} catch (e) {
67+
if (e is DioException) {
68+
if ((e).response?.statusCode == 404) {
69+
// 没有则文件创建
70+
try {
71+
await _update([], '创建歌单文件', '');
72+
} catch (e) {
73+
BotToast.showText(text: "创建文件失败");
74+
return Future.error(e);
75+
}
76+
}
77+
}
78+
79+
return Future.error(e);
80+
}
81+
}
82+
6283
Future<GithubFile> _loadData() async {
6384
Map<String, String> query = {'ref': branch};
85+
Response<dynamic>? response;
6486
try {
65-
final response = await dio.get(_path.toString(), queryParameters: query);
87+
response = await dio.get(_path.toString(), queryParameters: query);
6688
final res = GithubFile.fromJson(response.data);
6789
return res;
6890
} catch (e) {
@@ -130,6 +152,7 @@ class UserMusicOrderForGithub implements UserMusicOrderOrigin {
130152
return [];
131153
}
132154
try {
155+
await _initFile();
133156
final res = await _loadData();
134157
return res.content;
135158
} catch (e) {

lib/modules/user_music_order/github/types.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ List<MusicOrderItem> decodeGithubFileContent(String content) {
7373
final contentRaw = content.replaceAll('\n', '');
7474
// base 64 解码
7575
String raw = Utf8Decoder().convert(base64Decode(contentRaw));
76-
final data = json.decode(raw);
76+
final data = json.decode(raw.trim().isEmpty ? '[]' : raw);
7777
List<MusicOrderItem> list = [];
7878
for (var item in data) {
7979
list.add(

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: bbmusic
22
description: '哔哔音乐,听歌自由'
33
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
44

5-
version: 1.0.2+4
5+
version: 1.1.0+5
66

77
environment:
88
sdk: '>=3.3.1 <4.0.0'

0 commit comments

Comments
 (0)