playAudio method

dynamic playAudio({
  1. required AudioPlayInfo source,
})

function for playing audio message source (required) audio message's source

Implementation

playAudio({
  required AudioPlayInfo source,
}) async {
  if (player?.state == PlayerState.playing) {
    console(logs: "audio playing stop first, stop messageID: ${_audioPlayInfo?.msgID}");
    await player?.stop();
    updateCurrentAudioPlayInfo(isCompleted: false, progress: _currentPlayAudioInfo?.progress ?? 0, isPaused: true);
    player!.seek(Duration.zero);
    _audioPlayInfo = null;
    _currentPlayAudioInfo = null;
  }
  console(logs: "current messageID: ${source.msgID}, audio path: ${source.path}");

  var allowext = ["mp3", 'wav', 'm4a'];
  if (source.type == AudioPlayType.path) {
    File f = File(source.path);
    var type = source.path.split(".").last.toLowerCase();
    var typeArr = type.split('?');

    if (typeArr.isNotEmpty) {
      type = typeArr[0];
    }
    if (!allowext.contains(type)) {
      return console(logs: "the audio type:$type is not allowed ");
    }
    if (f.existsSync()) {
      _audioPlayInfo = source;
      _currentPlayAudioInfo = CurrentPlayAudioInfo(
        progress: 0,
        playInfo: source,
        isCompleted: false,
        isPaused: false,
      );
      console(logs: "start play audio, messageID: ${source.msgID}");
      await player?.play(DeviceFileSource(source.path));
    } else {
      console(logs: "audio play by path. path not exists");
    }
  } else {
    var type = source.path.split(".").last.toLowerCase();
    var typeArr = type.split('?');

    if (typeArr.isNotEmpty) {
      type = typeArr[0];
    }

    if (!allowext.contains(type)) {
      return console(logs: "the audio type:$type is not allowed ");
    }
    _audioPlayInfo = source;
    _currentPlayAudioInfo = CurrentPlayAudioInfo(
      progress: 0,
      playInfo: source,
      isCompleted: false,
      isPaused: false,
    );
    await player?.play(UrlSource(source.path));
  }
}