概述
添加在launcher中比较合适,但是launcher需要添加系统权限,需要添加开启和关闭语音唤醒的设置,菜单做在setting里,只是一个改变值,launcher这边接收到去设置
mWakeupEnable = 0 != Settings.System.getInt(
getApplicationContext().getContentResolver(), Settings.System.IFLYTEK_VOICE_WAKEUP_ENABLE, 0);
private void registerSettingChange(){
getApplicationContext().getContentResolver().registerContentObserver(
Settings.System.getUriFor(Settings.System.IFLYTEK_VOICE_WAKEUP_ENABLE), true,
mSettingsObserver);
mSettingsObserver.onChange(false);
}
private ContentObserver mSettingsObserver = new ContentObserver(new Handler()) {
public void onChange(boolean selfChange) {
mWakeupEnable = 0 != Settings.System.getInt(
getApplicationContext().getContentResolver(), Settings.System.IFLYTEK_VOICE_WAKEUP_ENABLE, 0);
Log.d(TAG, "ssss iflytek wakeup setting change enable="+mWakeupEnable);
if(mWakeupEnable)
{
registerStopSleep();
}
else {
unregisterStopSleep();
}
};
};
语音唤醒需要去讯飞网站上面去定制唤醒词,然后根据demo集成进service里就行了,注意的是亮屏的时候关闭唤醒,灭屏的时候开启唤醒,完整code如下
/*
* Copyright (C) 2014 MediaTek Inc.
* Modification based on code covered by the mentioned copyright
* and/or permission notice(s).
*/
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.*;
import android.util.Log;
import android.util.LongSparseArray;
import java.util.ArrayList;
import java.util.List;
//iflytek add
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.RequestListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechEvent;
import com.iflytek.cloud.VoiceWakeuper;
import com.iflytek.cloud.WakeuperListener;
import com.iflytek.cloud.WakeuperResult;
import com.iflytek.cloud.util.ResourceUtil;
import com.iflytek.cloud.util.ResourceUtil.RESOURCE_TYPE;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
// iflytek end
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.app.KeyguardManager;
//wakeup setting
import android.provider.Settings;
import android.database.ContentObserver;
import com.android.internal.widget.LockPatternUtils;
import android.os.UserHandle;
public class ziflytekSevice extends Service {
public static final String TAG = ziflytekSevice.class.getSimpleName();
public static final String ACTION_START_TRACKING = "com.android.launcher3.action.START_TRACKING";
private final boolean et_is_add_iflytek = true;
private static final int MSG_START = 1;
private static final int MSG_STOP = 2;
private static final int MSG_UPDATE = 3;
private static final int MSG_RESTART = 4;
private ScreenStatusReceiver mScreenStatusReceiver;
private boolean isUnlockScreenDisable=false;
private boolean mWakeupEnable;
private LockPatternUtils lockUtils=null;
@Override
public void onCreate() {
if(et_is_add_iflytek){
//iflytekInit();//iflytek add
//iflytekWakeUpHandle();
mWakeupEnable = 0 != Settings.System.getInt(
getApplicationContext().getContentResolver(), Settings.System.IFLYTEK_VOICE_WAKEUP_ENABLE, 0);
Message msg = mHandler.obtainMessage(MSG_START);
mHandler.sendMessageDelayed(msg,1000);
registerBroadcast();
registerStopSleep();
registerSettingChange();
}
}
//setting handle
private void registerSettingChange(){
getApplicationContext().getContentResolver().registerContentObserver(
Settings.System.getUriFor(Settings.System.IFLYTEK_VOICE_WAKEUP_ENABLE), true,
mSettingsObserver);
mSettingsObserver.onChange(false);
}
private ContentObserver mSettingsObserver = new ContentObserver(new Handler()) {
public void onChange(boolean selfChange) {
mWakeupEnable = 0 != Settings.System.getInt(
getApplicationContext().getContentResolver(), Settings.System.IFLYTEK_VOICE_WAKEUP_ENABLE, 0);
Log.d(TAG, "ssss iflytek wakeup setting change enable="+mWakeupEnable);
if(mWakeupEnable)
{
registerStopSleep();
}
else {
unregisterStopSleep();
}
};
};
//end setting handle
private class ScreenStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("android.intent.action.SCREEN_ON".equals(intent.getAction())) {
mHandler.removeMessages(MSG_RESTART);
if(mWakeupEnable)
iflyteWakeupEnable(false);
Log.d(TAG, "ssss message SCREEN_ON mWakeupEnable="+mWakeupEnable);
} else if("android.intent.action.SCREEN_OFF".equals(intent.getAction())) {
Log.d(TAG, "ssss message SCREEN_OFF isUnlockScreenDisable="+isUnlockScreenDisable+" mWakeupEnable="+mWakeupEnable);
mHandler.removeMessages(MSG_RESTART);
if(mWakeupEnable){
Message msg = mHandler.obtainMessage(MSG_RESTART);
mHandler.sendMessageDelayed(msg,1000);
}
if(isUnlockScreenDisable)
ziflytekSevice.this.setDisableKeyguard(false);
}
}
}
private void registerBroadcast(){
mScreenStatusReceiver = new ScreenStatusReceiver();
IntentFilter filterIF = new IntentFilter();//new一个intent过滤器
filterIF.addAction("android.intent.action.SCREEN_ON");//增加亮屏操作
filterIF.addAction("android.intent.action.SCREEN_OFF");//增加灭屏操作
registerReceiver(mScreenStatusReceiver, filterIF);//注册监听
}
private void unregisterBroadcast(){
unregisterReceiver(mScreenStatusReceiver);
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message m) {
switch (m.what) {
case MSG_START:
iflytekInit();//iflytek add
iflytekWakeUpHandle();
Log.d(TAG, "ssss message MSG_START");
break;
case MSG_RESTART:
Log.d(TAG, "ssss message MSG_RESTART mIvw="+mIvw);
mHandler.removeMessages(MSG_RESTART);
iflyteWakeupEnable(true);
break;
case MSG_UPDATE:
break;
}
}
};
private void iflyteWakeupEnable(boolean isEnable){
mIvw = VoiceWakeuper.getWakeuper();
if(mIvw!=null){
if(isEnable==false)
mIvw.stopListening();
else{
mIvw.stopListening();
mIvw.startListening(mWakeuperListener);
}
}
}
//iflytek add
PowerManager mPowerManager=null;
// 语音唤醒对象
private VoiceWakeuper mIvw=null;
// 唤醒结果内容
private String resultString;
private final static int MAX = 3000;
private final static int MIN = 0;
private int curThresh = 1450;
private String threshStr = "门限值:";
private String keep_alive = "1";
private String ivwNetMode = "0";
private void iflytekInit(){
Log.d(TAG, "ssss iflytekInit");
StringBuffer param = new StringBuffer();
param.append("appid="+getString(R.string.app_id));
param.append(",");
// 设置使用v5+
param.append("engine_mode"+"="+"msc");
SpeechUtility.createUtility(getApplicationContext(), param.toString());
}
private void iflytekDestory(){
// 销毁合成对象
mIvw = VoiceWakeuper.getWakeuper();
if (mIvw != null) {
mIvw.destroy();
}
unregisterBroadcast();
}
private void iflytekWakeUpHandle(){
mIvw = VoiceWakeuper.createWakeuper(this, null);
mIvw = VoiceWakeuper.getWakeuper();
Log.d(TAG, "ssss mIvw="+mIvw);
if(mIvw != null) {
// 清空参数
mIvw.setParameter(SpeechConstant.PARAMS, null);
// 唤醒门限值,根据资源携带的唤醒词个数按照“id:门限;id:门限”的格式传入
mIvw.setParameter(SpeechConstant.IVW_THRESHOLD, "0:"+ curThresh);
// 设置唤醒模式
mIvw.setParameter(SpeechConstant.IVW_SST, "wakeup");
// 设置持续进行唤醒
mIvw.setParameter(SpeechConstant.KEEP_ALIVE, keep_alive);
// 设置闭环优化网络模式
mIvw.setParameter(SpeechConstant.IVW_NET_MODE, ivwNetMode);
// 设置唤醒资源路径
mIvw.setParameter(SpeechConstant.IVW_RES_PATH, getResource());
// 设置唤醒录音保存路径,保存最近一分钟的音频
mIvw.setParameter( SpeechConstant.IVW_AUDIO_PATH, Environment.getExternalStorageDirectory().getPath()+"/msc/ivw.wav" );
mIvw.setParameter( SpeechConstant.AUDIO_FORMAT, "wav" );
// 如有需要,设置 NOTIFY_RECORD_DATA 以实时通过 onEvent 返回录音音频流字节
//mIvw.setParameter( SpeechConstant.NOTIFY_RECORD_DATA, "1" );
// 启动唤醒
/* mIvw.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");*/
mIvw.startListening(mWakeuperListener);
/*File file = new File(Environment.getExternalStorageDirectory().getPath() + "/msc/ivw1.wav");
byte[] byetsFromFile = getByetsFromFile(file);
mIvw.writeAudio(byetsFromFile,0,byetsFromFile.length);*/
mIvw.stopListening();
} else {
;//showTip("唤醒未初始化");
}
}
KeyguardManager.KeyguardLock kl=null;
KeyguardManager km=null;
private void setDisableKeyguard(boolean isDisable){
if(km==null)
km = (KeyguardManager)getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
if(kl==null)
kl = km.newKeyguardLock("unlock");
if(isDisable)
kl.disableKeyguard();
else
kl.reenableKeyguard();
isUnlockScreenDisable=isDisable;
}
private WakeuperListener mWakeuperListener = new WakeuperListener() {
@Override
public void onResult(WakeuperResult result) {
Log.d(TAG, "onResult");
if(!"1".equalsIgnoreCase(keep_alive)) {
;//setRadioEnable(true);
}
try {
String text = result.getResultString();
JSONObject object;
object = new JSONObject(text);
StringBuffer buffer = new StringBuffer();
buffer.append("【RAW】 "+text);
buffer.append("n");
buffer.append("【操作类型】"+ object.optString("sst"));
buffer.append("n");
buffer.append("【唤醒词id】"+ object.optString("id"));
buffer.append("n");
buffer.append("【得分】" + object.optString("score"));
buffer.append("n");
buffer.append("【前端点】" + object.optString("bos"));
buffer.append("n");
buffer.append("【尾端点】" + object.optString("eos"));
resultString =buffer.toString();
} catch (JSONException e) {
resultString = "结果解析出错";
e.printStackTrace();
}
//textView.setText(resultString);
Log.d(TAG, "ssssjj"+ resultString);
Toast.makeText(getApplicationContext(), "wake up", Toast.LENGTH_LONG).show();
wakeUp();
}
private void wakeUp(){
PowerManager pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.FULL_WAKE_LOCK, "bright");
wl.acquire();
wl.release();
if(ziflytekSevice.this.getCurrentLockScreenIsSecure()==false)
ziflytekSevice.this.setDisableKeyguard(true);
}
@Override
public void onError(SpeechError error) {
//showTip(error.getPlainDescription(true));
//setRadioEnable(true);
}
@Override
public void onBeginOfSpeech() {
}
@Override
public void onEvent(int eventType, int isLast, int arg2, Bundle obj) {
switch( eventType ){
// EVENT_RECORD_DATA 事件仅在 NOTIFY_RECORD_DATA 参数值为 真 时返回
case SpeechEvent.EVENT_RECORD_DATA:
final byte[] audio = obj.getByteArray( SpeechEvent.KEY_EVENT_RECORD_DATA );
Log.i( TAG, "ivw audio length: "+audio.length );
break;
}
}
@Override
public void onVolumeChanged(int volume) {
}
};
private String getResource() {
final String resPath = ResourceUtil.generateResourcePath(this, RESOURCE_TYPE.assets, "ivw/"+getString(R.string.app_id)+".jet");
Log.d( TAG, "resPath: "+resPath );
return resPath;
}
//end iflytek
private PowerManager.WakeLock mWakeLock=null;
private void registerStopSleep(){
PowerManager pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
if (mWakeLock == null)
{
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"battery service");
mWakeLock.acquire();
}
}
private void unregisterStopSleep(){
if (mWakeLock != null){
mWakeLock.release();
mWakeLock=null;
}
}
@Override
public void onDestroy() {
if(et_is_add_iflytek){
iflytekDestory();
unregisterStopSleep();
}
}
private boolean getCurrentLockScreenIsSecure(){
if(lockUtils==null)
lockUtils = new LockPatternUtils(this);
boolean lockscreenSecure = lockUtils.isSecure(UserHandle.myUserId());
Log.d( TAG, "ssss current lock is secure: "+lockscreenSecure );
return lockscreenSecure;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "Received start id " + startId + ": " + intent);
return START_STICKY;
}
public class ziflytekSeviceInterface extends Binder {
ziflytekSevice getService() {
return ziflytekSevice.this;
}
}
private final IBinder mBinder = new ziflytekSeviceInterface();
public IBinder onBind(Intent intent) {
return mBinder;
}
}
最后
以上就是高贵柠檬为你收集整理的添加讯飞语音唤醒系统并解锁系统的全部内容,希望文章能够帮你解决添加讯飞语音唤醒系统并解锁系统所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复