Android 文本到语音转换并非始终有效
我想了解一下如何改进我目前的设置。应用程序首先读取FCM消息并提示用户说出语音命令 我没有收到任何错误,有时是有效的,但不是始终如一的。换句话说,程序有时会说话,有时不会。你建议我做什么 只需具备以下条件:Android 文本到语音转换并非始终有效,android,firebase,android-service,text-to-speech,firebase-cloud-messaging,Android,Firebase,Android Service,Text To Speech,Firebase Cloud Messaging,我想了解一下如何改进我目前的设置。应用程序首先读取FCM消息并提示用户说出语音命令 我没有收到任何错误,有时是有效的,但不是始终如一的。换句话说,程序有时会说话,有时不会。你建议我做什么 只需具备以下条件: public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { private static final String TAG = "Fireba
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
TextToSpeech instanceTTS =null;
Context context;
String moveactivity;
Intent i;
SpeechRecognizer mRecognizer;
Intent Activity_intent;
RecognitionListener listener;
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// new OperationTTS().execute(remoteMessage.getData().get("TTS"));
instanceTTS =new TextToSpeech(getApplicationContext(), new SdnaOnInitListener(remoteMessage.getData().get("TTS")));
instanceTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String s) {
}
@Override
public void onDone(String s) {
get_voice();
instanceTTS.shutdown();
instanceTTS = null;
}
@Override
public void onError(String s) {
}
});
//추가한것
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage remoteMessage) {
String message = remoteMessage.getData().get("message");
String title = remoteMessage.getData().get("title");
moveactivity = remoteMessage.getData().get("activity");
Activity_intent = SetActivity(moveactivity);//new Intent(this, MenuActivity.class);
Activity_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, Activity_intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); //화면 깨움
wakelock.acquire(5000);
notificationManager.notify(1231231 /* ID of notification */, notificationBuilder.build());
}
private void TextTTS(String message){
String text2 = message;
Log.d("Sdnalog", "TTS 말하는중?"+instanceTTS.isSpeaking());
//http://stackoverflow.com/a/29777304
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.d("Sdnalog", "TTS진입2");
ttsGreater21(text2);
} else {
Log.d("Sdnalog", "TTS진입3");
ttsUnder20(text2);
}
instanceTTS.shutdown();
}
private void ttsUnder20(String text) {
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
Log.d("Sdnalog", "TTS speak");
instanceTTS.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
String utteranceId=this.hashCode() + "";
Log.d("Sdnalog", "TTS speak");
instanceTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}
public class SdnaOnInitListener implements TextToSpeech.OnInitListener
{
String msg;
public SdnaOnInitListener(String s) {
msg = s;
Log.d("Sdnalog", "SdnaOnInitListener 생성자");
}
@Override
public void onInit ( int status){
if (status == TextToSpeech.SUCCESS)
{
instanceTTS.setLanguage(Locale.KOREAN);
Log.d("Sdnalog", "초기화 완료");
TextTTS(msg);
}
}
}
@Override
public void onDestroy() {
Log.d("slog", "onDestroy()");
super.onDestroy();
//Toast toast = Toast.makeText(context, "서비스 onDestroy", Toast.LENGTH_SHORT);
//toast.show();
}
@Override
public void onCreate() {
super.onCreate();
Log.d("slog", "onCreate()");
context = getApplicationContext();
//Toast toast = Toast.makeText(context, "서비스 onCreate", Toast.LENGTH_SHORT);
// toast.show();
}
@Override
public void onStart(Intent intent, int startId) {
Log.d("slog", "onStart()");
super.onStart(intent, startId);
//Toast toast = Toast.makeText(context, "서비스 onStart", Toast.LENGTH_SHORT);
//toast.show();
}
public Intent SetActivity(String a){
Intent intent;
String text;
if(a==null){text="menu";}
else{text=a;}
switch (text){
case "menu":
intent = new Intent(this, MenuActivity.class);
break;
case "air":
intent = new Intent(this, AirSensorList.class);break;
case "body":
intent = new Intent(this, BodySensorList.class);break;
case "commu":
intent = new Intent(this, CommuSensorList.class);break;
case "engine":
intent = new Intent(this, EngineSensorList.class);break;
case "gas":
intent = new Intent(this, GasSensorList.class);break;
case "lower":
intent = new Intent(this, LowerSensorList.class);break;
case "steering":
intent = new Intent(this, SteeringSensorList.class);break;
case "trans":
intent = new Intent(this, TransmissionSensorList.class);break;
case "map":
intent = new Intent(this, MapActivity.class);break;
case "notilog":
intent = new Intent(this, NotiLogList.class);break;
default:
intent = new Intent(this,MenuActivity.class);break;
}
return intent;
}
public void get_voice() {
Thread thread = new Thread(new Runnable() {
public void run() {
handler.sendEmptyMessage(0);
}
});
thread.start();
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
listener = new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
}
@Override
public void onResults(Bundle bundle) {
String key = "";
key = SpeechRecognizer.RESULTS_RECOGNITION;
ArrayList<String> mResult = bundle.getStringArrayList(key);
String[] rs = new String[mResult.size()];
mResult.toArray(rs);
//tv.setText(""+rs[0]);
if(rs[0].contains("예")||rs[0].contains("응")||rs[0].contains("그래")){
Context context = getApplicationContext();
Intent intent = SetActivity(moveactivity);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
};
i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko-KR");
mRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
mRecognizer.setRecognitionListener(listener);
mRecognizer.startListening(i);
// sendNotification(NotiApplyItem item) << 메소드 호출
msg = null;
}
};
}
公共类MyFirebaseMessagingService扩展了com.google.firebase.messagingService.FirebaseMessagingService{
私有静态最终字符串TAG=“FirebaseMsgService”;
TextToSpeech instancets=null;
语境;
字符串移动活动;
意图一;
语音识别器;
意向活动\意向;
识别听者;
//[启动接收消息]
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
//新操作TTS().execute(remoteMessage.getData().get(“TTS”));
instanceTTS=newtexttospeech(getApplicationContext(),newsdnaoninitlistener(remoteMessage.getData().get(“TTS”));
instancets.setOnPatternanceProgressListener(新的话语进度Listener(){
@凌驾
公共void onStart(字符串s){
}
@凌驾
公共无效onDone(字符串s){
得到你的声音();
instancets.shutdown();
instancets=null;
}
@凌驾
公共void onError(字符串s){
}
});
//추가한것
发送通知(远程消息);
}
私有无效发送通知(RemoteMessage RemoteMessage){
String message=remoteMessage.getData().get(“消息”);
字符串title=remoteMessage.getData().get(“title”);
moveactivity=remoteMessage.getData().get(“活动”);
Activity_intent=SetActivity(moveactivity);//新的intent(this,MenuActivity.class);
Activity\u intent.addFlags(intent.FLAG\u Activity\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(此,0/*请求代码*/,Activity_),
悬挂式帐篷(一杆旗帜);
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(此)
.setSmallIcon(R.mipmap.ic_启动器)
.setContentTitle(标题)
.setContentText(消息)
.setAutoCancel(真)
.setSound(defaultSoundUri)
.setContentIntent(挂起内容);
通知经理通知经理=
(NotificationManager)getSystemService(上下文通知服务);
PowerManager pm=(PowerManager)this.getSystemService(Context.POWER\u服务);
PowerManager.WakeLock WakeLock=pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,“标记”)//화면 깨움
wakelock.acquire(5000);
notificationManager.notify(1231231/*通知ID*/,notificationBuilder.build());
}
专用void textts(字符串消息){
字符串text2=消息;
Log.d(“Sdnalog”、“TTS말하는중?"+instancets.isSpeaking());
//http://stackoverflow.com/a/29777304
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.LOLLIPOP){
Log.d(“Sdnalog”、“TTS진입2");
ttsGreater21(text2);
}否则{
Log.d(“Sdnalog”、“TTS진입3");
ttsUnder20(文本2);
}
instancets.shutdown();
}
私有void ttsUnder20(字符串文本){
HashMap=newHashMap();
map.put(TextToSpeech.Engine.KEY_参数_话语_ID,“MessageId”);
Log.d(“Sdnalog”、“TTS语音”);
instancets.speak(text,TextToSpeech.QUEUE\u FLUSH,map);
}
@TargetApi(Build.VERSION\u code.LOLLIPOP)
私有void ttsGreater21(字符串文本){
字符串outtanceId=this.hashCode()+“”;
Log.d(“Sdnalog”、“TTS语音”);
instancets.speak(text,TextToSpeech.QUEUE\u FLUSH,null,outtanceid);
}
公共类SDNAonitListener实现TextToSpeech.onitListener
{
串味精;
公共SdnaOnInitListener(字符串s){
msg=s;
Log.d(“Sdnalog”、“SdnaOnInitListener생성자");
}
@凌驾
公共无效onInit(int状态){
if(status==TextToSpeech.SUCCESS)
{
instancets.setLanguage(Locale.KOREAN);
Log.d(“Sdnalog”초기화 완료");
textts(msg);
}
}
}
@凌驾
公共空间{
Log.d(“slog”、“onDestroy()”);
super.ondestory();
//Toast Toast=Toast.makeText(上下文,“서비스 烤面包片,长度(短);
//toast.show();
}
@凌驾
public void onCreate(){
super.onCreate();
Log.d(“slog”、“onCreate()”);
context=getApplicationContext();
//Toast Toast=Toast.makeText(上下文,“서비스 “一次制作”,吐司。长度(短);
//toast.show();
}
@凌驾
公共无效启动(Intent Intent,int startId){
Log.d(“slog”、“onStart()”);
super.onStart(intent,startId);
//Toast Toast=Toast.makeText(上下文,“서비스 “开始”,吐司。长度(短);
//toast.show();
}
公共意图集合活动(字符串a){
意图;
字符串文本;
如果(a==null){text=“menu”;}
else{text=a;}
开关(文本){
案例“菜单”:
intent=新intent(这是MenuActivity.class);
打破
“空气”一案:
intent=新intent(此,AirSensorList.class);break;
案例“正文”:
意图=新意图(此,BodySensorList.class);中断;
案例“commu”:
intent=新intent(this,CommuSensorList.class);break;
案例“发动机”:
意图=新意图(此,EngineSensorList.class);中断;
案例“气体”:
intent=新intent(这个,GasSensorList.class);break;
小写:
intent=新intent(this,LowerSensorList.class);break;
案例“转向”:
intent=新intent(this,SteeringSensorList.class);break;
案例“trans”:
意图=新意图(此,TransmissionSensorList.class);中断;
案例“地图”:
意向=新意向(本,