audio_player_util.dart 3.33 KB
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);
  }
}