我安装了https://play.google.com/store/apps/details?id=com.ivona.tts和他们所有的声音。如何以编程方式从 ivona tts 获取所有已安装声音的列表?
android SDK 8中是否有任何命令?
我可以以编程方式更改 ivona tts 的 pico tts 吗?不适合我的程序的整个手机?
我安装了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)));
}
}
}
也许更简单的方法是使用任何活动类中的以下代码:
final int TTS_CHECK_DATA = 977; // arbitrary number
private ArrayList<String> myVoices = null;
在某些活动类方法中:
Intent in = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
// if you want specific, non-default TTS Engine also set package, else skip:
in = in.setPackage("com.acapelagroup.android.tts"); // or whatever package you want
startActivityForResult(in, TTS_CHECK_DATA);
然后在 onActivityResult() 中:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TTS_CHECK_DATA && data != null) {
myVoices = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
}
}
现在 myVoices 充满了语言环境字符串(lang-country-variant),例如对于 Acapela,我得到:
pol-POL-Ania
eng-USA-Heather
eng-GBR-Rachel
rus-RUS-Alyona
每个声音只有一个扬声器的 TTS 引擎可能不会给您第三个(变体)组件,例如 Ivona 现在只给您“eng-USA”、“eng-GBR”、“fra-FRA”等。
格雷格