b1869cf8
biao
背景音乐添加
|
1
|
import 'package:audioplayers/audioplayers.dart';
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
2
|
import 'package:flutter/cupertino.dart';
|
b1869cf8
biao
背景音乐添加
|
3
4
|
import 'package:wow_english/common/extension/string_extension.dart';
|
2d1ead53
吴启风
feat:背景音播放优化
|
5
6
|
import 'log_util.dart';
|
b1869cf8
biao
背景音乐添加
|
7
8
9
10
11
12
13
14
15
16
|
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'),
|
c623c7b2
吴启风
feat:语音跟读作答结果动效&语音
|
17
18
19
20
|
touch('touch_instrumental'),
excellent('excellent'),
great('great'),
good('good'),
|
819ae43b
吴启风
feat:体验优化-练习题目取消阻...
|
21
22
23
|
tryAgain('try_again'),
right('right'),
wrong('wrong');
|
b1869cf8
biao
背景音乐添加
|
24
25
26
27
28
29
|
const AudioPlayerUtilType(this.path);
final String path;
}
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
30
|
class AudioPlayerUtil extends WidgetsBindingObserver {
|
b1869cf8
biao
背景音乐添加
|
31
|
static AudioPlayerUtil? _instance;
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
32
|
late AudioPlayer _audioPlayer;
|
b1869cf8
biao
背景音乐添加
|
33
|
late AudioPlayerUtilType currentType;
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
34
|
bool _wasPlaying = false;
|
2d1ead53
吴启风
feat:背景音播放优化
|
35
|
static const TAG = "AudioPlayerUtil";
|
b1869cf8
biao
背景音乐添加
|
36
37
38
|
// 私有构造函数
AudioPlayerUtil._internal() {
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
39
40
41
42
|
// 监听应用生命周期
WidgetsBinding.instance.addObserver(this);
_audioPlayer = AudioPlayer();
_audioPlayer.onPlayerStateChanged.listen((event) async {
|
b1869cf8
biao
背景音乐添加
|
43
44
45
46
47
48
49
50
51
52
|
if (event == PlayerState.completed) {
// 播放结束再次播放
if (currentType == AudioPlayerUtilType.inMyTummy) {
AudioPlayerUtil.getInstance()
.playAudio(AudioPlayerUtilType.inMyTummy);
}
if (currentType == AudioPlayerUtilType.countWithMe) {
AudioPlayerUtil.getInstance()
.playAudio(AudioPlayerUtilType.countWithMe);
}
|
b1869cf8
biao
背景音乐添加
|
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
if (currentType == AudioPlayerUtilType.touch) {
AudioPlayerUtil.getInstance().playAudio(AudioPlayerUtilType.touch);
}
}
});
}
static AudioPlayerUtil getInstance() {
_instance ??= AudioPlayerUtil._internal();
return _instance!;
}
// 播放音频
Future<void> playAudio(AudioPlayerUtilType type) async {
|
2d1ead53
吴启风
feat:背景音播放优化
|
67
|
Log.d("$TAG playAudio $type");
|
b1869cf8
biao
背景音乐添加
|
68
69
|
currentType = type;
String path = type.path;
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
70
71
|
await _audioPlayer.play(AssetSource(path.assetMp3), volume: 0.5);
await _audioPlayer.onPlayerComplete.first;
|
b1869cf8
biao
背景音乐添加
|
72
73
74
|
}
// stop
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
75
|
Future<void> stop() async {
|
2d1ead53
吴启风
feat:背景音播放优化
|
76
|
Log.d("$TAG stop _audioPlayer.state=${_audioPlayer.state}");
|
9e29ffea
吴启风
feat:音频api调用优化
|
77
|
await _audioPlayer.stop();
|
b1869cf8
biao
背景音乐添加
|
78
79
80
|
}
// pause
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
81
|
Future<void> pause() async {
|
2d1ead53
吴启风
feat:背景音播放优化
|
82
|
Log.d("$TAG pause _audioPlayer.state=${_audioPlayer.state}");
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
83
|
if (_audioPlayer.state == PlayerState.playing) {
|
9e29ffea
吴启风
feat:音频api调用优化
|
84
|
await _audioPlayer.pause();
|
b1869cf8
biao
背景音乐添加
|
85
86
87
88
|
}
}
// resume
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
89
|
Future<void> resume() async {
|
2d1ead53
吴启风
feat:背景音播放优化
|
90
|
Log.d("$TAG resume _audioPlayer.state=${_audioPlayer.state}");
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
91
|
if (_audioPlayer.state == PlayerState.paused) {
|
9e29ffea
吴启风
feat:音频api调用优化
|
92
|
await _audioPlayer.resume();
|
b1869cf8
biao
背景音乐添加
|
93
94
|
}
}
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
95
96
97
|
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
|
2d1ead53
吴启风
feat:背景音播放优化
|
98
|
Log.d("$TAG didChangeAppLifecycleState appState=$state _wasPlaying=$_wasPlaying _audioPlayer.state=${_audioPlayer.state}");
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
if (state == AppLifecycleState.paused) {
if (_audioPlayer.state == PlayerState.playing) {
_wasPlaying = true;
await pause();
};
} else if (state == AppLifecycleState.resumed) {
if (_wasPlaying == true) {
_wasPlaying = false;
await resume();
}
}
}
void dispose() {
|
2d1ead53
吴启风
feat:背景音播放优化
|
113
|
Log.d("$TAG dispose _audioPlayer.state=${_audioPlayer.state}");
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
114
115
116
|
_audioPlayer.dispose();
WidgetsBinding.instance.removeObserver(this);
}
|
b1869cf8
biao
背景音乐添加
|
117
|
}
|