gpt4 book ai didi

android - ivona tts 以及如何以编程方式获取所有已安装语音的列表?

转载 作者:太空狗 更新时间:2023-10-29 16:06:44 26 4
gpt4 key购买 nike

我安装了 https://play.google.com/store/apps/details?id=com.ivona.tts和他们所有的声音。我如何以编程方式从 ivona tts 获取所有已安装语音的列表?

Android SDK 8 中有这方面的命令吗?

我可以通过编程方式为 ivona tts 更改 pico tts 吗?不是整个手机只是为了我的程序?

最佳答案

要获取已安装的 TTS 引擎列表,您应该获取 com.ivona.tts 包的 uid:
http://developer.android.com/reference/android/content/pm/PackageManager.html#getApplicationInfo(java.lang.String,%20int%29
然后向 PackageManager 询问共享此 uid 的所有应用程序:
http://developer.android.com/reference/android/content/pm/PackageManager.html#getPackagesForUid(int%29

要更改 TTS 引擎,请使用此方法:
http://developer.android.com/reference/android/speech/tts/TextToSpeech.html#setEngineByPackageName(java.lang.String%29

(请注意,此方法已弃用,在较新的 API 级别上,您应该使用以下构造函数:
http://developer.android.com/reference/android/speech/tts/TextToSpeech.html#TextToSpeech(android.content.Context,%20android.speech.tts.TextToSpeech.OnInitListener,%20java.lang.String%29 )

最后要设置特定的 Ivona 语音,您必须从语音包名称中解析语言/国家/名称并将其传递给:
http://developer.android.com/reference/android/speech/tts/TextToSpeech.html#setLanguage(java.util.Locale%29例如。要选择 com.ivona.tts.voicebeta.eng.gbr.amy,您应该使用:

tts.setLanguage(new Locale("eng", "gbr", "amy"));

这是将可用语音打印到 logcat 的示例 Activity:

package com.example.com.ivona.tts.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class TTSActivity extends Activity implements TextToSpeech.OnInitListener {
private static final String TAG = "TTSActivity";
private TextToSpeech tts;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tts = new TextToSpeech(TTSActivity.this, TTSActivity.this);
}

@Override
public void onInit(int status) {
PackageManager pm = getPackageManager();

ApplicationInfo ai = null;
try {
ai = pm.getApplicationInfo("com.ivona.tts", 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
return;
}

String[] ivonaVoices = pm.getPackagesForUid(ai.uid);

Log.d(TAG, "Possible tts.setLanguage() usage:");
Pattern p = Pattern.compile("^com\\.ivona\\.tts\\.voicebeta\\.([^.]+)\\.([^.]+)\\.([^.]+)$");
for (String voice: ivonaVoices) {
Matcher m = p.matcher(voice);
if (m.matches())
Log.d(TAG, String.format("tts.setLanguage(\"%s\", \"%s\", \"%s\")", m.group(1), m.group(2), m.group(3)));
}
}
}

关于android - ivona tts 以及如何以编程方式获取所有已安装语音的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12449801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com