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");
    _audioPlayInfo = null;
    _currentPlayAudioInfo = null;
    await player?.stop();
  }
  console(logs: "current audio path: ${source.path}");
  if (source.type == AudioPlayType.path) {
    File f = File(source.path);
    var allowext = ["mp3", 'wav'];
    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;
      console(logs: "start play audio");
      await player?.play(DeviceFileSource(source.path));
    } else {
      console(logs: "audio play by path. path not exists");
    }
  } else {
    var allowext = ["mp3", 'wav'];

    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;
    await player?.play(UrlSource(source.path));
  }
}