Android中的离线语音识别(JellyBean)

时间:2021-02-15 18:23:57

It looks as though Google has made offline speech recognition available from Google Now for third-party apps. It is being used by the app named Utter.

看起来谷歌已经从谷歌即时提供了第三方应用程序的离线语音识别功能。它被名为Utter的应用程序使用。

Has anyone seen any implementations of how to do simple voice commands with this offline speech rec? Do you just use the regular SpeechRecognizer API and it works automatically?

有没有人看过如何使用这个离线语音rec进行简单的语音命令的任何实现?您是否只使用常规的SpeechRecognizer API并自动运行?

8 个解决方案

#1


67  

Google did quietly enable offline recognition in that Search update, but there is (as yet) no API or additional parameters available within the SpeechRecognizer class. {See Edit at the bottom of this post} The functionality is available with no additional coding, however the user’s device will need to be configured correctly for it to begin working and this is where the problem lies and I would imagine why a lot of developers assume they are ‘missing something’.

Google确实在该搜索更新中悄然启用了离线识别功能,但在SpeechRecognizer类中(目前)还没有可用的API或其他参数。 {请参阅本文底部的编辑}该功能可用,无需额外编码,但是用户的设备需要正确配置才能开始工作,这就是问题所在,我想象为什么很多开发人员假设他们“遗失了某些东西”。

Also, Google have restricted certain Jelly Bean devices from using the offline recognition due to hardware constraints. Which devices this applies to is not documented, in fact, nothing is documented, so configuring the capabilities for the user has proved to be a matter of trial and error (for them). It works for some straight away – For those that it doesn't, this is the ‘guide’ I supply them with.

此外,由于硬件限制,Google限制某些Jelly Bean设备使用离线识别。哪些适用的设备没有记录,事实上,没有任何文件记录,因此为用户配置功能已经证明是一个试验和错误的问题(对他们而言)。它适用于一些直接 - 对于那些它没有,这是我提供给他们的'指南'。

  1. Make sure the default Android Voice Recogniser is set to Google not Samsung/Vlingo
  2. 确保默认的Android Voice Recogniser设置为Google而不是Samsung / Vlingo
  3. Uninstall any offline recognition files you already have installed from the Google Voice Search Settings
  4. 从Google语音搜索设置中卸载已安装的所有脱机识别文件
  5. Go to your Android Application Settings and see if you can uninstall the updates for the Google Search and Google Voice Search applications.
  6. 转到您的Android应用设置,看看您是否可以卸载Google搜索和Google语音搜索应用的更新。
  7. If you can't do the above, go to the Play Store see if you have the option there.
  8. 如果您无法执行上述操作,请转到Play商店,看看您是否有选项。
  9. Reboot (if you achieved 2, 3 or 4)
  10. 重启(如果你达到2,3或4)
  11. Update Google Search and Google Voice Search from the Play Store (if you achieved 3 or 4 or if an update is available anyway).
  12. 从Play商店更新Google搜索和Google语音搜索(如果您已达到3或4,或者无论如何都有更新)。
  13. Reboot (if you achieved 6)
  14. 重启(如果你达到了6)
  15. Install English UK offline language files
  16. 安装英国英语离线语言文件
  17. Reboot
  18. 重启
  19. Use utter! with a connection
  20. 用完了!有连接
  21. Switch to aeroplane mode and give it a try
  22. 切换到飞行模式并试一试
  23. Once it is working, the offline recognition of other languages, such as English US should start working too.
  24. 一旦它工作,其他语言的离线识别,如英语美国也应该开始工作。

EDIT: Temporarily changing the device locale to English UK also seems to kickstart this to work for some.

编辑:暂时将设备区域设置更改为英国英国似乎也启动了这项工作。

Some users reported they still had to reboot a number of times before it would begin working, but they all get there eventually, often inexplicably to what was the trigger, the key to which are inside the Google Search APK, so not in the public domain or part of AOSP.

一些用户报告说他们在开始工作之前仍然需要多次重启,但他们最终都会到达那里,通常莫名其妙地触发了什么是触发器,其关键在于Google搜索APK,所以不在公共领域或AOSP的一部分。

