You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 13下Google语音服务查询支持语言失效,求可行方案

Android 13 下获取语音服务支持语言的替代方法

问题概述

原通过RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS查询语音服务支持语言的代码在 Android 12 及以下正常运行,但在 Android 13(API 33)上返回空列表——原因是 Google App 在 API 33 环境下将speech_services_enabled设为false,禁用了对应广播接收器。

原实现代码:

val intent = Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS)
packageManager.queryBroadcastReceivers(intent, 0).map { resolveInfo ->
    sendOrderedBroadcast(
        intent.apply {
            component = ComponentName(resolveInfo.activityInfo.applicationInfo.packageName, resolveInfo.activityInfo.name)
        },
        null,
        object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                val extras = getResultExtras(true)
                val supportedLanguages = extras.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)
                // Android 13上所有服务的supportedLanguages均为空
            }
        },
        null, Activity.RESULT_OK, null, null
    )
}

另外尝试SpeechRecognizer.checkRecognitionSupport方法,始终返回错误 14(ERROR_CANNOT_CHECK_SUPPORT):

val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context)
speechRecognizer.checkRecognitionSupport(
    Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
    ...
)

替代方案

1. 调用 Google Cloud Speech-to-Text API(需网络)

直接请求官方接口获取完整支持语言列表,需提前申请 API 密钥:

val client = OkHttpClient()
val request = Request.Builder()
    .url("https://www.google.com/speech-api/v2/langcodes?key=你的API密钥")
    .build()

client.newCall(request).enqueue(object : Callback {
    override fun onFailure(call: Call, e: IOException) {
        // 处理请求失败逻辑
    }

    override fun onResponse(call: Call, response: Response) {
        response.body?.string()?.let { json ->
            // 解析JSON数据,示例结构:[{"language": "zh-CN", "name": "Chinese (China)"}]
            val gson = Gson()
            val langList = gson.fromJson(json, Array<Language>::class.java).toList()
            // 处理语言列表
        }
    }
})

// 数据类用于解析
data class Language(val language: String, val name: String)

2. 读取系统已配置的语音识别语言

通过系统设置获取用户已安装/配置的语音语言,虽不是完整支持列表,但能覆盖常用场景:

val languageStr = Settings.Secure.getString(
    context.contentResolver,
    Settings.Secure.VOICE_RECOGNITION_LANGUAGES
)
val supportedLangs = languageStr?.split(",") ?: emptyList()

3. 通过 TextToSpeech 间接获取(近似列表)

语音识别与 TTS 的支持语言高度重叠,可通过TextToSpeech获取可用语言:

val tts = TextToSpeech(context) { status ->
    if (status == TextToSpeech.SUCCESS) {
        val availableLocales = tts.availableLanguages
        val langCodes = availableLocales.map { it.toLanguageTag() }
        // 使用langCodes列表
    }
}

内容的提问来源于stack exchange,提问作者dipdipdip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 07:50:28