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

时间:2012-09-16 19:05:06

标签: android text-to-speech

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

android SDK 8中有没有这个命令?

我可以通过编程方式更改ivona tts的pico tts吗?仅适用于我的程序的整个手机?

2 个答案:

答案 0 :(得分:6)

要获取已安装的TTS引擎列表,您应该获得 com.ivona.tts 包的内容:
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)));
        }
    }
}

答案 1 :(得分:1)

也许更简单的方法是使用任何活动类中的以下代码:

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引擎可能无法提供第3个(变体)组件,例如Ivona现在只给你“eng-USA”,“eng-GBR”,“fra-FRA”等。

格雷格