搜索插件、代码仓与 APP 分类

插件详情v2.3.2

cloud_text_to_speech

Google、Microsoft 和 Amazon 文本转语音的单一接口。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
294
喜欢数
31
Pub 分数
120

安装插件

flutter pub add cloud_text_to_speech

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个统一的 Flutter 包,提供单一接口以集成来自 Google、Microsoft 和 Amazon 云服务的文本转语音功能。它通过抽象提供者特定的 API,简化了在多个平台上的语音输出实现。开发者可以仅通过少量代码更改在不同提供者之间切换,从而在需要自然语音合成的应用中实现可扩展且灵活的 TTS 解决方案。该包支持 Android、iOS、Web、macOS、Windows 和 Linux,非常适合跨平台语音应用。

适合场景

  • 跨平台语音助手
  • 带有音频旁白的电子学习应用
  • 应用中的无障碍功能
  • 多语言语音输出
  • 聊天机器人中的自动语音回复

主要特点

  • Google、Microsoft 和 AWS TTS 的统一 API
  • 支持多种语言和语音
  • 平台无关的实现
  • 轻松更换提供商
  • 内置错误处理和重试
  • 实时音频流支持

适合对象

  • 开发多提供商语音合成应用的开发者
  • 需要跨平台保持一致语音输出的应用
  • 需要快速原型设计语音特征的项目
  • 使用多个云TTS服务的团队

使用建议

  • 每个云提供商都需要 API 密钥
  • 计费基于使用量
  • 云调用需要网络连接
  • 有限的离线功能
  • 语音质量取决于所选提供商和地区

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Cloud Text-To-Speech

Pub Version Pub Likes Pub Popularity Pub Points GitHub License GitHub Sponsor Buy Me a Coffee

Single interface to Google, Microsoft, and Amazon Text-To-Speech. Flutter implementation of:

Features

  • Universal implementation for accessing all providers with one interface.
  • Separate implementation for every provider so we could access every functionality.
  • Sanitize SSML input per provider so we send only supported SSML elements.
  • Locale names in English and native language so we could display language selector.
  • Fake name generation for Google voices that are generated randomly based on voice locale.
  • Accessible configurable output format (per provider), rate, and pitch.

Feature Requests

We welcome and value your ideas and suggestions to improve this project! To submit and vote for feature requests, please visit our Feature Requests Board.

On the board, you can:

  • Submit new feature requests: Share your ideas on how we can enhance the project.
  • Vote on existing requests: Help prioritize the most popular features by voting for the ones you find most valuable.

Thank you for contributing to the development and improvement of Cloud Text-To-Speech!

Getting Started

There are essentially two ways to use Cloud Text-To-Speech:

  • Universal: Using TtsUniversal to be able to configure the TTS provider dynamically and us it.
    • Single: Using TtsProviders.google, TtsProviders.microsoft, TtsProviders.amazon to use the single provider at a time.
    • Combine: Using TtsProviders.combine to combine all providers and get all voices at once.
  • Provider: Using TtsGoogle, TtsMicrosoft, TtsAmazon to get the most from provider's API.
Universal(Single)

To init configuration use:

//Do init once and run it before any other method
TtsUniversal.init(
  provider: TtsProviders.amazon,
  googleParams: InitParamsGoogle(apiKey: 'API-KEY'),
  microsoftParams: InitParamsMicrosoft(
  subscriptionKey: 'SUBSCRIPTION-KEY', region: 'eastus'),
  amazonParams: InitParamsAmazon(
  keyId: 'KEY-ID', accessKey: 'ACCESS-KEY', region: 'us-east-1'),
  withLogs: true
);

To change provider use:

TtsUniversal.setProvider(TtsProviders.microsoft);

To get the list of all voices use:

//Get voices
final voicesResponse = await TtsUniversal.getVoices();

final voices = voicesResponse.voices;

//Print all available voices
print(voices);

//Pick an English Voice
final voice = voices
    .where((element) => element.locale.code.startsWith("en-"))
    .toList(growable: false)
    .first;

To convert TTS and get audio use:

//Generate Audio for a text
const text = "Amazon, Microsoft and Google Text-to-Speech API are awesome";

final ttsParams = TtsParamsUniversal(
        voice: voice, 
        audioFormat: AudioOutputFormatUniversal.mp3_64k, 
        text: text, 
        rate: 'slow', //optional
        pitch: 'default' //optional
      );

final ttsResponse = await TtsUniversal.convertTts(ttsParams);

//Get the audio bytes.
final audioBytes = ttsResponse.audio.buffer.asByteData();
Universal(Combine)

To init configuration use:

//Do init once and run it before any other method
TtsUniversal.init(
  provider: TtsProviders.combine,
  googleParams: InitParamsGoogle(apiKey: 'API-KEY'),
  microsoftParams: InitParamsMicrosoft(
  subscriptionKey: 'SUBSCRIPTION-KEY', region: 'eastus'),
  amazonParams: InitParamsAmazon(
  keyId: 'KEY-ID', accessKey: 'ACCESS-KEY', region: 'us-east-1'),
  withLogs: true
);