From what I can establish, Google tests the availability of a connection prior to deciding whether to use offline or online recognition. If a connection is available initially but is lost prior to the response, Google will supply a connection error, it won’t fall-back to offline. As a side note, if a request for the network synthesised voice has been made, there is no error supplied it if fails – You get silence.

根据我的建议,Google在决定是否使用离线或在线识别之前测试连接的可用性。如果连接最初可用但在响应之前丢失,则Google将提供连接错误,它不会退回到脱机状态。作为旁注,如果已经发出了对网络合成语音的请求,则如果失败则没有提供错误 - 您将获得静音。

The Google Search update enabled no additional features in Google Now and in fact if you try to use it with no internet connection, it will error. I mention this as I wondered if the ability would be withdrawn as quietly as it appeared and therefore shouldn't be relied upon in production.

Google搜索更新在Google即时中未启用任何其他功能,实际上如果您尝试在没有互联网连接的情况下使用它,则会出错。我提到这一点,因为我想知道这种能力是否会像它出现的那样安静地被撤回,因此不应该依赖于生产。

If you intend to start using the SpeechRecognizer class, be warned, there is a pretty major bug associated with it, which require your own implementation to handle.

如果您打算开始使用SpeechRecognizer类,请注意,有一个相关的主要错误,需要您自己的实现来处理。

Not being able to specifically request offline = true, makes controlling this feature impossible without manipulating the data connection. Rubbish. You’ll get hundreds of user emails asking you why you haven’t enabled something so simple!

无法专门请求offline = true,无需操作数据连接就无法控制此功能。垃圾。您将收到数百封用户电子邮件,询问您为什么没有启用这么简单的东西!

EDIT: Since API level 23 a new parameter has been added EXTRA_PREFER_OFFLINE which the Google recognition service does appear to adhere to.

编辑:自API级别23以来,添加了一个新参数EXTRA_PREFER_OFFLINE,Google认可服务似乎确实遵守了该参数。

Hope the above helps.

希望以上有所帮助。

#2


20  

I would like to improve the guide that the answer https://*.com/a/17674655/2987828 sends to its users, with images. It is the sentence "For those that it doesn't, this is the ‘guide’ I supply them with." that I want to improve.

我想改进答案https://*.com/a/17674655/2987828向用户发送带有图片的指南。这句话是“对于那些没有的人,这是我向他们提供的'指南'。”我想改进。

The user should click on the four buttons highlighted in blue in these images:

用户应在这些图像中单击以蓝色突出显示的四个按钮:

Android中的离线语音识别(JellyBean)Android中的离线语音识别(JellyBean)Android中的离线语音识别(JellyBean)Android中的离线语音识别(JellyBean)

Then the user can select any desired languages. When the download is done, he should disconnect from network, and then click on the "microphone" button of the keyboard.

然后,用户可以选择任何所需的语言。下载完成后,他应断开与网络的连接,然后单击键盘上的“麦克风”按钮。

It worked for me (android 4.1.2), then language recognition worked out of the box, without rebooting. I can now dictates instructions to the shell of Terminal Emulator ! And it is twice faster offline than online, on a padfone 2 from ASUS.

它适用于我(android 4.1.2),然后语言识别开箱即用,无需重启。我现在可以指示终端仿真器的shell指令!在华硕的padfone 2上,离线速度比在线快两倍。

These images are licensed under cc by-sa 3.0 with attribution required to *.com/a/21329845/2987828 ; you may hence add these images anywhere along with this attribution.

这些图像是根据cc by-sa 3.0获得许可的,其归属地址为*.com/a/21329845/2987828;因此,您可以将这些图像添加到此属性的任何位置。

(This the standard policy of all images and texts at *.com)

(这是*.com上所有图像和文本的标准政策)

#3


17  

A simple and flexible offline recognition on Android is implemented by CMUSphinx, an open source speech recognition toolkit. It works purely offline, fast and configurable It can listen continuously for keyword, for example.

Android上的简单灵活的离线识别是由开源语音识别工具包CMUSphinx实现的。它完全脱机,快速和可配置工作它可以持续监听关键字,例如。

