text_to_speech_api 0.1.0 text_to_speech_api: ^0.1.0 copied to clipboard
A simple wrapper for accessing Google's Text-To-Spech API and converting the results to MP3 files.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:text_to_speech_api/text_to_speech_api.dart';
import 'package:audioplayer/audioplayer.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Text To Speech Example'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextToSpeechService _service = TextToSpeechService('Your api key');
AudioPlayer _audioPlayer = AudioPlayer();
void _playDemo() async {
File file = await _service.textToSpeech(text: 'Hello World');
_audioPlayer.play(file.path, isLocal: true);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Press the play button to speak a demo text.',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _playDemo,
tooltip: 'Play Demo',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}