To change provider use:

TtsUniversal.setProvider(TtsProviders.combine);

To get the list of all voices use:

//Get voices
final voicesResponse = await TtsUniversal.getVoices();

final voices = voicesResponse.voices;

//Print all available voices
print(voices);

//Pick an English Voice
final voice = voices
    .where((element) => element.locale.code.startsWith("en-"))
    .toList(growable: false)
    .first;

To convert TTS and get audio use:

//Generate Audio for a text
const text = "Amazon, Microsoft and Google Text-to-Speech API are awesome";

final ttsParams = TtsParamsUniversal(
        voice: voice, 
        audioFormat: AudioOutputFormatUniversal.mp3_64k, 
        text: text, 
        rate: 'slow', //optional
        pitch: 'default' //optional
);

final ttsResponse = await TtsUniversal.convertTts(ttsParams);

//Get the audio bytes.
final audioBytes = ttsResponse.audio.buffer.asByteData();
Google

To init configuration use:

//Do init once and run it before any other method
TtsGoogle.init(
  params: InitParamsGoogle(apiKey: "API-KEY"), 
  withLogs:true
);

To get the list of all voices use:

//Get voices
final voicesResponse = await TtsGoogle.getVoices();

final voices = voicesResponse.voices;

//Print all voices
print(voices);

//Pick an English Voice
final voice = voices
    .where((element) => element.locale.code.startsWith("en-"))
    .toList(growable: false)
    .first;

To convert TTS and get audio use:

//Generate Audio for a text
final text = '<speak>Google<break time="2s"> Speech Service Text-to-Speech API is awesome!</speak>';

TtsParamsGoogle ttsParams = TtsParamsGoogle(
        voice: voice, 
        audioFormat: AudioOutputFormatGoogle.mp3, 
        text: text, 
        rate: 'slow', //optional
        pitch: 'default' //optional
      );

final ttsResponse = await TtsGoogle.convertTts(ttsParams);

//Get the audio bytes.
final audioBytes = ttsResponse.audio.buffer.asByteData();
Microsoft

To init configuration use:

//Do init once and run it before any other method
TtsMicrosoft.init(
  params: InitParamsMicrosoft(
  subscriptionKey: "SUBSCRIPTION-KEY", region: "eastus"),
  withLogs: true
);

To get the list of all voices use:

//Get voices
final voicesResponse = await TtsMicrosoft.getVoices();

final voices = voicesResponse.voices;

//Print all voices
print(voices);

//Pick an English Voice
final voice = voices
    .where((element) => element.locale.code.startsWith("en-"))
    .toList(growable: false)
    .first;

To convert TTS and get audio use:

//Generate Audio for a text
final text = '<speak>Microsoft<break time="2s"> Speech Service Text-to-Speech API is awesome!</speak>';

TtsParamsMicrosoft ttsParams = TtsParamsMicrosoft(
        voice: voice, 
        audioFormat: AudioOutputFormatMicrosoft.audio48Khz192kBitrateMonoMp3, 
        text: text, 
        rate: 'slow', //optional
        pitch: 'default' //optional
      );

final ttsResponse = await TtsMicrosoft.convertTts(ttsParams);

//Get the audio bytes.
final audioBytes = ttsResponse.audio.buffer.asByteData();
Amazon

To init configuration use:

//Do init once and run it before any other method
TtsAmazon.init(
  params: InitParamsAmazon(
    keyId: 'KEY-ID', 
    accessKey: 'ACCESS-KEY', 
    region: 'us-east-1'
  ),
  withLogs: true
);

To get the list of all voices use:

//Get voices
final voicesResponse = await TtsAmazon.getVoices();

final voices = voicesResponse.voices;

//Print all voices
print(voices);

//Pick an English Voice
final voice = voices
    .where((element) => element.locale.code.startsWith("en-"))
    .toList(growable: false)
    .first;

To convert TTS and get audio use:

//Generate Audio for a text
final text = '<speak>Amazon<break time="2s"> Speech Service Text-to-Speech API is awesome!</speak>';

TtsParamsAmazon ttsParams = TtsParamsAmazon(
        voice: voice, 
        audioFormat: AudioOutputFormatAmazon.audio48Khz192kBitrateMonoMp3, 
        text: text, 
        rate: 'slow', //optional
        pitch: 'default' //optional
);

final ttsResponse = await TtsAmazon.convertTts(ttsParams);

//Get the audio bytes.
final audioBytes = ttsResponse.audio.buffer.asByteData();

Notes

There are things you should take care of:

  • Securing of your API keys and credentials, they could be extracted from your mobile or web app.
  • Sometimes Amazon Polly is not working in emulator, so you could get 403 error.
  • For fixing SSML/XML before passing it to TTS Params, you could use the xml package's, methods XmlDocument.parse(ssml).toXmlString().
  • Audio has uniform format for all providers, it is Uint8List that you could use to play it or save it to file.
  • Some player packages that are good fit are: audioplayers and assets_audio_player.

所属分类

依赖

依赖清单 6 项

源码仓库

使用此插件的项目

暂无相关数据。