How to play an audio from an API call response or save it int the App directory?

Hi there,

I'm facing some issues implementing Azure Text to Speech by using an API Call that returns an audio file.

I'm new on Dart / Flutterflow and I really don't know if it is a Flutterflow limitation or something I'm doing wrong.

I tried to play the audio directly from the api response using the loadFromByteData() method from audiofileplayer package but it didn't work. Then I tried to save the file in the app directory, in order to play it but it returns the error below.

This is my custom action:


import 'dart:io';
import 'dart:typed_data';
import 'package:http/http.dart' as http;
import 'package:audiofileplayer/audiofileplayer.dart';
import 'package:audiofileplayer/audio_system.dart';
import 'package:path_provider/path_provider.dart';

Future callAzureTtsApi() async {
  var token = await getAzureIssuer();

  String url = "https://eastus.tts.speech.microsoft.com/cognitiveservices/v1";

  var body =
      "<speak xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:mstts=\"http://www.w3.org/2001/mstts\" xmlns:emo=\"http://www.w3.org/2009/10/emotionml\" version=\"1.0\" xml:lang=\"pt-BR\"><voice name=\"pt-BR-FranciscaNeural\">Olá mundo!</voice></speak>";

  var response = await http.post(Uri.parse(url),
      headers: {
        "Authorization": "Bearer " + token,
        "X-Microsoft-OutputFormat": "audio-48khz-96kbitrate-mono-mp3",
        "Content-Type": "application/ssml+xml"
      },
      body: body);

  var bytes = response.bodyBytes;

  final tempDir = await getTemporaryDirectory();
  File file = await File('${tempDir.path}/audio.mp3').create();
  file.writeAsBytesSync(bytes);

  final audio = Audio.load(file.path);

  await audio.play();
  audio.dispose();

}

And this is the error Im getting:

MissingPluginException(No implementation found for method getTemporaryDirectory on channel plugins.flutter.io/path_provider)

I'll apreciate any help or advice.

Regards.

5
4 replies