playAudio method
Plays audio data.
Args: audioData (Uint8List): The audio data to play. sampleRate (int?): The sample rate of the audio data. Defaults to the instance's sample rate. bitDepth (int?): The bit depth of the audio data. Defaults to the instance's bit depth.
Implementation
Future<void> playAudio(Uint8List audioData,
{int? sampleRate, int? bitDepth}) async {
final tempDir = await getTemporaryDirectory();
File file = await File('${tempDir.path}/audio.wav').create();
await file.writeAsBytes(_bytesToWav(
audioData, bitDepth ?? _bitDepth, sampleRate ?? _sampleRate));
try {
AudioPlayer player = AudioPlayer(handleAudioSessionActivation: false);
await player.setAudioSource(AudioSource.file(file.path));
await player.play();
await player.dispose();
} catch (error) {
logger.warning("Error playing audio. $error");
}
}