检测已安装的语言以进行脱机识别

时间:2013-07-23 07:04:30

标签: android speech-recognition

可以通过代码确定设备上当前安装了哪些语言包?试过这个:

    Intent detailsIntent =  new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
    sendOrderedBroadcast(detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);

    public class LanguageDetailsChecker extends BroadcastReceiver  {

    private List<String> supportedLanguages;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle results = getResultExtras(true);
        if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
        {
            supportedLanguages =results.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
            Log.d("TAG","languages: " + supportedLanguages);
        }
    }
}

但是,输出显示了大量语言,而我只安装了en_UK,en_US和es_ES。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

如果你有root(对不起),你可以这样做:

public static boolean isOfflineDictionaryPresent(String language) {
    if (locale == null) locale = Locale.US;
    String dir = "/system/usr/srec/config/" +
            language.replace('_', '.').toLowerCase();
    if ((new File(dir)).isDirectory()) return true;
    return false;
}

这是从Android 4.2.2 Recognizer.java source中删除并修改后的:

  • 返回一个简单的布尔值而不是字典目录
  • 接受String输入(例如“en_US”)而不是Locale

我会像你一样得到完整的列表,然后循环查看哪些列表可以脱机使用。我检查了两个设备上的/system/usr/srec/config/文件夹,它们都与我安装的字典相匹配。

当然,缺点是它只适用于root,所以我不确定这对你最终会有多大帮助。我真的不确定对于非root用户说什么,我找不到任何东西。


编辑:出于好奇,如果 离线,EXTRA_SUPPORTED_LANGUAGES包含什么?如果它正确返回,您可能只需要伪造网络管理员。

答案 1 :(得分:0)

检查此样本:

public class SpeakingAndroid extends Activity implements  OnInitListener {

        //TTS object
    private TextToSpeech myTTS;
        //status check code
    private int TS_CHECK_CODE = 0;

        //create the Activity
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

            //check for TTS data
            Intent checkTTSIntent = new Intent();
            checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
            startActivityForResult(checkTTSIntent, TS_CHECK_CODE);
    }

        //act on result of TTS data check
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                //the user has the necessary data - create the TTS
            myTTS = new TextToSpeech(this, this);
            }
            else {
                    //no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }

        //setup TTS
    public void onInit(int initStatus) {

            //check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        }
        else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
        }
    }
}

你可以获得所有语言并检查tts是否支持每个语言? myTTS.isLanguageAvailable(Locale.US)

答案 2 :(得分:0)

您应该简单地尝试一下:

 for (Locale locale : Locale.getAvailableLocales()) {
                try {
                    if (tts != null) {
                        if (locale != null && locale.getISO3Country() != null && locale.getISO3Language() != null && tts.isLanguageAvailable(locale) == 1) {
                            available_locs.add(locale);
                            tts.setLanguage((Locale) locale);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                if (tts.getVoice().getFeatures().contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)) {
                                    Log.i(TAG, "[onInit]contains:IF " + locale.getDisplayName());
                                } else {
                                    Log.i(TAG, "***********[onInit]contains:ELSE " + locale.getDisplayName());
                                }

                            } else {
                                    /*not supported  21 below sdk so hide progressbar and hide spinner and only TTs in default
                                    language */
                                Log.i(TAG,"Not supported");
                            }

                        }
                    }
                } catch (Exception e) {
                    Log.i(TAG, "[onViewCreated] " + e.getLocalizedMessage());
                }
            }

在其他情况下,您应该知道设备中的安装语言。