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';
|
9e14f47a
吴启风
feat:解决开场音乐前点击(带音...
|
5
|
import '../common/request/basic_config.dart';
|
2d1ead53
吴启风
feat:背景音播放优化
|
6
7
|
import 'log_util.dart';
|
b1869cf8
biao
背景音乐添加
|
8
9
10
11
12
13
14
15
16
17
|
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:语音跟读作答结果动效&语音
|
18
19
20
21
|
touch('touch_instrumental'),
excellent('excellent'),
great('great'),
good('good'),
|
819ae43b
吴启风
feat:体验优化-练习题目取消阻...
|
22
23
24
|
tryAgain('try_again'),
right('right'),
wrong('wrong');
|
b1869cf8
biao
背景音乐添加
|
25
26
27
28
29
30
|
const AudioPlayerUtilType(this.path);
final String path;
}
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
31
|
class AudioPlayerUtil extends WidgetsBindingObserver {
|
b1869cf8
biao
背景音乐添加
|
32
|
static AudioPlayerUtil? _instance;
|
c272c662
吴启风
fix: 崩溃修复
|
33
|
AudioPlayer? _audioPlayer;
|
b1869cf8
biao
背景音乐添加
|
34
|
late AudioPlayerUtilType currentType;
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
35
|
bool _wasPlaying = false;
|
2d1ead53
吴启风
feat:背景音播放优化
|
36
|
static const TAG = "AudioPlayerUtil";
|
b1869cf8
biao
背景音乐添加
|
37
38
39
|
// 私有构造函数
AudioPlayerUtil._internal() {
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
40
41
42
|
// 监听应用生命周期
WidgetsBinding.instance.addObserver(this);
_audioPlayer = AudioPlayer();
|
9e14f47a
吴启风
feat:解决开场音乐前点击(带音...
|
43
44
45
|
if (!BasicConfig.isEnvProd()) {
AudioLogger.logLevel = AudioLogLevel.info;
}
|
c272c662
吴启风
fix: 崩溃修复
|
46
|
_audioPlayer?.onPlayerStateChanged.listen((event) async {
|
9e14f47a
吴启风
feat:解决开场音乐前点击(带音...
|
47
|
Log.d("$TAG onPlayerStateChanged $event _wasPlaying=$_wasPlaying");
|
b1869cf8
biao
背景音乐添加
|
48
49
50
51
52
53
54
55
56
57
|
if (event == PlayerState.completed) {
// 播放结束再次播放
if (currentType == AudioPlayerUtilType.inMyTummy) {
AudioPlayerUtil.getInstance()
.playAudio(AudioPlayerUtilType.inMyTummy);
}
if (currentType == AudioPlayerUtilType.countWithMe) {
AudioPlayerUtil.getInstance()
.playAudio(AudioPlayerUtilType.countWithMe);
}
|
b1869cf8
biao
背景音乐添加
|
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
if (currentType == AudioPlayerUtilType.touch) {
AudioPlayerUtil.getInstance().playAudio(AudioPlayerUtilType.touch);
}
}
});
}
static AudioPlayerUtil getInstance() {
_instance ??= AudioPlayerUtil._internal();
return _instance!;
}
// 播放音频
Future<void> playAudio(AudioPlayerUtilType type) async {
|
9e14f47a
吴启风
feat:解决开场音乐前点击(带音...
|
72
|
Log.d('$TAG playAudio begin $type');
|
c272c662
吴启风
fix: 崩溃修复
|
73
74
75
76
|
if (_audioPlayer == null) {
Log.d('$TAG playAudio _audioPlayer is null, creating new one');
_audioPlayer = AudioPlayer();
}
|
b1869cf8
biao
背景音乐添加
|
77
78
|
currentType = type;
String path = type.path;
|
c272c662
吴启风
fix: 崩溃修复
|
79
80
81
82
83
84
|
try {
await _audioPlayer!.play(AssetSource(path.assetMp3), volume: 0.5);
await _audioPlayer!.onPlayerComplete.first;
} catch (e) {
Log.d('$TAG playAudio error: $e');
}
|
9e14f47a
吴启风
feat:解决开场音乐前点击(带音...
|
85
|
Log.d('$TAG playAudio end $type');
|
b1869cf8
biao
背景音乐添加
|
86
87
88
|
}
// stop
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
89
|
Future<void> stop() async {
|
c272c662
吴启风
fix: 崩溃修复
|
90
91
92
93
94
95
96
97
|
if (_audioPlayer == null) return;
Log.d("$TAG stop _audioPlayer.state=${_audioPlayer!.state}");
try {
if (_audioPlayer!.state == PlayerState.playing) {
await _audioPlayer!.stop();
}
} catch (e) {
Log.d("$TAG stop error: $e");
|
9e14f47a
吴启风
feat:解决开场音乐前点击(带音...
|
98
|
}
|
b1869cf8
biao
背景音乐添加
|
99
100
101
|
}
// pause
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
102
|
Future<void> pause() async {
|
c272c662
吴启风
fix: 崩溃修复
|
103
104
105
106
107
108
109
110
|
if (_audioPlayer == null) return;
Log.d("$TAG pause _audioPlayer.state=${_audioPlayer!.state}");
try {
if (_audioPlayer!.state == PlayerState.playing) {
await _audioPlayer!.pause();
}
} catch (e) {
Log.d("$TAG pause error: $e");
|
b1869cf8
biao
背景音乐添加
|
111
112
113
114
|
}
}
// resume
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
115
|
Future<void> resume() async {
|
c272c662
吴启风
fix: 崩溃修复
|
116
117
118
119
120
121
122
123
|
if (_audioPlayer == null) return;
Log.d("$TAG resume _audioPlayer.state=${_audioPlayer!.state}");
try {
if (_audioPlayer!.state == PlayerState.paused) {
await _audioPlayer!.resume();
}
} catch (e) {
Log.d("$TAG resume error: $e");
|
b1869cf8
biao
背景音乐添加
|
124
125
|
}
}
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
126
127
128
|
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
|
c272c662
吴启风
fix: 崩溃修复
|
129
130
|
if (_audioPlayer == null) return;
Log.d("$TAG didChangeAppLifecycleState appState=$state _wasPlaying=$_wasPlaying _audioPlayer.state=${_audioPlayer!.state}");
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
131
|
if (state == AppLifecycleState.paused) {
|
c272c662
吴启风
fix: 崩溃修复
|
132
|
if (_audioPlayer!.state == PlayerState.playing) {
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
133
134
135
136
137
138
139
140
141
142
143
|
_wasPlaying = true;
await pause();
};
} else if (state == AppLifecycleState.resumed) {
if (_wasPlaying == true) {
_wasPlaying = false;
await resume();
}
}
}
|
9e14f47a
吴启风
feat:解决开场音乐前点击(带音...
|
144
|
bool isPlaying() {
|
c272c662
吴启风
fix: 崩溃修复
|
145
|
return _audioPlayer?.state == PlayerState.playing;
|
9e14f47a
吴启风
feat:解决开场音乐前点击(带音...
|
146
147
|
}
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
148
|
void dispose() {
|
c272c662
吴启风
fix: 崩溃修复
|
149
150
151
152
153
|
if (_audioPlayer != null) {
Log.d("$TAG dispose _audioPlayer.state=${_audioPlayer!.state}");
_audioPlayer!.dispose();
_audioPlayer = null;
}
|
523864dd
吴启风
feat:音频播放单例增加声明周期感知
|
154
155
|
WidgetsBinding.instance.removeObserver(this);
}
|
b1869cf8
biao
背景音乐添加
|
156
|
}
|