b1869cf8
biao
背景音乐添加
|
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
|
import 'package:audioplayers/audioplayers.dart';
import 'package:wow_english/common/extension/string_extension.dart';
enum AudioPlayerUtilType {
welcomeToWow('welcome_to_wow'),
classTime('class_time'),
gameTime('game_time'),
musicTime('music_time'),
readingTime('reading_time'),
videoTime('video_time'),
quizTime('quiz_time'),
countWithMe('count_with_me_instrumental'),
inMyTummy('in_my_tummy_instrumental'),
touch('touch_instrumental');
const AudioPlayerUtilType(this.path);
final String path;
}
class AudioPlayerUtil {
static AudioPlayerUtil? _instance;
late AudioPlayer audioPlayer;
late AudioPlayerUtilType currentType;
// 私有构造函数
AudioPlayerUtil._internal() {
audioPlayer = AudioPlayer();
audioPlayer.onPlayerStateChanged.listen((event) async {
if (event == PlayerState.completed) {
// 播放结束再次播放
if (currentType == AudioPlayerUtilType.inMyTummy) {
AudioPlayerUtil.getInstance()
.playAudio(AudioPlayerUtilType.inMyTummy);
}
if (currentType == AudioPlayerUtilType.countWithMe) {
AudioPlayerUtil.getInstance()
.playAudio(AudioPlayerUtilType.countWithMe);
}
if (currentType == AudioPlayerUtilType.welcomeToWow) {
AudioPlayerUtil.getInstance().playAudio(AudioPlayerUtilType.touch);
}
if (currentType == AudioPlayerUtilType.touch) {
AudioPlayerUtil.getInstance().playAudio(AudioPlayerUtilType.touch);
}
}
});
}
static AudioPlayerUtil getInstance() {
_instance ??= AudioPlayerUtil._internal();
return _instance!;
}
// 播放音频
Future<void> playAudio(AudioPlayerUtilType type) async {
currentType = type;
String path = type.path;
await audioPlayer.play(AssetSource(path.assetMp3), volume: 0.5);
await audioPlayer.onPlayerComplete.first;
}
// stop
void stop() {
audioPlayer.stop();
}
// pause
void pause() {
if (audioPlayer.state == PlayerState.playing) {
audioPlayer.pause();
}
}
// resume
void resume() {
if (audioPlayer.state == PlayerState.paused) {
audioPlayer.resume();
}
}
}
|