Flutter play audio from file

70 Views Asked by At

I have a flutter app and I am getting a wav file from a cloud function which I am receiving as bytes. Then I am passing that file to following function:

void _playAudio(Uint8List audioData) async {
    debugPrint('audioData.isEmpty ${audioData.isEmpty}');
    try {
      // Get temporary directory
      final tempDir = await getTemporaryDirectory();
      final filePath = '${tempDir.path}/tempAudio.wav';

      // Write the bytes (audioData) to the file
      final file = File(filePath);
      await file.writeAsBytes(audioData);

      await _player.setFilePath(filePath);

      // Play the audio from the file path
      // await _player.play(DeviceFileSource(filePath));
      await _player.play();
    } catch (e) {
      debugPrint("Error playing audio: $e");
    }
  }

In here I am using just_audio: ^0.9.36 and flutter 3.16.5 and dart 3.2.3.

I also attempted this with audioplayers: ^5.2.1 and had not luck.

The goal of this code is to receive audio and when it receives it from the cloud function it plays it.

I tried playing the file with both just_audio: ^0.9.36 and audioplayers: ^5.2.1 and nothing happened. Although I think that I did not get an error with audioplayers at some point there was no sound coming up and when I hot realoaded had errors again.

0

There are 0 best solutions below