前言
实不相瞒,小编作为了一个湖南人,时常被说普通话不够标准,不仅N/L不分,有时候还分不出来卷舌音,经常闹笑话,导致小编十分苦恼。有时看着电视里的主持人流利的口播,和完全标准的绕口令都羡慕不已,常常幻想着自己有一天也能说一口流利的绕口令。恰巧,小编昨日上网的时候被推送了集成了华为HMS ML Kit实时语音服务的小游戏-Tongue Twister,这款游戏究竟是如何玩转绕口令的,接下来就和小编一起一探究竟吧!
应用场景
Tongue Twister 就是一款集成了华为HMS ML Kit实时语音识别服务的绕口令小游戏,游戏中一共有5个关卡,每一个关卡就是一段绕口令,通过秘诀就是凭借强大的实时语音识别,实时语音识别服务覆盖日常生活及工作中的众多领域,并且深度优化了购物搜索、影视搜索、音乐搜索以及导航等场景中的识别能力,识别准确率高,可轻松检测闯关者的发音,如若发音清晰标准即可过关,
下面我们一起来看看这个游戏的正确打开方式吧!
这么样,是不是心动了,那就一起来试试自己定制属于你的绕口令大闯关吧!
开发步骤
1.请参见云端鉴权信息实用须知,设置您应用的鉴权信息。
2.用户调用接口创建一个语音识别器。
MLAsrRecognizer mSpeechRecognizer = MLAsrRecognizer.createAsrRecognizer(context);
3.创建语音识别结果监听器回调。
/**
* Use the callback to implement the MLAsrListener API and methods in the API.
*/
protected class SpeechRecognitionListener implements MLAsrListener {
@Override
public void onStartListening() {
// The recorder starts to receive speech.
}
@Override
public void onStartingOfSpeech() {
// The user starts to speak, that is, the speech recognizer detects that the user starts to speak.
}
@Override
public void onVoiceDataReceived(byte[] data, float energy, Bundle bundle) {
// Return the original PCM stream and audio power to the user.
}
@Override
public void onRecognizingResults(Bundle partialResults) {
// Receive the recognized text from MLAsrRecognizer.
}
@Override
public void onResults(Bundle results) {
// Text data of ASR.
}
}
@Override
public void onError(int error, String errorMessage) {
// If you don't add this, there will be no response after you cut the network
}
@Override
public void onState(int state, Bundle params) {
// Notify the app status change.
}
}
4.将新建的结果监听器回调与语音识别器绑定
mSpeechRecognizer.setAsrListener(new SpeechRecognitionListener());
5.配置识别参数,调用启动语音识别
// Set parameters and start the audio device.
Intent mSpeechRecognizerIntent = new Intent(MLAsrConstants.ACTION_HMS_ASR_SPEECH);
mSpeechRecognizerIntent
// Set the language that can be recognized to English. If this parameter is not set,
// English is recognized by default. Example: "zh-CN": Chinese;"en-US": English;"fr-FR": French;"es-ES": Spanish;"de-DE": German;"it-IT": Italian.
.putExtra(MLAsrConstants.LANGUAGE, language)
// Set to return the recognition result along with the speech. If you ignore the setting, this mode is used by default. Options are as follows:
// MLAsrConstants.FEATURE_WORDFLUX: Recognizes and returns texts through onRecognizingResults.
// MLAsrConstants.FEATURE_ALLINONE: After the recognition is complete, texts are returned through onResults.
.putExtra(MLAsrConstants.FEATURE, MLAsrConstants.FEATURE_WORDFLUX);
mSpeechRecognizer.startRecognizing(mSpeechRecognizerIntent);
6.识别完成后,释放资源
if (mSpeechRecognizer != null) {
mSpeechRecognizer.destroy();
mSpeechRecognizer = null;
}
maven地址
buildscript {
repositories {
maven { url
'https://developer.huawei.com/repo/' }
}
}
allprojects {
repositories {
maven { url
'https://developer.huawei.com/repo/' }
}
}
引入SDK
dependencies {
// Automatic speech recognition Long voice SDK.
implementation 'com.huawei.hms:ml-computer-voice-realtimetranscription:2.0.3.300'
// Automatic speech recognition SDK.
implementation 'com.huawei.hms:ml-computer-voice-asr:2.0.3.300'
// Automatic speech recognition plugin.
implementation 'com.huawei.hms:ml-computer-voice-asr-plugin:2.0.3.300'
}
清单文件
<manifest
...
<
meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value="ocr/>
...
</
manifest>
权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
动态权限申请
private void requestCameraPermission() {
final String[] permissions = new String[]{Manifest.permission.RECORD_AUDIO};
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.
RECORD_AUDIO)) { ActivityCompat.requestPermissions(this,
permissions,
TongueTwisterActivity.
AUDIO_CODE);
return;
}
}
总结
除了在游戏当中的应用,实时语音识别服务在使用购物类App搜索商品时,可以将语音描述的商品名称或特征识别为文字从而搜索到目标商品。同样,在使用音乐类App时,可以将语音输入的歌名或歌手识别为文字进而搜索歌曲。另外,司机在驾驶过程中不方便输入文字时,可以将输入的语音转换为文字继而搜索目的地,让行车更加安全。
GitHub Demo Code
欲了解更多详情,请参阅:
华为开发者联盟官网:
https://developer.huawei.com/consumer/cn/hms/huawei-mlkit
获取开发指导文档:
参与开发者讨论请到Reddit社区:https://www.reddit.com/r/HuaweiDevelopers/
下载demo和示例代码请到Github:https://github.com/HMS-Core
解决集成问题请到Stack Overflow:
https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest