添加语音播报

This commit is contained in:
wangdongsheng 2021-09-16 18:29:12 +08:00
parent e25b23ff09
commit be694155e1
14 changed files with 187 additions and 43 deletions

View File

@ -34,7 +34,11 @@
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
<application
android:name=".api.UserApplication"
android:allowBackup="true"

View File

@ -27,6 +27,8 @@ import com.navinfo.outdoor.http.HttpInterface;
import com.navinfo.outdoor.http.OkGoBuilder;
import com.navinfo.outdoor.util.Base64;
import com.navinfo.outdoor.util.Md5Util;
import com.navinfo.outdoor.util.SpeakMode;
import com.navinfo.outdoor.util.SystemTTS;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
@ -113,6 +115,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
etLoginPaw.setText(pass_word);
}
}
@Override
@ -144,7 +147,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
return;
}
if (!cbStatement.isChecked()){
Toast.makeText(this, "请先同意免责声明", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "请先同意用户协议", Toast.LENGTH_SHORT).show();
return;
}else {
initLogIn(name, paw);

View File

@ -31,6 +31,7 @@ import com.navinfo.outdoor.room.PoiEntity;
import com.navinfo.outdoor.util.AWMp4ParserHelper;
import com.navinfo.outdoor.util.GeometryTools;
import com.navinfo.outdoor.util.MyTecentLocationSource;
import com.navinfo.outdoor.util.SystemTTS;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraLogger;
import com.otaliastudios.cameraview.CameraView;
@ -82,7 +83,10 @@ public class PhotographActivity extends BaseActivity implements View.OnClickList
@Override
public void onClick(View v) {
if (v.getId() == R.id.capture_picture) {
SystemTTS.getInstance(PhotographActivity.this).playText("11aa");
cameraView.takePicture();
}
}
@ -126,6 +130,7 @@ public class PhotographActivity extends BaseActivity implements View.OnClickList
assert file != null;
intent.putExtra("file",file.getPath());
setResult(0x104,intent);
SystemTTS.getInstance(PhotographActivity.this).stopSpeak();
finish();
}
});

View File

@ -493,6 +493,8 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
tencentMap.getCameraPosition().zoom, //目标缩放级别
0, //目标倾斜角
tencentLocation.getBearing())); //目标旋转角 0~360° (正北方为0)
float speed = tencentLocation.getSpeed();
Log.d("TAG", "onEventMessageMainThread: "+speed);
tencentMap.animateCamera(cameraSigma);
}
}

View File

@ -226,12 +226,7 @@ public class UserActivity extends BaseActivity implements View.OnClickListener {
}
});
} else {
MessageDialog.show(UserActivity.this, "提示","是否保存", "确定", "取消").setOkButton(new OnDialogButtonClickListener() {
@Override
public boolean onClick(BaseDialog baseDialog, View v) {
return false;
}
});
Toast.makeText(UserActivity.this, response.getMessage()+"", Toast.LENGTH_SHORT).show();
}
}
@Override

View File

@ -10,6 +10,7 @@ import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.DBCookieStore;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpHeaders;
import com.navinfo.outdoor.util.SystemTTS;
import com.navinfo.outdoor.util.TalentLocationUtils;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.umcrash.UMCrash;
@ -41,7 +42,6 @@ public class UserApplication extends Application {
TalentLocationUtils.getInstance(this).startLocation(this);
}
public static UserApplication getUserApplication() {

View File

@ -193,7 +193,7 @@ public class CapacityMeasurementFragment extends BaseFragment implements View.On
MessageDialog.show((AppCompatActivity) Objects.requireNonNull(getActivity()), "提示", "不通过", "确定", "取消").setOkButton(new OnDialogButtonClickListener() {
@Override
public boolean onClick(BaseDialog baseDialog, View v) {
// Toast.makeText(getContext(), "点击了确定", Toast.LENGTH_SHORT).show();
return false;
}
});
@ -201,7 +201,6 @@ public class CapacityMeasurementFragment extends BaseFragment implements View.On
MessageDialog.show((AppCompatActivity) Objects.requireNonNull(getActivity()), "提示", "通过", "确定", "取消").setOkButton(new OnDialogButtonClickListener() {
@Override
public boolean onClick(BaseDialog baseDialog, View v) {
// Toast.makeText(getContext(), "点击了确定", Toast.LENGTH_SHORT).show();
return false;
}
});

View File

@ -185,7 +185,7 @@ public class RegisterFragment extends BaseFragment implements View.OnClickListen
httpParams.put("sessionId", sessionId);
}
if (!ivRegisterCheck.isChecked()){
Toast.makeText(getActivity(), "请先同意免责声明", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "请先同意用户协议", Toast.LENGTH_SHORT).show();
return;
}
long time = System.currentTimeMillis();

View File

