Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检测已安装的语言以进行脱机识别_Android_Speech Recognition - Fatal编程技术网

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

Android 检测已安装的语言以进行脱机识别,android,speech-recognition,Android,Speech Recognition,可以通过代码确定设备上当前安装了哪些语言包?我试过这个: Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); sendOrderedBroadcast(detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null); public类语言detailsch

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

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

public类语言detailschecker扩展了BroadcastReceiver{
私有列表支持的语言;
@凌驾
公共void onReceive(上下文、意图)
{
Bundle results=getResultTextras(true);
if(results.containsKey(RecognizerIntent.EXTRA_支持的语言))
{
supportedLanguages=results.getStringArrayList(RecognizerIntent.EXTRA\u支持的\u语言);
Log.d(“标记”,“语言:”+支持的语言);
}
}
}
然而,输出显示了大量的语言,而我只安装了en_-UK、en_-US和es_-es。有什么想法吗?

如果你有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;
}
这是从安卓4.2.2中摘取并修改的:

  • 返回一个简单的布尔值,而不是字典目录
  • 接受字符串输入(例如“en_US”)而不是区域设置
我会像你一样得到完整的列表,并循环检查哪些列表是离线可用的。我检查了两台设备上的
/system/usr/srec/config/
文件夹,它们都与我安装的词典相匹配

当然,不利的一面是它只对root有效,所以我不确定这最终会对您有多大帮助。我真的不知道该说什么,我找不到任何东西


编辑:不过出于好奇,如果您离线,受支持的语言包含哪些额外内容?如果返回正确,您可能只需伪造网络管理器。

检查此示例:

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.islanguage可用(Locale.US)

您只需尝试以下操作:

 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());
                }
            }

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

包含:语言:[af ZA、id id、ms MY、ca ES、cs CZ、de、en AU、en ca、en-001、en IN、en NZ、en ZA、en GB、en US、ES AR、ES BO、ES CL、ES CO、ES CR、ES EC、ES US、ES SV、ES ES ES GT、ES HN、ES MX、ES NI、ES PA、ES PY、ES PE、ES PR、ES DO、ES UY、ES VE、eu ES、fil PH、fr fr、gl ES、hr、zu ZA、is、it、hu、nl nl nl nl nl、nb、pl、pl、pt、pt、sk-SK、fi fi、sv SE、vi VN、tr tr、bg bg、ru ru、sr RS、uk UA、he IL、ar IL、ar JO、ar AE、ar BH、ar SA、ar KW、ar OM、ar PS、ar QA、ar LB、ar EG、ko KR、cmn Hans CN、cmn Hans HK、cmn Hant TW、yue Hant HK、ja JP、la]这是所有可用的语言。包含es、en US和en GB,这是我目前安装的语言。我看到了,所以假装“没有网络”这种情况是行不通的。那么,除了根解决方案之外,我不知道该告诉你什么。也许其他人会发现一些东西。试试这个-可能对你有用,好运气你测试的语音识别服务是什么?谷歌语音识别支持几十种语言。你到底是什么意思,你只有英语、英国/美国和Spanish已安装?您可以脱机安装语言包,因此不需要连接到Google。我需要知道安装了哪些语言。我想您无法知道哪些语言包可供脱机使用,因为这是Google语音识别引擎特有的。您的答案是关于文本到语音,而不是(脱机)从演讲到文本。
 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());
                }
            }