elevenlabs 1.0.0 elevenlabs: ^1.0.0 copied to clipboard
A simple wrapper for accessing ElevenLabs's Text-To-Spech API and converting the results to MP3 files.
import 'package:audioplayers/audioplayers.dart';
import 'package:elevenlabs/elevenlabs.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await dotenv.load();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Text To Speech Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
final ElevenLabsTTS elevenlabs = ElevenLabsTTS(apiKey: dotenv.get("API_KEY"));
final audioPlayer = AudioPlayer();
String text =
"Once men turned their thinking over to machines in the hope that this would set them free. But that only permitted other men with machines to enslave them.";
_playDemo() async {
elevenlabs.textToSpeech(
text: text,
voiceId: "ErXwobaYiN019PkySvjV",
stability: 0.01,
similarityBoost: 0.6,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
text,
style: const TextStyle(
fontSize: 15,
color: Colors.white,
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _playDemo,
tooltip: 'Play Demo',
child: const Icon(Icons.arrow_right_alt_outlined),
),
);
}
}