@ -434,8 +434,6 @@ public class TreasureFragment extends BaseFragment implements View.OnClickListen
Integer telLength = getPhoneBean.getBody().getTelLength();
Constant.CODE = code;
Constant.TelLength = telLength;
} else {
Toast.makeText(getActivity(), getPhoneBean.getMessage() + "", Toast.LENGTH_SHORT).show();
}
}
@ -481,7 +479,6 @@ public class TreasureFragment extends BaseFragment implements View.OnClickListen
.url(HttpInterface.USER_LOCATION)
.cls(UserBean.class)
.params(httpParams)
.token(Constant.ACCESS_TOKEN)
.postRequest(new Callback<UserBean>() {
@Override

View File

@ -0,0 +1,141 @@
package com.navinfo.outdoor.util;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.kongzue.dialog.util.DialogSettings;
import com.kongzue.dialog.v3.MessageDialog;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Set;
//语音类
public class SpeakMode extends Activity implements TextToSpeech.OnInitListener{
private Activity mActivity;
private TextToSpeech mTextToSpeech;//TTS对象
private int status;
private int MY_DATA_CHECK_CODE = 0;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x11:
try {
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, "STREAM_NOTIFICATION");//设置播放类型音频流类型
mTextToSpeech.speak(msg.obj + "", TextToSpeech.QUEUE_ADD, params);//将这个发音任务添加当前任务之后
//BaseToast.makeText(mActivity,msg.obj+"",Toast.LENGTH_LONG).show();
mTextToSpeech.playSilence(100, TextToSpeech.QUEUE_ADD, params);//间隔多长时间
} catch (Exception e) {
}
break;
}
}
};
public SpeakMode(Activity activity) {
mActivity = activity;
if (mActivity != null && !mActivity.isFinishing())
this.mTextToSpeech = new TextToSpeech(this.mActivity, this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
public void setData(String json) {
}
@Override
public void onInit(int status) {
this.status = status;
if (this.mTextToSpeech != null) {
int result = this.mTextToSpeech.setLanguage(Locale.CHINESE);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
if (mActivity != null && !mActivity.isFinishing()) {
DialogSettings.style = DialogSettings.STYLE.STYLE_KONGZUE;
MessageDialog.show((AppCompatActivity) mActivity, "提示", "设备不支持语音播报。", "确定");
}
}
}
Log.i("TextToSpeechDemo", String.valueOf(status));
}
//读语音处理
public void speakText(final String message) {
new Thread(new Runnable() {
@Override
public void run() {
if (mTextToSpeech != null) {
Set<Locale> availableLanguages = mTextToSpeech.getAvailableLanguages();
List<TextToSpeech.EngineInfo> engines = mTextToSpeech.getEngines();
int result = mTextToSpeech.setLanguage(Locale.CHINESE);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
} else {
if (mTextToSpeech != null && mTextToSpeech.isSpeaking()) {
while (mTextToSpeech.isSpeaking()) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
Message msg = new Message();
msg.what = 0x11;
msg.obj = message;
mHandler.sendMessage(msg);
}
}
}
}).start();
}
public void stopSpeek() {
try {
if (this.mTextToSpeech != null && this.mTextToSpeech.isSpeaking()) {
this.mTextToSpeech.stop();
}
} catch (Exception e) {
}
}
}

View File

@ -1,12 +1,16 @@
package com.navinfo.outdoor.util;
import android.annotation.SuppressLint;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.widget.Toast;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@SuppressLint("NewApi")
public class SystemTTS extends UtteranceProgressListener implements TTS, TextToSpeech.OnUtteranceCompletedListener {
private Context mContext;
private static SystemTTS singleton;
@ -29,29 +33,29 @@ public class SystemTTS extends UtteranceProgressListener implements TTS, TextToS
textToSpeech = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
//系统语音初始化成功
if (i == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.CHINA);
textToSpeech.setPitch(1.0f);// 设置音调值越大声音越尖女生值越小则变成男声,1.0是常规
textToSpeech.setSpeechRate(1.0f);
textToSpeech.setOnUtteranceProgressListener(SystemTTS.this);
textToSpeech.setOnUtteranceCompletedListener(SystemTTS.this);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
//系统不支持中文播报
isSuccess = false;
}
}
}
}
});
//系统语音初始化成功
int result = textToSpeech.setLanguage(Locale.getDefault());
textToSpeech.setPitch(1.0f);// 设置音调值越大声音越尖女生值越小则变成男声,1.0是常规
textToSpeech.setSpeechRate(1.0f);
textToSpeech.setOnUtteranceProgressListener(SystemTTS.this);
textToSpeech.setOnUtteranceCompletedListener(SystemTTS.this);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
//系统不支持中文播报
isSuccess = false;
}
}
public void playText(String playText) {
if (!isSuccess) {
Toast.makeText(mContext, "系统不支持中文播报", Toast.LENGTH_SHORT).show();
return;
}
// if (!isSuccess) {
// return;
// }
Set<Locale> availableLanguages = textToSpeech.getAvailableLanguages();
List<TextToSpeech.EngineInfo> engines = textToSpeech.getEngines();
String defaultEngine = textToSpeech.getDefaultEngine();
if (textToSpeech != null) {
textToSpeech.speak(playText,
TextToSpeech.QUEUE_ADD, null, null);
@ -64,18 +68,10 @@ public class SystemTTS extends UtteranceProgressListener implements TTS, TextToS
}
}
// public boolean isSpeaking() {
// if (textToSpeech.isSpeaking()) {
// return true;
// }
// return false;
// }
//播报完成回调
@Override
public void onUtteranceCompleted(String utteranceId) {
}
@Override
@ -91,5 +87,6 @@ public class SystemTTS extends UtteranceProgressListener implements TTS, TextToS
public void onError(String utteranceId) {
}
}

View File

@ -2,6 +2,6 @@ package com.navinfo.outdoor.util;
public interface TTS {
void playText(String playText);
void stopSpeak();
void stopSpeak();
}

View File

@ -176,4 +176,5 @@
android:layout_marginBottom="20dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -255,7 +255,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:text="免责声明"
android:text="用户协议"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="@id/iv_register_check"
app:layout_constraintLeft_toRightOf="@id/iv_register_check"