audio_player_util.dart
3.33 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
import 'package:wow_english/common/extension/string_extension.dart';
import 'log_util.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'),
  excellent('excellent'),
  great('great'),
  good('good'),
  tryAgain('try_again'),
  right('right'),
  wrong('wrong');
  const AudioPlayerUtilType(this.path);
  final String path;
}
class AudioPlayerUtil extends WidgetsBindingObserver {
  static AudioPlayerUtil? _instance;
  late AudioPlayer _audioPlayer;
  late AudioPlayerUtilType currentType;
  bool _wasPlaying = false;
  static const TAG = "AudioPlayerUtil";
  // 私有构造函数
  AudioPlayerUtil._internal() {
    // 监听应用生命周期
    WidgetsBinding.instance.addObserver(this);
    _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.touch) {
          AudioPlayerUtil.getInstance().playAudio(AudioPlayerUtilType.touch);
        }
      }
    });
  }
  static AudioPlayerUtil getInstance() {
    _instance ??= AudioPlayerUtil._internal();
    return _instance!;
  }
// 播放音频
  Future<void> playAudio(AudioPlayerUtilType type) async {
    Log.d("$TAG playAudio $type");
    currentType = type;
    String path = type.path;
    await _audioPlayer.play(AssetSource(path.assetMp3), volume: 0.5);
    await _audioPlayer.onPlayerComplete.first;
  }
  // stop
  Future<void> stop() async {
    Log.d("$TAG stop _audioPlayer.state=${_audioPlayer.state}");
    await _audioPlayer.stop();
  }
  // pause
  Future<void> pause() async {
    Log.d("$TAG pause _audioPlayer.state=${_audioPlayer.state}");
    if (_audioPlayer.state == PlayerState.playing) {
      await _audioPlayer.pause();
    }
  }
  // resume
  Future<void> resume() async {
    Log.d("$TAG resume _audioPlayer.state=${_audioPlayer.state}");
    if (_audioPlayer.state == PlayerState.paused) {
      await _audioPlayer.resume();
    }
  }
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    Log.d("$TAG didChangeAppLifecycleState appState=$state _wasPlaying=$_wasPlaying _audioPlayer.state=${_audioPlayer.state}");
    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() {
    Log.d("$TAG dispose _audioPlayer.state=${_audioPlayer.state}");
    _audioPlayer.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }
}