import 'package:flutter_tts/flutter_tts.dart';

import '../main.dart';

class AudioPlayer {
  static Future<void> playAudio(String? audio) async {
    if (globalSharedPreferences.getBool('sound_on') != false && audio != null) {
      FlutterTts flutterTts = FlutterTts();
      var playText = audio;
      if (audio.contains('_')) {
        final lang = audio.split('_').first;
        playText = audio.replaceFirst('${lang}_', '');
        await flutterTts.setLanguage(lang);
      }
      await flutterTts.setSpeechRate(0.5);
      flutterTts.speak(playText);
    }
  }
}
