[add] //////暂时解决小智语音不播报,后续需要解决获取不到其他应用进程的问题,重新解决小智语音不播报问题isVoiceServiceReady方法应默认返回false

This commit is contained in:
liujing
2021-05-27 21:16:44 +08:00
parent fe3426cde4
commit dd1d3bdfa2
15 changed files with 576 additions and 9 deletions

1
tts/tts-pad/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

54
tts/tts-pad/build.gradle Normal file
View File

@@ -0,0 +1,54 @@
apply plugin: 'com.android.library'
apply plugin: 'com.alibaba.arouter'
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
// buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode Integer.valueOf(VERSION_CODE)
versionName getValueFromRootProperties("${project.name.replace("-", "_").toUpperCase()}_VERSION")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation rootProject.ext.dependencies.androidxappcompat
implementation rootProject.ext.dependencies.arouter
annotationProcessor rootProject.ext.dependencies.aroutercompiler
api rootProject.ext.dependencies.aiassist
api rootProject.ext.dependencies.aiassistReplace
if (Boolean.valueOf(RELEASE)) {
implementation rootProject.ext.dependencies.ttsbase
implementation rootProject.ext.dependencies.mogoutils
} else {
implementation project(":tts:tts-base")
implementation project(":foudations:mogo-utils")
}
}
apply from: new File(rootProject.rootDir, "gradle/upload.gradle").toString()

View File

View File

@@ -0,0 +1,3 @@
GROUP=com.mogo.tts
POM_ARTIFACT_ID=tts-pad
VERSION_CODE=1

21
tts/tts-pad/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mogo.tts.pad">
/
</manifest>

View File

