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