您可以让 Gemini TTS 模型根据文本提示词生成语音(音频)输出。使用 Firebase AI Logic 时,您可以直接从应用中发出此请求。
文字转语音 (TTS) 生成是可控的,这意味着您需要提供确切的文本来合成语音。此外,您还可以在提示中使用自然语言来引导音频输出的风格、口音、语速和语气。您可以将 TTS 视为转写(语音转文字)的反向过程。
您可以使用任何 Gemini -tts 模型来使用此功能,这些模型针对高质量、低延迟的语音生成进行了优化。
借助此功能,您可以执行以下操作:
互动式讲故事:打造沉浸式有声读物或角色扮演游戏,让模型根据不同角色切换声音,或根据故事情节调整语气(例如在悬疑场景中低语,或在讲笑话时大笑)。
语言学习:构建发音指南,以特定的地区口音或较慢的语速朗读文本,帮助学习者练习困难的发音。
情境感知内容朗读器:使用与内容相匹配的语音角色和情感语调(例如,针对重大新闻使用严肃的语调,针对分步烹饪说明使用温暖、耐心的语调)大声朗读新闻报道、食谱或博文。
本指南介绍了如何通过单发言人或多发言人文本输入生成语音,以及如何流式传输音频回答。
TTS 与Live API之间的比较
文字转语音 (TTS) 模型和 Live API 模型都是低延迟的语音生成模型,可以针对不同的回答语音和语言进行配置。不过,两者的应用场景大不相同。
文字转语音 (TTS) 生成是一种单向请求-响应互动(输入文本,输出音频)。它专为需要精确朗读所提供文本的场景而设计,可对风格和声音进行精细控制,例如播客解说、有声读物或朗读文章。
Live API 生成支持双向流式传输,可实现实时语音对话(语音输入、语音输出)。它在动态对话场景中表现出色,模型可以决定返回适用的语音。请注意,最新的 Live API 模型还支持视频和图片输入。
准备工作
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
如果您尚未完成入门指南,请先完成该指南,其中介绍了如何设置 Firebase 项目、将应用连接到 Firebase、添加 SDK、为所选的 Gemini API 提供程序初始化后端服务,以及创建 GenerativeModel 实例。
如需测试和迭代提示,我们建议使用 Google AI Studio。
支持此功能的模型
gemini-3.1-flash-tts-preview
根据文本生成语音
您可以使用 Gemini TTS 模型根据提供的文本生成语音。
生成单人语音
|
在试用此示例之前,请先完成本指南的准备工作部分,以设置您的项目和应用。 在该部分中,您还需要点击所选 Gemini API 提供商对应的按钮,以便在此页面上查看特定于提供商的内容。 |
您可以将模型配置为使用单一声音输出音频。
在 GenerationConfig 中,添加以下内容:
将
responseModalities设置为包含AUDIO。使用以下命令配置
SpeechConfig:(必需) 响应语音名称(例如
Kore)(可选) 语言代码。
如果您未指定语言,Gemini TTS 模型可以自动检测提示中的语言。
使用文本提示词调用 generateContent。模型会在回答部分中返回原始 PCM 音频数据。
Swift
import FirebaseAILogic
// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
let config = GenerationConfig(
responseModalities: [.audio],
speechConfig: SpeechConfig(voiceName: "Kore", languageCode: "en-US")
)
// Create a `GenerativeModel` instance with a model that supports speech generation.
let model = ai.generativeModel(
modelName: "gemini-3.1-flash-tts-preview",
generationConfig: config
)
// Provide a text prompt.
let prompt = "Say cheerfully: Have a wonderful day!"
// Call `generateContent` to generate the speech output based on your text prompt.
let response = try await model.generateContent(prompt)
// Extract the audio data and handle it for downstream use. For example:
for part in response.inlineDataParts {
let data = part.data // Raw PCM audio bytes (24kHz, 1 channel, 16-bit)
let mimeType = part.mimeType // for example: "audio/pcm"
print("Received audio data with MIME type: \(mimeType)")
// To play back raw PCM audio bytes, you'll need to write your own `playRawPcm` function.
playRawPcm(data: data)
}
Kotlin
对于 Kotlin,此 SDK 中的方法是挂起函数,需要从协程作用域中调用。
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
val config = generationConfig {
responseModalities = listOf(ResponseModality.AUDIO)
speechConfig = SpeechConfig(
voice = Voice("Kore"),
languageCode = "en-US"
)
}
// Initialize the Gemini Developer API backend service.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "gemini-3.1-flash-tts-preview",
generationConfig = config
)
// Provide a text prompt.
val prompt = "Say cheerfully: Have a wonderful day!"
// Call `generateContent` to generate the speech output based on your text prompt.
val response = model.generateContent(prompt)
// Extract the audio data and handle it for downstream use. For example:
val part = response.candidates.firstOrNull()?.content?.parts?.firstOrNull()
if (part is InlineDataPart) {
val pcmData = part.inlineData // Raw PCM bytes (24kHz, 1 channel, 16-bit)
val mimeType = part.mimeType // for example: "audio/pcm"
// To play back PCM audio data, you'll need to write your own `playAudio` function.
playAudio(pcmData)
}
Java
对于 Java,此 SDK 中的流式传输方法会返回 Reactive Streams 库中的Publisher 类型。
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
GenerationConfig config = new GenerationConfig.Builder()
.setResponseModalities(Collections.singletonList(ResponseModality.AUDIO))
.setSpeechConfig(new SpeechConfig(new Voice("Kore"), "en-US"))
.build();
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-3.1-flash-tts-preview", config);
// Use the GenerativeModelFutures Java compatibility layer.
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
// Provide a text prompt.
String prompt = "Say cheerfully: Have a wonderful day!";
Content content = new Content.Builder().addText(prompt).build();
Executor executor = Executors.newSingleThreadExecutor();
// Call `generateContent` to generate the speech output based on your text prompt.
// Extract the audio data and handle it for downstream use.
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
Part part = result.getCandidates().get(0).getContent().getParts().get(0);
if (part instanceof InlineDataPart) {
byte[] pcmData = ((InlineDataPart) part).getInlineData();
String mimeType = ((InlineDataPart) part).getMimeType();
// To play back PCM audio data, you'll need to write your own `playAudio` function.
playAudio(pcmData);
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend, ResponseModality } from "firebase/ai";
// TODO(developer): Replace with your app's Firebase configuration
const firebaseConfig = { /* ... */ };
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
const generationConfig = {
responseModalities: [ResponseModality.AUDIO],
speechConfig: {
voiceConfig: { prebuiltVoiceConfig: { voiceName: "Kore" } },
languageCode: "en-US"
}
};
// Create a `GenerativeModel` instance with a model that supports speech generation.
const model = getGenerativeModel(ai, {
model: "gemini-3.1-flash-tts-preview",
generationConfig
});
// Provide a text prompt.
const prompt = "Say cheerfully: Have a wonderful day!";
// Call `generateContent` to generate the speech output based on your text prompt.
const result = await model.generateContent(prompt);
const inlineDataParts = result.response.inlineDataParts();
// Extract the audio data and handle it for downstream use. For example:
if (inlineDataParts?.[0]) {
const pcmBase64 = inlineDataParts[0].inlineData.data;
// Decode base64 to ArrayBuffer
const pcmBuffer = Uint8Array.from(atob(pcmBase64), c => c.charCodeAt(0)).buffer;
// To play back a PCM buffer, you'll need to write your own `playAudio` function.
playAudio(pcmBuffer);
}
Dart
import 'package:firebase_ai/firebase_ai.dart';
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
final config = GenerationConfig(
responseModalities: [ResponseModalities.audio],
speechConfig: SpeechConfig(voiceName: 'Kore', languageCode: 'en-US'),
);
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
final model = FirebaseAI.googleAI().generativeModel(
model: 'gemini-3.1-flash-tts-preview',
generationConfig: config,
);
// Provide a text prompt.
final prompt = 'Say cheerfully: Have a wonderful day!';
// Call `generateContent` to generate the speech output based on your text prompt.
final response = await model.generateContent([Content.text(prompt)]);
// Extract the audio data and handle it for downstream use. For example:
final part = response.candidates.first.content.parts.first;
if (part is InlineDataPart && part.mimeType.startsWith('audio/')) {
final Uint8List pcmData = part.bytes; // Raw PCM bytes (24kHz, 1 channel, 16-bit)
// To play back PCM audio data, you'll need to write your own `playAudio` function.
await playAudio(pcmData);
}
Unity
using Firebase.AI;
// Set `responseModalities` to include `Audio`.
// Configure a `SpeechConfig` with your chosen voice name and language code.
var config = new GenerationConfig(
responseModalities: new System.Collections.Generic.List<ResponseModality> { ResponseModality.Audio },
speechConfig: SpeechConfig.UsePrebuiltVoice("Kore", "en-US")
);
// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports speech generation.
var model = ai.GetGenerativeModel(
modelName: "gemini-3.1-flash-tts-preview",
generationConfig: config
);
// Provide a text prompt.
var prompt = "Say cheerfully: Have a wonderful day!";
// Call `GenerateContentAsync` to generate the speech output based on your text prompt.
var response = await model.GenerateContentAsync(prompt);
// Extract the audio data and handle it for downstream use. For example:
if (response.Candidates.Count > 0) {
foreach (var part in response.Candidates[0].Content.Parts) {
if (part is ModelContent.InlineDataPart inlineData) {
byte[] pcmData = inlineData.Data; // Raw PCM bytes (24kHz, 1 channel, 16-bit)
// To play back PCM audio data, you'll need to write your own `playAudio` function.
playAudio(pcmData);
}
}
}
生成包含多个发言人的语音
|
在试用此示例之前,请先完成本指南的准备工作部分,以设置您的项目和应用。 在该部分中,您还需要点击所选 Gemini API 提供商对应的按钮,以便在此页面上查看特定于提供商的内容。 |
您可以将模型配置为针对文本中的不同说话者使用不同的语音。这对于为对话或交谈生成音频非常有用。
创建一个
MultiSpeakerVoiceConfig,用于将说话者姓名(您将在提示中使用)映射到特定的回答语音名称(例如Kore)。多发言人配置仅支持 2 位发言人。
在
GenerationConfig中,添加以下内容:将
responseModalities设置为包含AUDIO。使用以下命令配置
SpeechConfig:(必需)传入您的
MultiSpeakerVoiceConfig。(可选) 语言代码。
如果您未指定语言,Gemini TTS 模型可以自动检测提示中的语言。
在提示中,使用演讲者姓名作为前缀(例如
Joe: Hello. Jane: Hi.)来指明发言者。
使用文本提示词调用 generateContent。模型会在回答部分中返回原始 PCM 音频数据。
Swift
import FirebaseAILogic
// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
let multiSpeechConfig = SpeechConfig(
multiSpeakerVoiceConfig: MultiSpeakerVoiceConfig(
speakerVoiceConfigs: [
SpeakerVoiceConfig(speaker: "Joe", voiceName: "Puck"),
SpeakerVoiceConfig(speaker: "Jane", voiceName: "Kore")
]
),
languageCode: "en-US"
)
// Set `responseModalities` to include `audio`.
let config = GenerationConfig(
responseModalities: [.audio],
speechConfig: multiSpeechConfig
)
// Create a `GenerativeModel` instance with a model that supports speech generation.
let model = ai.generativeModel(
modelName: "gemini-3.1-flash-tts-preview",
generationConfig: config
)
// Provide a text prompt that includes the names of the speakers.
let prompt = """
Joe: How's it going today Jane?
Jane: Not too bad, how about you?
"""
// Call `generateContent` to generate the speech output based on your text prompt.
let response = try await model.generateContent(prompt)
// Extract the audio data and handle it for downstream use. For example:
for part in response.inlineDataParts {
let data = part.data // Raw PCM audio bytes (24kHz, 1 channel, 16-bit)
let mimeType = part.mimeType // for example: "audio/pcm"
print("Received audio data with MIME type: \(mimeType)")
// To play back raw PCM audio bytes, you'll need to write your own `playRawPcm` function.
playRawPcm(data: data)
}
Kotlin
对于 Kotlin,此 SDK 中的方法是挂起函数,需要从协程作用域中调用。
// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
val multiSpeechConfig = SpeechConfig(
multiSpeakerVoiceConfig = MultiSpeakerVoiceConfig(
speakerVoiceConfigs = listOf(
SpeakerVoiceConfig(speaker = "Joe", voice = Voice("Puck")),
SpeakerVoiceConfig(speaker = "Jane", voice = Voice("Kore"))
)
),
languageCode = "en-US"
)
// Set `responseModalities` to include `AUDIO`.
val config = generationConfig {
responseModalities = listOf(ResponseModality.AUDIO)
speechConfig = multiSpeechConfig
}
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "gemini-3.1-flash-tts-preview",
generationConfig = config
)
// Provide a text prompt that includes the names of the speakers.
val prompt = """
Joe: How's it going today Jane?
Jane: Not too bad, how about you?
"""
// Call `generateContent` to generate the speech output based on your text prompt.
val response = model.generateContent(prompt)
// Extract the audio data and handle it for downstream use. For example:
val part = response.candidates.firstOrNull()?.content?.parts?.firstOrNull()
if (part is InlineDataPart) {
val pcmData = part.inlineData // Raw PCM bytes (24kHz, 1 channel, 16-bit)
val mimeType = part.mimeType // for example: "audio/pcm"
// To play back PCM audio data, you'll need to write your own `playAudio` function.
playAudio(pcmData)
}
Java
对于 Java,此 SDK 中的流式传输方法会返回 Reactive Streams 库中的Publisher 类型。
// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
MultiSpeakerVoiceConfig multiSpeakerVoiceConfig = new MultiSpeakerVoiceConfig(
Arrays.asList(
new SpeakerVoiceConfig("Joe", new Voice("Puck")),
new SpeakerVoiceConfig("Jane", new Voice("Kore"))
)
);
SpeechConfig multiSpeechConfig = new SpeechConfig(multiSpeakerVoiceConfig);
// Set `responseModalities` to include `AUDIO`.
GenerationConfig config = new GenerationConfig.Builder()
.setResponseModalities(Collections.singletonList(ResponseModality.AUDIO))
.setSpeechConfig(multiSpeechConfig)
.build();
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-3.1-flash-tts-preview", config);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
// Provide a text prompt that includes the names of the speakers.
String prompt = "Joe: How's it going today Jane?\nJane: Not too bad, how about you?";
Content content = new Content.Builder().addText(prompt).build();
Executor executor = Executors.newSingleThreadExecutor();
// Call `generateContent` to generate the speech output based on your text prompt.
// Extract the audio data and handle it for downstream use.
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
Part part = result.getCandidates().get(0).getContent().getParts().get(0);
if (part instanceof InlineDataPart) {
byte[] pcmData = ((InlineDataPart) part).getInlineData(); // Raw PCM bytes (24kHz, 1 channel, 16-bit)
String mimeType = ((InlineDataPart) part).getMimeType(); // for example: "audio/pcm"
// To play back PCM audio data, you'll need to write your own `playAudio` function.
playAudio(pcmData);
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend, ResponseModality } from "firebase/ai";
// TODO(developer): Replace with your app's Firebase configuration
const firebaseConfig = { /* ... */ };
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
const generationConfig = {
responseModalities: [ResponseModality.AUDIO],
speechConfig: {
multiSpeakerVoiceConfig: {
speakerVoiceConfigs: [
{ speaker: "Joe", voiceConfig: { prebuiltVoiceConfig: { voiceName: "Puck" } } },
{ speaker: "Jane", voiceConfig: { prebuiltVoiceConfig: { voiceName: "Kore" } } }
]
},
languageCode: "en-US"
}
};
// Create a `GenerativeModel` instance with a model that supports speech generation.
const model = getGenerativeModel(ai, {
model: "gemini-3.1-flash-tts-preview",
generationConfig
});
// Provide a text prompt that includes the names of the speakers.
const prompt = `
Joe: How's it going today Jane?
Jane: Not too bad, how about you?
`;
// Call `generateContent` to generate the speech output based on your text prompt.
const result = await model.generateContent(prompt);
const inlineDataParts = result.response.inlineDataParts();
// Extract the audio data and handle it for downstream use. For example:
if (inlineDataParts?.[0]) {
const pcmBase64 = inlineDataParts[0].inlineData.data; // Raw PCM bytes (24kHz, 1 channel, 16-bit)
const pcmBuffer = Uint8Array.from(atob(pcmBase64), c => c.charCodeAt(0)).buffer;
// To play back a PCM buffer, you'll need to write your own `playAudio` function.
playAudio(pcmBuffer);
}
Dart
import 'package:firebase_ai/firebase_ai.dart';
// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
final multiSpeechConfig = SpeechConfig.multiSpeaker(
multiSpeakerVoiceConfig: MultiSpeakerVoiceConfig(
speakerVoiceConfigs: [
SpeakerVoiceConfig(speaker: 'Joe', voiceName: 'Puck'),
SpeakerVoiceConfig(speaker: 'Jane', voiceName: 'Kore'),
],
),
languageCode: 'en-US',
);
// Set `responseModalities` to include `audio`.
final config = GenerationConfig(
responseModalities: [ResponseModalities.audio],
speechConfig: multiSpeechConfig,
);
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
final model = FirebaseAI.googleAI().generativeModel(
model: 'gemini-3.1-flash-tts-preview',
generationConfig: config,
);
// Provide a text prompt that includes the names of the speakers.
final prompt = '''
Joe: How's it going today Jane?
Jane: Not too bad, how about you?
''';
// Call `generateContent` to generate the speech output based on your text prompt.
final response = await model.generateContent([Content.text(prompt)]);
// Extract the audio data and handle it for downstream use. For example:
final part = response.candidates.first.content.parts.first;
if (part is InlineDataPart && part.mimeType.startsWith('audio/')) {
final Uint8List pcmData = part.bytes; // Raw PCM bytes (24kHz, 1 channel, 16-bit)
// To play back PCM audio data, you'll need to write your own `playAudio` function.
await playAudio(pcmData);
}
Unity
using Firebase.AI;
// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
var multiSpeakerVoiceConfig = new MultiSpeakerVoiceConfig(
new System.Collections.Generic.List<SpeakerVoiceConfig> {
SpeakerVoiceConfig.UsePrebuiltVoice("Joe", "Puck"),
SpeakerVoiceConfig.UsePrebuiltVoice("Jane", "Kore")
}
);
var multiSpeechConfig = SpeechConfig.UseMultiSpeakerVoice(multiSpeakerVoiceConfig);
// Set `responseModalities` to include `Audio`.
var config = new GenerationConfig(
responseModalities: new System.Collections.Generic.List<ResponseModality> { ResponseModality.Audio },
speechConfig: multiSpeechConfig
);
// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports speech generation.
var model = ai.GetGenerativeModel(
modelName: "gemini-3.1-flash-tts-preview",
generationConfig: config
);
// Provide a text prompt that includes the names of the speakers.
var prompt = "Joe: How's it going today Jane?\nJane: Not too bad, how about you?";
// Call `GenerateContentAsync` to generate the speech output based on your text prompt.
var response = await model.GenerateContentAsync(prompt);
// Extract the audio data and handle it for downstream use. For example:
if (response.Candidates.Count > 0) {
foreach (var part in response.Candidates[0].Content.Parts) {
if (part is ModelContent.InlineDataPart inlineData) {
byte[] pcmData = inlineData.Data; // Raw PCM bytes (24kHz, 1 channel, 16-bit)
// To play back PCM audio data, you'll need to write your own `playAudio` function.
playAudio(pcmData);
}
}
}
流式传输回答
|
在试用此示例之前,请先完成本指南的准备工作部分,以设置您的项目和应用。 在该部分中,您还需要点击所选 Gemini API 提供商对应的按钮,以便在此页面上查看特定于提供商的内容。 |
您可以在生成音频回答时对其进行流式传输,而不是等待整个音频文件生成完毕,从而实现更快的互动和更低的延迟。
单说话人和多说话人配置均支持流式传输生成的语音。只有在使用 Gemini 3.x TTS 模型时才支持此功能。
如需流式传输语音回答,请调用 generateContentStream 而不是 generateContent,并在收到数据块时进行处理。以下示例展示了如何流式传输单人回答:
Swift
import FirebaseAILogic
// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Set `responseModalities` to include `audio`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
let config = GenerationConfig(
responseModalities: [.audio],
speechConfig: SpeechConfig(voiceName: "Kore")
)
// Create a `GenerativeModel` instance with a model that supports speech generation.
let model = ai.generativeModel(
modelName: "gemini-3.1-flash-tts-preview",
generationConfig: config
)
// Provide a text prompt.
let prompt = "Tell me a story about a brave knight."
// Call `generateContentStream` to generate the speech output stream based on your text prompt.
let responseStream = try model.generateContentStream(prompt)
// Extract the audio data and handle it for downstream use. For example:
for try await chunk in responseStream {
for part in chunk.inlineDataParts {
let data = part.data // Raw PCM audio bytes (24kHz, 1 channel, 16-bit)
let mimeType = part.mimeType // for example: "audio/pcm"
// Append the audio chunk to your audio queue/buffer for playback.
appendAudioChunk(data)
}
}
Kotlin
对于 Kotlin,此 SDK 中的方法是挂起函数,需要从协程作用域中调用。
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
val config = generationConfig {
responseModalities = listOf(ResponseModality.AUDIO)
speechConfig = SpeechConfig(voice = Voice("Kore"))
}
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "gemini-3.1-flash-tts-preview",
generationConfig = config
)
// Provide a text prompt.
val prompt = "Tell me a story about a brave knight."
// Call `generateContentStream` to generate the speech output stream based on your text prompt.
// Extract the audio data and handle it for downstream use.
model.generateContentStream(prompt).collect { chunk ->
val part = chunk.candidates.firstOrNull()?.content?.parts?.firstOrNull()
if (part is InlineDataPart) {
val pcmChunk = part.inlineData // Raw PCM bytes (24kHz, 1 channel, 16-bit)
val mimeType = part.mimeType // for example: "audio/pcm"
// Append the audio chunk to your audio queue/buffer for playback.
appendAudioChunk(pcmChunk)
}
}
Java
对于 Java,此 SDK 中的流式传输方法会返回 Reactive Streams 库中的Publisher 类型。
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
GenerationConfig config = new GenerationConfig.Builder()
.setResponseModalities(Collections.singletonList(ResponseModality.AUDIO))
.setSpeechConfig(new SpeechConfig(new Voice("Kore")))
.build();
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-3.1-flash-tts-preview", config);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
// Provide a text prompt.
String prompt = "Tell me a story about a brave knight.";
Content content = new Content.Builder().addText(prompt).build();
// Call `generateContentStream` to generate the speech output stream based on your text prompt.
Publisher<GenerateContentResponse> streamingResponse =
model.generateContentStream(content);
// Extract the audio data and handle it for downstream use.
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(GenerateContentResponse chunk) {
Part part = chunk.getCandidates().get(0).getContent().getParts().get(0);
if (part instanceof InlineDataPart) {
byte[] pcmChunk = ((InlineDataPart) part).getInlineData(); // Raw PCM bytes (24kHz, 1 channel, 16-bit)
String mimeType = ((InlineDataPart) part).getMimeType(); // for example: "audio/pcm"
// Append the audio chunk to your audio queue/buffer for playback.
appendAudioChunk(pcmChunk);
}
}
@Override
public void onComplete() {
// Audio stream complete.
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
});
Web
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend, ResponseModality } from "firebase/ai";
// TODO(developer): Replace with your app's Firebase configuration
const firebaseConfig = { /* ... */ };
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
const generationConfig = {
responseModalities: [ResponseModality.AUDIO],
speechConfig: {
voiceConfig: { prebuiltVoiceConfig: { voiceName: "Kore" } }
}
};
// Create a `GenerativeModel` instance with a model that supports speech generation.
const model = getGenerativeModel(ai, {
model: "gemini-3.1-flash-tts-preview",
generationConfig
});
// Provide a text prompt.
const prompt = "Tell me a story about a brave knight.";
// Call `generateContentStream` to generate the speech output stream based on your text prompt.
const result = await model.generateContentStream(prompt);
// Extract the audio data and handle it for downstream use. For example:
const playbackQueue = [];
for await (const chunk of result.stream) {
const inlineDataParts = chunk.inlineDataParts();
if (inlineDataParts?.[0]) {
const pcmBase64 = inlineDataParts[0].inlineData.data; // Raw PCM bytes (24kHz, 1 channel, 16-bit)
const pcmBuffer = Uint8Array.from(atob(pcmBase64), c => c.charCodeAt(0)).buffer;
// Append the audio chunk to your audio queue/buffer for playback.
playbackQueue.push(pcmBuffer);
}
}
// To play back an array of PCM buffers in sequence, you'll need to write your own `processPlaybackQueue` function.
processPlaybackQueue(playbackQueue);
Dart
import 'package:firebase_ai/firebase_ai.dart';
// Set `responseModalities` to include `audio`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
final config = GenerationConfig(
responseModalities: [ResponseModalities.audio],
speechConfig: SpeechConfig(voiceName: 'Kore'),
);
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
final model = FirebaseAI.googleAI().generativeModel(
model: 'gemini-3.1-flash-tts-preview',
generationConfig: config,
);
// Provide a text prompt.
final prompt = 'Tell me a story about a brave knight.';
// Call `generateContentStream` to generate the speech output stream based on your text prompt.
final responseStream = model.generateContentStream([Content.text(prompt)]);
// Extract the audio data and handle it for downstream use. For example:
await for (final chunk in responseStream) {
final part = chunk.candidates.first.content.parts.first;
if (part is InlineDataPart && part.mimeType.startsWith('audio/')) {
final Uint8List pcmChunk = part.bytes; // Raw PCM bytes (24kHz, 1 channel, 16-bit)
// Append the audio chunk to your audio queue/buffer for playback.
appendAudioChunk(pcmChunk);
}
}
Unity
using System.Collections.Generic;
using System.Linq;
using Firebase.AI;
// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Set `ResponseModalities` to include `Audio`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
var config = new GenerationConfig(
responseModalities: new List<ResponseModality> { ResponseModality.Audio },
speechConfig: SpeechConfig.UsePrebuiltVoice("Kore")
);
// Create a `GenerativeModel` instance with a model that supports speech generation.
var model = ai.GetGenerativeModel(
modelName: "gemini-3.1-flash-tts-preview",
generationConfig: config
);
// Provide a text prompt.
var prompt = "Tell me a story about a brave knight.";
// Call `GenerateContentStreamAsync` to generate the speech output stream based on your text prompt.
var responseStream = model.GenerateContentStreamAsync(prompt);
// Extract the audio data and handle it for downstream use. For example:
await foreach (var response in responseStream)
{
var audioParts = response.Candidates.FirstOrDefault().Content.Parts
.OfType<ModelContent.InlineDataPart>();
foreach (var part in audioParts)
{
byte[] pcmChunk = part.Data; // Raw PCM bytes (24kHz, 1 channel, 16-bit)
// Append the audio chunk to your audio queue/buffer for playback.
appendAudioChunk(pcmChunk);
}
}
使用提示控制语音输出
您可以使用特定的提示技巧来影响所生成语音的语气、语速和风格。
以下关于提示结构和音频标签的小节介绍了简要指南。如需详细指导,请参阅此提示指南。
提示结构
为获得最佳结果,请使用以下组件构建提示:
Audio Profile:描述说话者的角色、核心身份和原型(例如A warm, professional narrator)。Scene:描述环境和情感氛围(例如In a quiet library或Amidst a noisy crowd)。Director's Notes:描述情感、节奏、风格和口音(例如Speak slowly and with mystery)。Sample Context:为模型提供一个起点(例如The speaker is greeting a close friend)。转写文本:要朗读的实际文本。为获得最佳效果,请确保文本的书面语气和上下文与语音档案和导演备注保持一致。
提示示例:
[Audio Profile: A young, energetic voice]
[Scene: A lively sports broadcast]
[Director's Notes: Speak fast, with high energy and excitement]
[Sample Context: The game just ended with a last-second touchdown]
Welcome back fans! What an incredible game we're witnessing today!
音频标签
您可以直接在文本提示词中插入格式设置标记,以引导模型的表现。
只有在使用 Gemini 3.x TTS 模型时,系统才支持音频标记。
常用标记包括:
[whispers]:低声说话[laughs]:添加笑声[giggles]:添加笑声[sighs]:添加叹气[gasp]:添加喘气声[shouting]:大喊[excited]:兴奋地说话[serious]:严肃地说话[sighs whispers]:组合情感(您可以组合使用标签)
使用音频代码时,请注意以下事项:
没有详尽的列表:没有固定的受支持代码列表。您可以尝试不同的情绪和表达方式(例如
[bored]、[sarcastically],甚至[like dracula]),看看输出会发生怎样的变化。非英语文本提示词:如果您的文本提示词不是英语,您仍应使用英语音频标签,以获得最佳结果。
提示示例:
I have a secret to tell you. [whispers] I found the hidden treasure. [laughs] I can't believe it!
限制和要求
使用语音生成功能时,请注意以下限制和要求:
多发言人配置仅支持 2 位发言人。
只有在使用 Gemini 3.x TTS 模型时,才支持以下功能:流式传输、音频标签和其他自动检测到的语言。
gemini-3.1-flash-tts-preview 的限制条件
- 声音不一致:如果提示的语气和上下文与讲述者的特征不一致(例如,低沉的男声试图模仿年轻女孩的声音),模型的输出可能无法始终与所选讲述者严格匹配。确保提示语境与声音相符。
- 输出较长:对于时长超过几分钟的音频,语音质量和一致性可能会出现偏差。我们建议将较长的文本提示词拆分为较小的块。
- 偶尔返回文本 token:模型偶尔会返回文本 token 而不是音频 token,从而导致请求失败并出现
500错误。由于这种情况在少数请求中随机发生,因此您应在应用中实现重试逻辑。 - 分类器误拒:模糊的提示可能会导致语音合成分类器失败,从而导致请求被拒绝 (
PROHIBITED_CONTENT) 或导致模型大声朗读您的风格说明。为避免这种情况,请在提示开头使用带有明确预置指令(例如Audio Profile和Director's Notes)的结构化提示。
支持的语音和语言
Gemini TTS 模型接受文本输入并生成音频输出,因此响应是合成语音本身。以下各小节列出了 Gemini TTS 模型可以“说”(或用其回答)的受支持语音和语言。
这些语音支持多种语言,这意味着您可以使用同一种语音以任何受支持的语言生成语音。例如,您可以将语音设置为 Kore,并发送一组西班牙语、印地语和越南语的文本提示词。所有回答都将使用 Kore 语音,但会使用不同的语言。
语音名称
Gemini TTS 模型支持 30 种不同的合成高清语音,每种语音都具有不同的特征。您可以展开下面的部分,查看回答语音选项列表并试听每种语音。
语言
Gemini TTS 模型可以自动检测文本提示词中的以下语言。生成的语音将采用该语言。
请注意,您可以选择在语音配置中明确设置语言代码。
所有音频生成模型支持的语言
| 语言 | BCP-47 代码 | 语言 | BCP-47 代码 |
|---|---|---|---|
| 阿拉伯语(埃及语) | ar-EG | 德语(德国) | de-DE |
| 英语(美国) | en-US | 西班牙语(美国) | es-US |
| 法语(法国) | fr-FR | 印地语(印度) | hi-IN |
| 印度尼西亚语(印度尼西亚) | id-ID | 意大利语(意大利) | it-IT |
| 日语(日本) | ja-JP | 韩语(韩国) | ko-KR |
| 葡萄牙语(巴西) | pt-BR | 俄语(俄罗斯) | ru-RU |
| 荷兰语(荷兰) | nl-NL | 波兰语(波兰) | pl-PL |
| 泰语(泰国) | th-TH | 土耳其语(土耳其) | tr-TR |
| 越南语(越南) | vi-VN | 罗马尼亚语(罗马尼亚) | ro-RO |
| 乌克兰语(乌克兰) | uk-UA | 孟加拉语(孟加拉) | bn-BD |
| 英语(印度) | en-IN 和 hi-IN 软件包 | 马拉地语(印度) | mr-IN |
| 泰米尔语(印度) | ta-IN | 泰卢固语(印度) | te-IN |
音频生成 3.x 模型支持的其他语言
| 语言 | BCP-47 代码 | 语言 | BCP-47 代码 |
|---|---|---|---|
| 南非荷兰语 | 南非荷兰语 | 菲律宾语 | fil |
| 阿尔巴尼亚语 | 阿尔巴尼亚语 | 芬兰语 | 芬兰语 |
| 阿姆哈拉语 | 阿姆哈拉语 | 加利西亚语 | 加利西亚语 |
| 亚美尼亚语 | 亚美尼亚语 | 格鲁吉亚语 | 格鲁吉亚语 |
| 阿塞拜疆语 | 阿塞拜疆语 | 希腊语 | 希腊语 |
| 巴斯克语 | eu | 古吉拉特语 | 古吉拉特语 |
| 白俄罗斯语 | be | 海地克里奥尔语 | ht |
| 保加利亚语 | 保加利亚语 | 希伯来语 | he |
| 缅甸语 | 缅甸语 | 匈牙利语 | 匈牙利语 |
| 加泰罗尼亚语 | ca | 冰岛语 | 是 |
| 宿务语 | ceb | 爪哇语 | jv |
| 中文(普通话) | cmn | 卡纳达语 | 卡纳达语 |
| 克罗地亚语 | 小时 | 贡根语 | kok |
| 捷克语 | cs | 老挝语 | lo |
| 丹麦语 | 丹麦语 | 拉丁语 | la |
| 爱沙尼亚语 | 爱沙尼亚语 | 拉脱维亚语 | lv |
| 立陶宛语 | lt | 卢森堡语 | lb |
| 马其顿语 | 马其顿语 | 迈蒂利语 | mai |
| 马尔加什语 | mg | 马来语 | ms |
| 马拉雅拉姆语 | 马拉雅拉姆语 | 蒙古语 | mn |
| 尼泊尔语 | 尼泊尔语 | 挪威博克马尔语 | nb |
| 挪威语(尼诺斯克语) | nn | 奥里亚语 | 或 |
| 普什图语 | ps | 波斯语 | 波斯语 |
| 旁遮普语 | 旁遮普语 | 塞尔维亚语 | sr |
| 信德语 | sd | 僧伽罗文 | si |
| 斯洛伐克语 | sk | 斯洛文尼亚语 | 斯洛文尼亚语 |
| 斯瓦希里语 | sw | 瑞典语 | 瑞典语 |
| 乌尔都语 | 乌尔都语 |
(可选)明确设置语言代码
如果您未在语音配置中指定语言代码,模型会自动检测文本提示词中的语言。
不过,您可以选择明确设置语言(使用语音配置中的 languageCode 参数)。为此,您必须使用以下受支持的 BCP-47 语言区域代码之一:
- 阿拉伯语:
ar-XA - 孟加拉语:
bn-IN - 中文(普通话):
cmn-CN - 荷兰语:
nl-NL - 英语:
en-US、en-GB、en-AU、en-IN - 法语:
fr-FR、fr-CA - 德语:
de-DE - 古吉拉特语:
gu-IN - 印地语:
hi-IN - 印度尼西亚语:
id-ID - 意大利语:
it-IT - 日语:
ja-JP - 卡纳达语:
kn-IN - 韩语:
ko-KR - 马拉雅拉姆语:
ml-IN - 马拉地语:
mr-IN - 波兰语:
pl-PL - 葡萄牙语:
pt-BR - 俄语:
ru-RU - 西班牙语:
es-US、es-ES - 泰米尔语:
ta-IN - 泰卢固语:
te-IN - 泰语:
th-TH - 土耳其语:
tr-TR - 越南语:
vi-VN
你还可以做些什么?
- 了解如何在向模型发送长提示之前计算 token 数量。
-
开始考虑为正式发布做准备(请参阅正式发布核对清单):
- 尽早强制执行 Firebase App Check,以帮助保护 Gemini API 免遭未经授权的客户端滥用。
- 使用 Firebase Remote Config 或服务器提示模板,以便您按需更改 AI 功能的配置(例如模型名称),而无需发布应用的新版本。
试用其他功能
- 构建多轮对话(聊天)。
- 根据纯文本提示生成文本。
- 根据文本和多模态提示生成结构化输出(例如 JSON)。
- 根据文本和多模态提示生成和编辑图片。
- 使用 Gemini Live API 流式传输输入和输出(包括音频)。
-
使用工具(例如函数调用以及使用
Google Search 或Google Maps 进行接地)将 Gemini 模型连接到应用的其他部分以及外部系统和信息。
了解如何控制内容生成
您还可以使用 Google AI Studio 试验提示和模型配置,甚至获取生成的代码段。详细了解支持的模型
了解适用于各种应用场景的模型及其配额和价格。就您使用 Firebase AI Logic 的体验提供反馈