You can find latest code and tutorial here.

您可以在此处找到最新的代码和教程。

#4


8  

In short, I don't have the implementation, but the explanation.

总之,我没有实施,而是解释。

Google did not make offline speech recognition available to third party apps. Offline recognition is only accessable via the keyboard. Ben Randall (the developer of utter!) explains his workaround in an article at Android Police:

谷歌没有为第三方应用程序提供离线语音识别功能。离线识别只能通过键盘访问。 Ben Randall(utter的开发者!)在Android Police的一篇文章中解释了他的解决方法:

I had implemented my own keyboard and was switching between Google Voice Typing and the users default keyboard with an invisible edit text field and transparent Activity to get the input. Dirty hack!

我已经实现了自己的键盘,并在Google Voice Typing和用户默认键盘之间切换,其中包含一个不可见的编辑文本字段和透明的Activity来获取输入。肮脏的黑客!

This was the only way to do it, as offline Voice Typing could only be triggered by an IME or a system application (that was my root hack) . The other type of recognition API … didn't trigger it and just failed with a server error. … A lot of work wasted for me on the workaround! But at least I was ready for the implementation...

这是唯一的方法,因为离线语音输入只能由IME或系统应用程序触发(这是我的root hack)。另一种类型的识别API ...没有触发它,只是因服务器错误而失败。 ...在解决方法上浪费了很多工作!但至少我已准备好实施......

From Utter! Claims To Be The First Non-IME App To Utilize Offline Voice Recognition In Jelly Bean

来自Utter!声称是第一个在果冻豆中使用离线语音识别的非IME应用程序

#5


3  

I successfully implemented my Speech-Service with offline capabilities by using onPartialResults when offline and onResults when online.

我通过离线时使用onPartialResults和在线时使用onResults成功实现了具有离线功能的语音服务。

#6


2  

I was dealing with this and I noticed that you need to install the offline package for your Language. My language setting was "Español (Estados Unidos)" but there is not offline package for that language, so when I turned off all network connectivity I was getting an alert from RecognizerIntent saying that can't reach Google, then I change the language to "English (US)" (because I already have the offline package) and launched the RecognizerIntent it just worked out.

我正在处理这个问题,我注意到你需要为你的语言安装离线包。我的语言设置是“Español(Estados Unidos)”,但是没有该语言的离线包,所以当我关闭所有网络连接时,我收到来自RecognizerIntent的警告说无法访问Google,然后我将语言更改为“英语(美国)”(因为我已经有了离线软件包)并启动了它刚刚解决的RecognizerIntent。

Keys: Language setting == Offline Voice Recognizer Package

键:语言设置==离线语音识别器包

#7


1  

It is apparently possible to manually install offline voice recognition by downloading the files directly and installing them in the right locations manually. I guess this is just a way to bypass Google hardware requirements. However, personally I didn't have to reboot or anything, simply changing to UK and back again did it.

通过直接下载文件并手动将它们安装在正确的位置,显然可以手动安装离线语音识别。我想这只是绕过谷歌硬件要求的一种方式。但是,我个人没有重新启动或任何东西,只需转换到英国,然后再回来做。

#8


0  

Working example is given below,

工作实例如下,

MyService.class

MyService.class

public class MyService extends Service implements SpeechDelegate, Speech.stopDueToDelay {

  public static SpeechDelegate delegate;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    try {
      if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
        ((AudioManager) Objects.requireNonNull(
          getSystemService(Context.AUDIO_SERVICE))).setStreamMute(AudioManager.STREAM_SYSTEM, true);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    Speech.init(this);
    delegate = this;
    Speech.getInstance().setListener(this);

    if (Speech.getInstance().isListening()) {
      Speech.getInstance().stopListening();
    } else {
      System.setProperty("rx.unsafe-disable", "True");
      RxPermissions.getInstance(this).request(permission.RECORD_AUDIO).subscribe(granted -> {
        if (granted) { // Always true pre-M
          try {
            Speech.getInstance().stopTextToSpeech();
            Speech.getInstance().startListening(null, this);
          } catch (SpeechRecognitionNotAvailable exc) {
            //showSpeechNotSupportedDialog();

          } catch (GoogleVoiceTypingDisabledException exc) {
            //showEnableGoogleVoiceTyping();
          }
        } else {
          Toast.makeText(this, R.string.permission_required, Toast.LENGTH_LONG).show();
        }
      });
    }
    return Service.START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
    //TODO for communication return IBinder implementation
    return null;
  }

