在服务中运行Android TTS,您可以使用以下方法:
TextToSpeech是Android系统中的一个内置类,可以将文本转换为语音。以下是一个简单的示例:
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
public class MainActivity extends AppCompatActivity {
private TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language not supported");
} else {
convertTextToSpeech("Hello, Android!");
}
} else {
Log.e("TTS", "Initialization failed");
}
}
});
}
private void convertTextToSpeech(String text) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "unique");
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
}
}
除了Android系统自带的TextToSpeech类之外,您还可以使用第三方TTS引擎,例如Google的Cloud Text-to-Speech API。以下是一个使用Google Cloud Text-to-Speech API的示例:
首先,确保您已经在Google Cloud Console中创建了一个项目,并启用了Cloud Text-to-Speech API。然后,将以下依赖项添加到您的项目的build.gradle文件中:
implementation 'com.google.cloud:google-cloud-texttospeech:1.0.1'
接下来,使用以下代码调用Google Cloud Text-to-Speech API:
import com.google.cloud.texttospeech.v1.AudioConfig;
import com.google.cloud.texttospeech.v1.AudioEncoding;
import com.google.cloud.texttospeech.v1.SsmlVoiceGender;
import com.google.cloud.texttospeech.v1.SynthesisInput;
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
try {
convertTextToSpeech("Hello, Android!");
} catch (Exception e) {
Log.e("TTS", "Error converting text to speech", e);
}
}
}).start();
}
private void convertTextToSpeech(String text) throws Exception {
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US")
.setSsmlGender(SsmlVoiceGender.NEUTRAL)
.build();
AudioConfig audioConfig = AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3)
.build();
SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
try (OutputStream output = new FileOutputStream("/sdcard/output.mp3")) {
response.getAudioContent().writeTo(output);
}
}
}
}
这个示例将文本转换为语音,并将其保存到Android设备的SD卡上。您可以根据需要进行修改和优化。
领取专属 10元无门槛券
手把手带您无忧上云