
回复
权限申请
方式一
下载voice-overlay_ohos源码,启动 DevEco Studio并打开本工程可直接编译运行entry示例工程。
方式二
在project的build.gradle中添加mavenCentral()的引用
repositories {
...
mavenCentral()
...
}
在entry的build.gradle中添加依赖
dependencies {
...
implementation 'com.gitee.archermind-ti:voice:1.0.0-beta'
...
}
进入应用,通过如下方式判断是否具有权限:
if (!Voice.isRecordAudioPermissionGranted(getApplicationContext())) {
((FractionAbility)getAbility()).getFractionManager()
.startFractionScheduler()
.replace(ResourceTable.Id_main_fraction, new VoicePermissionDialogFraction(this))
.submit();
} else {
((FractionAbility)getAbility()).getFractionManager()
.startFractionScheduler()
.replace(ResourceTable.Id_main_fraction, new VoicePermissionDialogFraction(this))
.submit();
}
参见 实现代码.
如果没有 MICROPHONE
权限则会弹出权限对话框,当确认权限后进入语音识别页面。
通过 VoiceSpeechRecognizer.ResultsListener
得到使用者语音转换的文字信息:
@Override
public void onResults(List<String> possibleTexts) {
// Do something with the results, for example:
StringBuilder sb = new StringBuilder();
for (String text : possibleTexts) {
sb.append(text);
sb.append('\n');
}
resultsText.setText(sb.toString());
}
参见 实现代码.
如果用户不允许权限申请, 应该向用户解释使用权限的理由; 如果用户拒绝权限后还想使用语音转换功能,应该引导用户去恢复权限使用; Voice overlay都能处理以上情况:
@Override
public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
if (Voice.isRecordPermissionWithResults(requestCode, grantResults)) {
if (Voice.isPermissionGranted(grantResults)) {
if (currentSlice.get() != null) {
currentSlice.get().showVoiceDialog();
}
} else {
Voice.showPermissionRationale(this, null, null);
}
}
}
参见 实现代码.
以上实现将展示权限说明和恢复权限的操作流程。
你可以在Behavior和Suggestions中定制说明信息,例如以下的示例:
String[] suggestions = {
"64GB Smartphone",
"Red running shoes",
"Cheap TV screen"};
voiceInput.setSuggestions(suggestions);
你可以通过如下方式关闭自动启用语音:
// Requires the user to click the mic to start listening.
voiceInput.setAutoStart(false);
// [...]
// you can also start listening programmatically with
VoiceInputDialogFraction.start()
你可以在 strings.json
替换相关的说明文字:
{
"string": [
{
"name": "app_name",
"value": "voice"
},
{
"name": "input_title_listening",
"value": "Listening…"
},
{
"name": "input_subtitle_listening",
"value": "Say something"
},
{
"name": "input_subtitle_suggestions",
"value": "Say something like:"
},
{
"name": "input_title_error",
"value": "Sorry, we didn't quite get that."
},
{
"name": "input_subtitle_error",
"value": "Try repeating your request."
},
{
"name": "input_hint_error",
"value": "Try again"
},
{
"name": "permission_title",
"value": "You can use voice search to find products."
},
{
"name": "permission_subtitle",
"value": "May we access your device’s microphone to enable voice search?"
},
{
"name": "permission_button_allow",
"value": "Allow microphone access"
},
{
"name": "permission_button_reject",
"value": "No"
},
{
"name": "permission_rationale",
"value": "Voice search requires this permission."
},
{
"name": "permission_button_again",
"value": "Request again?"
},
{
"name": "permission_enable_rationale",
"value": "Permission denied, allow it to use voice search."
},
{
"name": "permission_button_enable",
"value": "Allow recording"
},
{
"name": "permission_enable_instructions",
"value": "On the next screen, tap Permissions then Microphone."
}
]
}
你可以替换应用的布局,但要遵守如下的布局结构:
在权限页面中使用 voice_permission.xml
布局文件,包括:
$+id:close
的关闭页面按钮$+id:title
的标题文字$+id:subtitle
的副标题文字在语音转换页面使用 voice_input.xml
布局文件,包括:
$+id:microphone
的VoiceMicrophone
来处理语音操作$+id:suggestions
的操作说明文字$+id:close
的关闭页面按钮$+id:title
的标题文字$+id:subtitle
的副标题文字$+id:hint
的提示文字$+id:ripple
的 RippleView
的动画对象