  @Override
  public void onStartOfSpeech() {
  }

  @Override
  public void onSpeechRmsChanged(float value) {

  }

  @Override
  public void onSpeechPartialResults(List<String> results) {
    for (String partial : results) {
      Log.d("Result", partial+"");
    }
  }

  @Override
  public void onSpeechResult(String result) {
    Log.d("Result", result+"");
    if (!TextUtils.isEmpty(result)) {
      Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onSpecifiedCommandPronounced(String event) {
    try {
      if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
        ((AudioManager) Objects.requireNonNull(
          getSystemService(Context.AUDIO_SERVICE))).setStreamMute(AudioManager.STREAM_SYSTEM, true);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (Speech.getInstance().isListening()) {
      Speech.getInstance().stopListening();
    } else {
      RxPermissions.getInstance(this).request(permission.RECORD_AUDIO).subscribe(granted -> {
        if (granted) { // Always true pre-M
          try {
            Speech.getInstance().stopTextToSpeech();
            Speech.getInstance().startListening(null, this);
          } catch (SpeechRecognitionNotAvailable exc) {
            //showSpeechNotSupportedDialog();

          } catch (GoogleVoiceTypingDisabledException exc) {
            //showEnableGoogleVoiceTyping();
          }
        } else {
          Toast.makeText(this, R.string.permission_required, Toast.LENGTH_LONG).show();
        }
      });
    }
  }


  @Override
  public void onTaskRemoved(Intent rootIntent) {
    //Restarting the service if it is removed.
    PendingIntent service =
      PendingIntent.getService(getApplicationContext(), new Random().nextInt(),
        new Intent(getApplicationContext(), MyService.class), PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    assert alarmManager != null;
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
    super.onTaskRemoved(rootIntent);
  }
}

For more details,

更多细节,

https://github.com/sachinvarma/Speech-Recognizer

https://github.com/sachinvarma/Speech-Recognizer

Hope this will help someone in future.

希望这将有助于将来的某些人。

#1


67  

Google did quietly enable offline recognition in that Search update, but there is (as yet) no API or additional parameters available within the SpeechRecognizer class. {See Edit at the bottom of this post} The functionality is available with no additional coding, however the user’s device will need to be configured correctly for it to begin working and this is where the problem lies and I would imagine why a lot of developers assume they are ‘missing something’.

Google确实在该搜索更新中悄然启用了离线识别功能,但在SpeechRecognizer类中(目前)还没有可用的API或其他参数。 {请参阅本文底部的编辑}该功能可用,无需额外编码,但是用户的设备需要正确配置才能开始工作,这就是问题所在,我想象为什么很多开发人员假设他们“遗失了某些东西”。

Also, Google have restricted certain Jelly Bean devices from using the offline recognition due to hardware constraints. Which devices this applies to is not documented, in fact, nothing is documented, so configuring the capabilities for the user has proved to be a matter of trial and error (for them). It works for some straight away – For those that it doesn't, this is the ‘guide’ I supply them with.

此外,由于硬件限制,Google限制某些Jelly Bean设备使用离线识别。哪些适用的设备没有记录,事实上,没有任何文件记录,因此为用户配置功能已经证明是一个试验和错误的问题(对他们而言)。它适用于一些直接 - 对于那些它没有,这是我提供给他们的'指南'。

  1. Make sure the default Android Voice Recogniser is set to Google not Samsung/Vlingo
  2. 确保默认的Android Voice Recogniser设置为Google而不是Samsung / Vlingo
  3. Uninstall any offline recognition files you already have installed from the Google Voice Search Settings
  4. 从Google语音搜索设置中卸载已安装的所有脱机识别文件
  5. Go to your Android Application Settings and see if you can uninstall the updates for the Google Search and Google Voice Search applications.
  6. 转到您的Android应用设置,看看您是否可以卸载Google搜索和Google语音搜索应用的更新。
  7. If you can't do the above, go to the Play Store see if you have the option there.
  8. 如果您无法执行上述操作,请转到Play商店,看看您是否有选项。
  9. Reboot (if you achieved 2, 3 or 4)
  10. 重启(如果你达到2,3或4)
  11. Update Google Search and Google Voice Search from the Play Store (if you achieved 3 or 4 or if an update is available anyway).
  12. 从Play商店更新Google搜索和Google语音搜索(如果您已达到3或4,或者无论如何都有更新)。
  13. Reboot (if you achieved 6)
  14. 重启(如果你达到了6)
  15. Install English UK offline language files
  16. 安装英国英语离线语言文件
  17. Reboot
  18. 重启
  19. Use utter! with a connection
  20. 用完了!有连接
  21. Switch to aeroplane mode and give it a try
  22. 切换到飞行模式并试一试
  23. Once it is working, the offline recognition of other languages, such as English US should start working too.
  24. 一旦它工作,其他语言的离线识别,如英语美国也应该开始工作。

EDIT: Temporarily changing the device locale to English UK also seems to kickstart this to work for some.

编辑:暂时将设备区域设置更改为英国英国似乎也启动了这项工作。

Some users reported they still had to reboot a number of times before it would begin working, but they all get there eventually, often inexplicably to what was the trigger, the key to which are inside the Google Search APK, so not in the public domain or part of AOSP.

一些用户报告说他们在开始工作之前仍然需要多次重启,但他们最终都会到达那里,通常莫名其妙地触发了什么是触发器,其关键在于Google搜索APK,所以不在公共领域或AOSP的一部分。

From what I can establish, Google tests the availability of a connection prior to deciding whether to use offline or online recognition. If a connection is available initially but is lost prior to the response, Google will supply a connection error, it won’t fall-back to offline. As a side note, if a request for the network synthesised voice has been made, there is no error supplied it if fails – You get silence.

根据我的建议,Google在决定是否使用离线或在线识别之前测试连接的可用性。如果连接最初可用但在响应之前丢失,则Google将提供连接错误,它不会退回到脱机状态。作为旁注,如果已经发出了对网络合成语音的请求,则如果失败则没有提供错误 - 您将获得静音。

The Google Search update enabled no additional features in Google Now and in fact if you try to use it with no internet connection, it will error. I mention this as I wondered if the ability would be withdrawn as quietly as it appeared and therefore shouldn't be relied upon in production.

Google搜索更新在Google即时中未启用任何其他功能,实际上如果您尝试在没有互联网连接的情况下使用它,则会出错。我提到这一点,因为我想知道这种能力是否会像它出现的那样安静地被撤回,因此不应该依赖于生产。

If you intend to start using the SpeechRecognizer class, be warned, there is a pretty major bug associated with it, which require your own implementation to handle.

如果您打算开始使用SpeechRecognizer类,请注意,有一个相关的主要错误,需要您自己的实现来处理。

Not being able to specifically request offline = true, makes controlling this feature impossible without manipulating the data connection. Rubbish. You’ll get hundreds of user emails asking you why you haven’t enabled something so simple!

无法专门请求offline = true,无需操作数据连接就无法控制此功能。垃圾。您将收到数百封用户电子邮件,询问您为什么没有启用这么简单的东西!

EDIT: Since API level 23 a new parameter has been added EXTRA_PREFER_OFFLINE which the Google recognition service does appear to adhere to.

编辑:自API级别23以来,添加了一个新参数EXTRA_PREFER_OFFLINE,Google认可服务似乎确实遵守了该参数。

Hope the above helps.

希望以上有所帮助。

#2


20  

I would like to improve the guide that the answer https://*.com/a/17674655/2987828 sends to its users, with images. It is the sentence "For those that it doesn't, this is the ‘guide’ I supply them with." that I want to improve.

我想改进答案https://*.com/a/17674655/2987828向用户发送带有图片的指南。这句话是“对于那些没有的人,这是我向他们提供的'指南'。”我想改进。

The user should click on the four buttons highlighted in blue in these images:

用户应在这些图像中单击以蓝色突出显示的四个按钮:

Android中的离线语音识别(JellyBean)Android中的离线语音识别(JellyBean)Android中的离线语音识别(JellyBean)Android中的离线语音识别(JellyBean)

Then the user can select any desired languages. When the download is done, he should disconnect from network, and then click on the "microphone" button of the keyboard.

然后,用户可以选择任何所需的语言。下载完成后,他应断开与网络的连接,然后单击键盘上的“麦克风”按钮。

It worked for me (android 4.1.2), then language recognition worked out of the box, without rebooting. I can now dictates instructions to the shell of Terminal Emulator ! And it is twice faster offline than online, on a padfone 2 from ASUS.

它适用于我(android 4.1.2),然后语言识别开箱即用,无需重启。我现在可以指示终端仿真器的shell指令!在华硕的padfone 2上,离线速度比在线快两倍。

These images are licensed under cc by-sa 3.0 with attribution required to *.com/a/21329845/2987828 ; you may hence add these images anywhere along with this attribution.

这些图像是根据cc by-sa 3.0获得许可的,其归属地址为*.com/a/21329845/2987828;因此,您可以将这些图像添加到此属性的任何位置。

(This the standard policy of all images and texts at *.com)

(这是*.com上所有图像和文本的标准政策)

#3


17  

A simple and flexible offline recognition on Android is implemented by CMUSphinx, an open source speech recognition toolkit. It works purely offline, fast and configurable It can listen continuously for keyword, for example.

Android上的简单灵活的离线识别是由开源语音识别工具包CMUSphinx实现的。它完全脱机,快速和可配置工作它可以持续监听关键字,例如。

You can find latest code and tutorial here.

您可以在此处找到最新的代码和教程。

#4


8  

In short, I don't have the implementation, but the explanation.

总之,我没有实施,而是解释。

Google did not make offline speech recognition available to third party apps. Offline recognition is only accessable via the keyboard. Ben Randall (the developer of utter!) explains his workaround in an article at Android Police:

谷歌没有为第三方应用程序提供离线语音识别功能。离线识别只能通过键盘访问。 Ben Randall(utter的开发者!)在Android Police的一篇文章中解释了他的解决方法:

I had implemented my own keyboard and was switching between Google Voice Typing and the users default keyboard with an invisible edit text field and transparent Activity to get the input. Dirty hack!

我已经实现了自己的键盘,并在Google Voice Typing和用户默认键盘之间切换,其中包含一个不可见的编辑文本字段和透明的Activity来获取输入。肮脏的黑客!

This was the only way to do it, as offline Voice Typing could only be triggered by an IME or a system application (that was my root hack) . The other type of recognition API … didn't trigger it and just failed with a server error. … A lot of work wasted for me on the workaround! But at least I was ready for the implementation...

这是唯一的方法,因为离线语音输入只能由IME或系统应用程序触发(这是我的root hack)。另一种类型的识别API ...没有触发它,只是因服务器错误而失败。 ...在解决方法上浪费了很多工作!但至少我已准备好实施......

From Utter! Claims To Be The First Non-IME App To Utilize Offline Voice Recognition In Jelly Bean

来自Utter!声称是第一个在果冻豆中使用离线语音识别的非IME应用程序

#5


3  

I successfully implemented my Speech-Service with offline capabilities by using onPartialResults when offline and onResults when online.

我通过离线时使用onPartialResults和在线时使用onResults成功实现了具有离线功能的语音服务。

#6


2  

I was dealing with this and I noticed that you need to install the offline package for your Language. My language setting was "Español (Estados Unidos)" but there is not offline package for that language, so when I turned off all network connectivity I was getting an alert from RecognizerIntent saying that can't reach Google, then I change the language to "English (US)" (because I already have the offline package) and launched the RecognizerIntent it just worked out.

我正在处理这个问题,我注意到你需要为你的语言安装离线包。我的语言设置是“Español(Estados Unidos)”,但是没有该语言的离线包,所以当我关闭所有网络连接时,我收到来自RecognizerIntent的警告说无法访问Google,然后我将语言更改为“英语(美国)”(因为我已经有了离线软件包)并启动了它刚刚解决的RecognizerIntent。

Keys: Language setting == Offline Voice Recognizer Package

键:语言设置==离线语音识别器包

#7


1  

It is apparently possible to manually install offline voice recognition by downloading the files directly and installing them in the right locations manually. I guess this is just a way to bypass Google hardware requirements. However, personally I didn't have to reboot or anything, simply changing to UK and back again did it.

通过直接下载文件并手动将它们安装在正确的位置,显然可以手动安装离线语音识别。我想这只是绕过谷歌硬件要求的一种方式。但是,我个人没有重新启动或任何东西,只需转换到英国,然后再回来做。

#8


0  

Working example is given below,

工作实例如下,

MyService.class

MyService.class

public class MyService extends Service implements SpeechDelegate, Speech.stopDueToDelay {

  public static SpeechDelegate delegate;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    try {
      if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
        ((AudioManager) Objects.requireNonNull(
          getSystemService(Context.AUDIO_SERVICE))).setStreamMute(AudioManager.STREAM_SYSTEM, true);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    Speech.init(this);
    delegate = this;
    Speech.getInstance().setListener(this);

    if (Speech.getInstance().isListening()) {
      Speech.getInstance().stopListening();
    } else {
      System.setProperty("rx.unsafe-disable", "True");
      RxPermissions.getInstance(this).request(permission.RECORD_AUDIO).subscribe(granted -> {
        if (granted) { // Always true pre-M
          try {
            Speech.getInstance().stopTextToSpeech();
            Speech.getInstance().startListening(null, this);
          } catch (SpeechRecognitionNotAvailable exc) {
            //showSpeechNotSupportedDialog();

          } catch (GoogleVoiceTypingDisabledException exc) {
            //showEnableGoogleVoiceTyping();
          }
        } else {
          Toast.makeText(this, R.string.permission_required, Toast.LENGTH_LONG).show();
        }
      });
    }
    return Service.START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
    //TODO for communication return IBinder implementation
    return null;
  }

  @Override
  public void onStartOfSpeech() {
  }

  @Override
  public void onSpeechRmsChanged(float value) {

  }

  @Override
  public void onSpeechPartialResults(List<String> results) {
    for (String partial : results) {
      Log.d("Result", partial+"");
    }
  }

  @Override
  public void onSpeechResult(String result) {
    Log.d("Result", result+"");
    if (!TextUtils.isEmpty(result)) {
      Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onSpecifiedCommandPronounced(String event) {
    try {
      if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
        ((AudioManager) Objects.requireNonNull(
          getSystemService(Context.AUDIO_SERVICE))).setStreamMute(AudioManager.STREAM_SYSTEM, true);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (Speech.getInstance().isListening()) {
      Speech.getInstance().stopListening();
    } else {
      RxPermissions.getInstance(this).request(permission.RECORD_AUDIO).subscribe(granted -> {
        if (granted) { // Always true pre-M
          try {
            Speech.getInstance().stopTextToSpeech();
            Speech.getInstance().startListening(null, this);
          } catch (SpeechRecognitionNotAvailable exc) {
            //showSpeechNotSupportedDialog();

          } catch (GoogleVoiceTypingDisabledException exc) {
            //showEnableGoogleVoiceTyping();
          }
        } else {
          Toast.makeText(this, R.string.permission_required, Toast.LENGTH_LONG).show();
        }
      });
    }
  }


  @Override
  public void onTaskRemoved(Intent rootIntent) {
    //Restarting the service if it is removed.
    PendingIntent service =
      PendingIntent.getService(getApplicationContext(), new Random().nextInt(),
        new Intent(getApplicationContext(), MyService.class), PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    assert alarmManager != null;
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
    super.onTaskRemoved(rootIntent);
  }
}

For more details,

更多细节,

https://github.com/sachinvarma/Speech-Recognizer

https://github.com/sachinvarma/Speech-Recognizer

Hope this will help someone in future.

希望这将有助于将来的某些人。