@@ -0,0 +1,480 @@
package com.mogo.tts.pad;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mogo.tts.base.IMogoTTS;
import com.mogo.tts.base.IMogoTTSCallback;
import com.mogo.tts.base.MogoTTSConstants;
import com.mogo.tts.base.PreemptType;
import com.mogo.utils.AppUtils;
import com.mogo.utils.logger.Logger;
import com.zhidao.auto.platform.voice.VoiceClient;
import com.zhidao.voicesdk.MogoVoiceManager;
import com.zhidao.voicesdk.MogoVoiceManagerImpl;
import com.zhidao.voicesdk.callback.OnConnStatusListener;
import com.zhidao.voicesdk.callback.OnTtsListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public
/**
* @author congtaowang
* @since 2020/10/12
* <p>
* 描述
*/
@Route( path = MogoTTSConstants.API_PATH )
class PadTTS implements IMogoTTS, VoiceClient.VoiceCmdCallBack, OnTtsListener {
private static final String TAG = "ZhiTTS";
private String mLastQAndASpeakText;
private boolean mHasFlush = false;
private boolean mInitReady = false;
private Context mContext;
public synchronized void release() {
Logger.d( TAG, "release" );
if ( mCmdMap != null && !mCmdMap.isEmpty() && mVoiceClient != null ) {
for ( String cmd : mCmdMap.keySet() ) {
try {
mVoiceClient.unRegisterCustomWakeupCmd( cmd );
} catch ( Exception e ) {
}
}
}
mQAndAMap.clear();
mVoiceClient.release();
mSpeakVoiceMap.clear();
mCacheUnWakeupCommands.clear();
mContext = null;
}
private VoiceClient mVoiceClient;
private MogoVoiceManager mogoVoiceManager;
// 免唤醒指令
private Map< String, List< IMogoTTSCallback > > mCmdMap = new HashMap<>();
// 问答指令
private Map< String, IMogoTTSCallback > mQAndAMap = new HashMap<>();
// 单独的语音播放
private Map< String, IMogoTTSCallback > mSpeakVoiceMap = new HashMap<>();
private Map< String, String[] > mCacheUnWakeupCommands = new ConcurrentHashMap<>();
private void initFlushStatus() {
if ( !mHasFlush ) {
mHasFlush = isVoiceServiceReady( mContext );
}
}
/**
* 初始化
*/
private void initSpeech( Context context ) {
mogoVoiceManager = MogoVoiceManagerImpl.getInstance();
mogoVoiceManager.init( context, new OnConnStatusListener() {
@Override
public void onSuccess() {
mInitReady = true;
}
@Override
public void onFailed() {
}
} );
}
/**
* 是否语音注册成功
*
* @return
*/
@Override
public boolean hasFlush() {
return mHasFlush;
}
@Override
public void onCmdSelected( String cmd ) {
if ( !mCmdMap.containsKey( cmd ) ) {
return;
}
Logger.d( TAG, "received command: %s", cmd );
Iterator< IMogoTTSCallback > iterator = null;
try {
List< IMogoTTSCallback > cmdCallBacks = mCmdMap.get( cmd );
iterator = new ArrayList<>( cmdCallBacks ).iterator();
} catch ( Exception e ) {
}
while ( iterator != null && iterator.hasNext() ) {
IMogoTTSCallback callBack = iterator.next();
if ( callBack != null ) {
callBack.onCmdSelected( cmd );
}
}
}
@Override
public void onCmdAction( String speakText ) {
if ( !TextUtils.isEmpty( mLastQAndASpeakText ) ) {
IMogoTTSCallback cmdCallBack = mQAndAMap.remove( mLastQAndASpeakText );
if ( cmdCallBack != null ) {
cmdCallBack.onCmdAction( speakText );
}
}
}
@Override
public void onCmdCancel( String speakText ) {
if ( !TextUtils.isEmpty( mLastQAndASpeakText ) ) {
IMogoTTSCallback cmdCallBack = mQAndAMap.remove( mLastQAndASpeakText );
if ( cmdCallBack != null ) {
cmdCallBack.onCmdCancel( speakText );
}
}
}
@Override
public void onSpeakEnd( String speakText ) {
if ( mQAndAMap.containsKey( speakText ) ) {
mLastQAndASpeakText = speakText;
IMogoTTSCallback cmdCallBack = mQAndAMap.get( speakText );
if ( cmdCallBack != null ) {
cmdCallBack.onSpeakEnd( speakText );
return;
}
}
IMogoTTSCallback callBack = mSpeakVoiceMap.remove( speakText );
if ( callBack != null ) {
callBack.onSpeakEnd( speakText );
}
}
@Override
public void onSpeakSelectTimeOut( String speakText ) {
if ( mQAndAMap.containsKey( speakText ) ) {
if ( TextUtils.equals( speakText, mLastQAndASpeakText ) ) {
mLastQAndASpeakText = null;
}
IMogoTTSCallback cmdCallBack = mQAndAMap.remove( speakText );
if ( cmdCallBack != null ) {
cmdCallBack.onSpeakSelectTimeOut( speakText );
return;
}
}
IMogoTTSCallback callBack = mSpeakVoiceMap.remove( speakText );
if ( callBack != null ) {
callBack.onSpeakSelectTimeOut( speakText );
}
}
/**
* 语音播报
*
* @param text
*/
public void speakTTSVoice( String text, IMogoTTSCallback callBack ) {
try {
initFlushStatus();
if ( mHasFlush ) {
mSpeakVoiceMap.put( text, callBack );
mVoiceClient.speakDefault( text );
}
} catch ( Exception e ) {
}
}
/**
* 语音播报
*
* @param text
*/
public void speakTTSVoice( String text ) {
try {
initFlushStatus();
if ( mHasFlush ) {
mVoiceClient.speakDefault( text );
}
} catch ( Exception e ) {
}
}
/**
* 语音播报
*
* @param text 播报内容
* @param type 播报策略
*/
public void speakTTSVoice( String text, PreemptType type, IMogoTTSCallback callBack ) {
try {
initFlushStatus();
if ( mHasFlush ) {
mSpeakVoiceMap.put( text, callBack );
VoiceClient.PreemptType preemptType = VoiceClient.PreemptType.PREEMPT_TYPE_NONE;
if ( type != null ) {
switch ( type ) {
case PREEMPT_TYPE_NEXT:
preemptType = VoiceClient.PreemptType.PREEMPT_TYPE_NEXT;
break;
case PREEMPT_TYPE_FLUSH:
preemptType = VoiceClient.PreemptType.PREEMPT_TYPE_FLUSH;
break;
case PREEMPT_TYPE_IMMEDIATELY:
preemptType = VoiceClient.PreemptType.PREEMPT_TYPE_IMMEADIATELY;
break;
case PREEMPT_TYPE_IMMEDIATELY_WITHOUT_CANCEL:
preemptType = VoiceClient.PreemptType.PREEMPT_TYPE_IMMEADIATELY_WITHOUT_CANCLE;
break;
}
}
mVoiceClient.speakTypeText( text, preemptType );
}
} catch ( Exception e ) {
}
}
/**
* 问答类型语音注册:默认确认和取消
*
* @param tts 播报内容
*/
public void speakQAndACmd( String tts, IMogoTTSCallback callBack ) {
initFlushStatus();
if ( mHasFlush ) {
mQAndAMap.put( tts, callBack );
mVoiceClient.speakTtsAndRegistCmd( tts );
}
}
/**
* 问答类型语音注册
*
* @param tts 播报内容
* @param okCmds 确认命令唤醒词
* @param cancelCmds 取消命令唤醒词
*/
public void speakQAndACmd( String tts, String[] okCmds, String[] cancelCmds, IMogoTTSCallback callBack ) {
initFlushStatus();
if ( mHasFlush ) {
mQAndAMap.put( tts, callBack );
mVoiceClient.speakTtsAndRegistCmd( tts, okCmds, cancelCmds );
}
}
/**
* 注册免唤醒命令
*
* @param cmd
* @param cmdWords
* @param callBack
*/
public void registerUnWakeupCommand( String cmd, String[] cmdWords, IMogoTTSCallback callBack ) {
if ( !mCmdMap.containsKey( cmd ) ) {
mCmdMap.put( cmd, new ArrayList<>() );
}
mCmdMap.get( cmd ).add( callBack );
initFlushStatus();
if ( mHasFlush ) {
mVoiceClient.registerCustomWakeupCmd( cmd, cmdWords );
mCacheUnWakeupCommands.remove( cmd );
}
Logger.i( TAG, "cache un wakeup command2. %s", cmd );
mCacheUnWakeupCommands.put( cmd, cmdWords );
}
/**
* 注册免唤醒命令
*
* @param cmd
* @param cmdWords
*/
private void registerUnWakeupCommand( String cmd, String[] cmdWords ) {
initFlushStatus();
if ( mHasFlush ) {
mVoiceClient.registerCustomWakeupCmd( cmd, cmdWords );
mCacheUnWakeupCommands.remove( cmd );
}
Logger.i( TAG, "cache un wakeup command. %s", cmd );
mCacheUnWakeupCommands.put( cmd, cmdWords );
}
/**
* 注销免唤醒命令
*
* @param cmd
*/
public synchronized void unregisterUnWakeupCommand( String cmd ) {
mCmdMap.remove( cmd );
try {
mVoiceClient.unRegisterCustomWakeupCmd( cmd );
} catch ( Exception e ) {
}
mCacheUnWakeupCommands.remove( cmd );
}
/**
* 注销免唤醒命令
*
* @param cmd
*/
public synchronized void unregisterUnWakeupCommand( String cmd, IMogoTTSCallback callBack ) {
if ( mCmdMap.containsKey( cmd ) ) {
List< IMogoTTSCallback > callBacks = mCmdMap.get( cmd );
if ( callBacks != null ) {
callBacks.remove( callBack );
}
if ( callBacks.isEmpty() ) {
mCmdMap.remove( cmd );
try {
mVoiceClient.unRegisterCustomWakeupCmd( cmd );
} catch ( Exception e ) {
}
mCacheUnWakeupCommands.remove( cmd );
}
}
}
public static void startAssistant( Context context ) {
startAssistant( context, 1 );
}
/**
* @param context
* @param status window_start_cancel 0 - 结束, 1 - 显示, 2 - 未激活调试进入
*/
public static void startAssistant( Context context, int status ) {
final Intent intent = new Intent();
intent.setFlags( Intent.FLAG_INCLUDE_STOPPED_PACKAGES );
intent.setAction( "pvetec.intent.action.txz.switch" );
intent.putExtra( "window_start_cancel", status );
intent.putExtra( "extra_switch_type", "window_start_cancel" );
Logger.d( TAG, "status = %d", status );
context.sendBroadcast( intent );
}
public synchronized void flush() {
if ( mCacheUnWakeupCommands.isEmpty() ) {
return;
}
mHasFlush = true;
Logger.d( TAG, "flush cache voice command when voice service ready." );
final Map< String, String[] > tmp = new HashMap<>( mCacheUnWakeupCommands );
for ( String cmd : tmp.keySet() ) {
registerUnWakeupCommand( cmd, tmp.get( cmd ) );
}
}
private boolean isVoiceServiceReady( Context context ) {
if ( AppUtils.isProcessRunning( context, AppUtils.getPackageUid( context, "com.zhidao.speech" ) )) {
return true;
} else if ( AppUtils.isProcessRunning( context, AppUtils.getPackageUid( context, "com.txznet.txz" ) )
&& AppUtils.isProcessRunning( context, AppUtils.getPackageUid( context, "com.txznet.adapter" ) ) ) {
Logger.d( TAG, "txz is voiceServiceReady" );
return true;
}
return true;
}
public void speakTTSAndDuck( String text ) {
speakTTSAndDuck( text, null );
}
public void speakTTSAndDuck( String text, IMogoTTSCallback callBack ) {
try {
if ( mInitReady ) {
mSpeakVoiceMap.put( text, callBack );
mogoVoiceManager.toSpeak( text, -3, this );
}
} catch ( Exception e ) {
}
}
public void shutUp( String ttsId, String text ) {
try {
mSpeakVoiceMap.remove( text );
mogoVoiceManager.shutUp( ttsId );
} catch ( Exception e ) {
}
}
/**
* 打断上一条正在播报的语音内容仅在Speech上生效TXZ为空实现
* 语音SDK生效版本从1.0.8.4版本起
*/
public void breakOffSpeak() {
mVoiceClient.breakOffSpeak();
}
public void clearTTSCallback( String text ) {
try {
mSpeakVoiceMap.remove( text );
} catch ( Exception e ) {
e.printStackTrace();
}
}
@Override
public void onTtsStart( String ttsId, String text ) {
IMogoTTSCallback callBack = mSpeakVoiceMap.get( text );
if ( callBack != null ) {
callBack.onTTSStart( ttsId, text );
}
}
@Override
public void onTtsFinish( String ttsId, String text ) {
IMogoTTSCallback callBack = mSpeakVoiceMap.remove( text );
if ( callBack != null ) {
callBack.onTTSEnd( ttsId, text );
}
}
@Override
public void onTtsError( String ttsId, String text ) {
IMogoTTSCallback callBack = mSpeakVoiceMap.remove( text );
if ( callBack != null ) {
callBack.onTTSError( ttsId, text );
}
}
@Override
public void init( Context context ) {
if ( context != null ) {
mContext = context.getApplicationContext();
mVoiceClient = new VoiceClient( mContext );
mVoiceClient.setCallBack( this );
initFlushStatus();
initSpeech( context );
Logger.w( TAG, "voice is ready = %s", mHasFlush );
}
}
@Override
public void startAIAssist( Context context ) {
startAssistant( context, 1 );
}
@Override
public void startAIAssist( Context context, int status ) {
final Intent intent = new Intent();
intent.setFlags( Intent.FLAG_INCLUDE_STOPPED_PACKAGES );
intent.setAction( "pvetec.intent.action.txz.switch" );
intent.putExtra( "window_start_cancel", status );
intent.putExtra( "extra_switch_type", "window_start_cancel" );
Logger.d( TAG, "status = %d", status );
context.sendBroadcast( intent );
}
}

View File

@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mogo.tts.zhi">
package="com.mogo.tts.pad">
/
</manifest>

View File

@@ -1,4 +1,4 @@
package com.mogo.tts.zhi;
package com.mogo.tts.pad;
import android.content.Context;
import android.content.Intent;