anasfik / openai

Dart/Flutter SDK for ChatGPT and all OpenAI APIs (GPT, Dall-e..)
https://pub.dev/packages/dart_openai
MIT License
539 stars 151 forks source link

text to speech model returning mpeg file, not mp3 #181

Open Lazizbek97 opened 2 weeks ago

Lazizbek97 commented 2 weeks ago

When we use text to speech model: model: "tts-1"; it is returning .mpeg file, and i cannot listen it on app. it should return mp3, so that i can listen it on app.

` // create speech from text Future createSpeech(String prompt) async { final appDir = await getApplicationDocumentsDirectory(); // The speech request. File speechFile = await OpenAI.instance.audio.createSpeech( model: "tts-1", input: prompt, voice: "nova", responseFormat: OpenAIAudioSpeechResponseFormat.mp3, outputFileName: "speech", outputDirectory: appDir, );

return speechFile;

} `

Padi142 commented 1 week ago

Hey! I've been able to play the mpeg file using the just_audio library and it works fine! I also tried just changing the file name to .mp3 and it worked as well.

dustinpham235 commented 6 days ago

@Padi142 would you mind sharing the code, I've tried what you suggested but none of it works. Thank you

Padi142 commented 6 days ago

Of course! I used the path_provider package to get a writable directory in my android app.

    final File speechFile = await OpenAI.instance.audio.createSpeech(
      model: 'tts-1-hd',
      input: 'Why do mice like cheese?',
      voice: 'nova',
      responseFormat: OpenAIAudioSpeechResponseFormat.mp3,
      outputDirectory: await getApplicationDocumentsDirectory(),
      outputFileName: 'output'
    );

This will create an audio file and returns its path with a file extension included. You can then use just_audio package like this:

final AudioPlayer player = AudioPlayer();
await player.setFilePath(speechFile.path);

The audio should start playing even tho the file extension is mpeg.