增加语音部分业务
This commit is contained in:
161
app/src/main/java/com/navinfo/omqs/util/SoundMeter.java
Normal file
161
app/src/main/java/com/navinfo/omqs/util/SoundMeter.java
Normal file
@@ -0,0 +1,161 @@
|
||||
package com.navinfo.omqs.util;
|
||||
|
||||
import android.media.MediaRecorder;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 录音接口
|
||||
*/
|
||||
public class SoundMeter {
|
||||
static final private double EMA_FILTER = 0.6;
|
||||
|
||||
private static final String TAG = "SoundMeter";
|
||||
private String mFilePath;
|
||||
private MediaRecorder mRecorder = null;
|
||||
private double mEMA = 0.0;
|
||||
//监听
|
||||
private OnSoundMeterListener mListener;
|
||||
//是否开启了语音录制
|
||||
private boolean isStartSound;
|
||||
/**
|
||||
* 开始录音
|
||||
*
|
||||
* @param name 录音文件保存路径
|
||||
*/
|
||||
public void start(final String name) {
|
||||
mFilePath = name;
|
||||
isStartSound = false;
|
||||
//执行录音操作
|
||||
if (!Environment.getExternalStorageState().equals(
|
||||
Environment.MEDIA_MOUNTED) || TextUtils.isEmpty(name)) {
|
||||
if(mListener!=null)
|
||||
mListener.onfaild("权限失败或者文件名称错误");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mRecorder == null) {
|
||||
mRecorder = new MediaRecorder();
|
||||
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
|
||||
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
|
||||
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
|
||||
mRecorder.setOutputFile(name);
|
||||
Log.w(TAG, "录音" + name);
|
||||
|
||||
try {
|
||||
mRecorder.prepare();
|
||||
mRecorder.start();
|
||||
mEMA = 0.0;
|
||||
isStartSound = true;
|
||||
} catch (IllegalStateException e) {
|
||||
if(mListener!=null)
|
||||
mListener.onfaild(e.getMessage());
|
||||
if (mRecorder != null)
|
||||
mRecorder.release();
|
||||
//启动异常释放资源
|
||||
isStartSound = false;
|
||||
mRecorder = null;
|
||||
System.out.print(e.getMessage());
|
||||
} catch (IOException e) {
|
||||
System.out.print(e.getMessage());
|
||||
if(mListener!=null)
|
||||
mListener.onfaild(e.getMessage());
|
||||
//启动异常释放资源
|
||||
isStartSound = false;
|
||||
if (mRecorder != null)
|
||||
mRecorder.release();
|
||||
mRecorder = null;
|
||||
}finally {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 结束录音接,释放录音对象
|
||||
*/
|
||||
public void stop() {
|
||||
isStartSound = false;
|
||||
try {
|
||||
if (mRecorder != null) {
|
||||
mRecorder.stop();
|
||||
}
|
||||
if(new File(mFilePath).exists()){
|
||||
if(mListener!=null)
|
||||
mListener.onSuccess(mFilePath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if(mListener!=null)
|
||||
mListener.onfaild(e.getMessage());
|
||||
} finally {
|
||||
if (mRecorder != null)
|
||||
mRecorder.release();
|
||||
mRecorder = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止录音
|
||||
*/
|
||||
public void pause() {
|
||||
if (mRecorder != null) {
|
||||
mRecorder.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始录音
|
||||
*/
|
||||
public void start() {
|
||||
if (mRecorder != null) {
|
||||
mRecorder.start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取录音基准值
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public double getAmplitude() {
|
||||
if (mRecorder != null)
|
||||
return (mRecorder.getMaxAmplitude() / 2700.0);
|
||||
else
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取EMA基准值
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public double getAmplitudeEMA() {
|
||||
double amp = getAmplitude();
|
||||
mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
|
||||
return mEMA;
|
||||
}
|
||||
|
||||
public OnSoundMeterListener getmListener() {
|
||||
return mListener;
|
||||
}
|
||||
|
||||
public void setmListener(OnSoundMeterListener mListener) {
|
||||
this.mListener = mListener;
|
||||
}
|
||||
|
||||
//是否开启了语音录制
|
||||
public boolean isStartSound(){
|
||||
return isStartSound;
|
||||
}
|
||||
|
||||
//录音监听
|
||||
public interface OnSoundMeterListener{
|
||||
public void onSuccess(String filePath);
|
||||
public void onfaild(String message);
|
||||
}
|
||||
|
||||
}
|
||||
129
app/src/main/java/com/navinfo/omqs/util/SoundRecordeUtils.java
Normal file
129
app/src/main/java/com/navinfo/omqs/util/SoundRecordeUtils.java
Normal file
@@ -0,0 +1,129 @@
|
||||
package com.navinfo.omqs.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.drawable.AnimationDrawable;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.PopupWindow;
|
||||
import android.widget.Toast;
|
||||
import com.blankj.utilcode.util.ToastUtils;
|
||||
import com.navinfo.omqs.Constant;
|
||||
import com.navinfo.omqs.R;
|
||||
import java.io.File;
|
||||
|
||||
public class SoundRecordeUtils {
|
||||
private static SoundRecordeUtils instance;
|
||||
private SoundMeter mSensor; // 系统录音组件
|
||||
private PopupWindow pop;
|
||||
private ImageView volume;
|
||||
private SpeakMode mSpeakMode;
|
||||
private Activity mActivity;
|
||||
|
||||
public static SoundRecordeUtils getInstance(Activity context) {
|
||||
if (instance == null) {
|
||||
instance = new SoundRecordeUtils(context);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public SoundRecordeUtils(Activity mContext) {
|
||||
this.mActivity = mContext;
|
||||
mSpeakMode = new SpeakMode(mContext);
|
||||
initVoicePop();
|
||||
}
|
||||
|
||||
private void initVoicePop() {
|
||||
//语音识别动画
|
||||
pop = new PopupWindow();
|
||||
pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
|
||||
pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
pop.setBackgroundDrawable(new BitmapDrawable());
|
||||
View view = View.inflate(mActivity, R.layout.cv_card_voice_rcd_hint_window, null);
|
||||
pop.setContentView(view);
|
||||
|
||||
pop.update();
|
||||
volume = (ImageView) view.findViewById(R.id.volume);
|
||||
}
|
||||
|
||||
//启动录音
|
||||
public String startSoundMeter(View v, SoundRecordeCallback soundRecordeCallback, boolean isRemind/*是否需要默认语音提醒开始和结束录音*/) {
|
||||
//录音动画
|
||||
if (pop != null)
|
||||
pop.showAtLocation(v, Gravity.CENTER, 0, 0);
|
||||
volume.setBackgroundResource(R.drawable.pop_voice_img);
|
||||
AnimationDrawable animation = (AnimationDrawable) volume.getBackground();
|
||||
animation.start();
|
||||
|
||||
final String name = DateTimeUtil.getTimeSSS() + ".m4a";
|
||||
if (mSensor == null) {
|
||||
mSensor = new SoundMeter();
|
||||
}
|
||||
mSensor.setmListener(new SoundMeter.OnSoundMeterListener() {
|
||||
@Override
|
||||
public void onSuccess(String filePath) {
|
||||
if (isRemind) {
|
||||
mSpeakMode.speakText("结束录音");
|
||||
}
|
||||
if (soundRecordeCallback!=null) {
|
||||
soundRecordeCallback.onSuccess(filePath, name);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onfaild(String message) {
|
||||
if (isRemind) {
|
||||
ToastUtils.showLong("录制失败!");
|
||||
mSpeakMode.speakText("录制失败");
|
||||
}
|
||||
if (soundRecordeCallback!=null) {
|
||||
soundRecordeCallback.onfaild(message);
|
||||
}
|
||||
}
|
||||
});
|
||||
//增加下目录创建,防止由于目录导致无法录制文件
|
||||
if (!new File(Constant.USER_DATA_ATTACHEMNT_PATH).exists()) {
|
||||
new File(Constant.USER_DATA_ATTACHEMNT_PATH).mkdirs();
|
||||
}
|
||||
if (mSensor.isStartSound()) {
|
||||
ToastUtils.showLong("已自动结束上一段录音");
|
||||
mSpeakMode.speakText("已自动结束上一段录音");
|
||||
return null;
|
||||
}
|
||||
//启动定时器
|
||||
mSensor.start(Constant.USER_DATA_ATTACHEMNT_PATH + name);
|
||||
if (isRemind) {
|
||||
ToastUtils.showLong("开始录音");
|
||||
mSpeakMode.speakText("开始录音");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
//判断是否启动了录音
|
||||
public boolean isStartSound(){
|
||||
if(mSensor!=null){
|
||||
return mSensor.isStartSound();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//停止语音录制
|
||||
public void stopSoundMeter() {
|
||||
//先重置标识,防止按钮抬起时触发语音结束
|
||||
|
||||
if (mSensor != null && mSensor.isStartSound()) {
|
||||
mSensor.stop();
|
||||
}
|
||||
|
||||
if (pop != null && pop.isShowing())
|
||||
pop.dismiss();
|
||||
}
|
||||
|
||||
public interface SoundRecordeCallback{
|
||||
public void onSuccess(String filePath, String fileName);
|
||||
public void onfaild(String message);
|
||||
}
|
||||
}
|
||||
149
app/src/main/java/com/navinfo/omqs/util/SpeakMode.java
Normal file
149
app/src/main/java/com/navinfo/omqs/util/SpeakMode.java
Normal file
@@ -0,0 +1,149 @@
|
||||
package com.navinfo.omqs.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 com.navinfo.omqs.ui.dialog.FirstDialog;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
|
||||
//语音类
|
||||
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()) {
|
||||
FirstDialog firstDialog = new FirstDialog(mActivity);
|
||||
firstDialog.setTitle("提示");
|
||||
firstDialog.setMessage("设备不支持语音播报,请先下载语音助手。");
|
||||
firstDialog.setConfirmListener(new FirstDialog.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(Dialog dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
firstDialog.setNegativeView(View.GONE);
|
||||
firstDialog.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.i("TextToSpeechDemo", String.valueOf(status));
|
||||
}
|
||||
|
||||
//读语音处理
|
||||
public void speakText(final String message) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
if (mTextToSpeech != null) {
|
||||
|
||||
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 {
|
||||
//增加播报停止,解决不能播报最新内容问题
|
||||
mTextToSpeech.stop();
|
||||
|
||||
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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user