直播带货源码或者说直播带货商城源码,是当下流行的直播+购物平台源码,也是电商重要的流量渠道,在直播中加入语音识别模块,可以方便用户在不方便打字的情况下,轻松使用语音进行评论、私信。下面为大家简单介绍一下云豹直播带货源码中的语音转文字功能如何实现。
如上图所示,在聊天的时候选择语音输入,打开对话框,上面有个红色按钮,按住之后说话,说话的声音会被app识别进而转成文字,显示在对话框上面,点击发送,便可将文字作为消息发送给对方。
实现直播带货源码中语音转文字功能的部分代码如下:
public class ImAsrUtil {
private EventManager mManager;
private EventListener mEventListener;
private String mJsonString;
private AsrCallback mCallback;
public ImAsrUtil(Context context) {
mManager = EventManagerFactory.create(context, "asr");
mEventListener = new EventListener() {
@Override
public void onEvent(String name, String params, byte[] bytes, int offset, int length) {
switch (name) {
case SpeechConstant.CALLBACK_EVENT_ASR_READY://引擎准备就绪,可以开始说话
//L.e(TAG, "引擎准备就绪,可以开始说话------>"+params);
break;
case SpeechConstant.CALLBACK_EVENT_ASR_BEGIN://检测到说话开始
//L.e(TAG, "检测到说话开始------>" + params);
// if(mCallback!=null){
// mCallback.onSpeakStart();
// }
break;
case SpeechConstant.CALLBACK_EVENT_ASR_END://检测到说话结束
//L.e(TAG, "检测到说话结束------>" + params);
// if (mCallback != null) {
// mCallback.onSpeakEnd();
// }
break;
case SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL://识别结果,类型json
JSONObject obj = JSON.parseObject(params);
String result = obj.getJSONArray("results_recognition").getString(0);
//L.e(TAG, "识别结果------>" + result);
if (mCallback != null) {
mCallback.onResult(result);
}
break;
case SpeechConstant.CALLBACK_EVENT_ASR_FINISH://识别结束(可能含有错误信息)
//L.e(TAG, "识别结束------>" + params);
break;
case SpeechConstant.CALLBACK_EVENT_ASR_LONG_SPEECH://长语音额外的回调,表示长语音识别结束
//L.e(TAG, "长语音识别结束------>" + params);
break;
}
}
};
mManager.registerListener(mEventListener);
JSONObject params = new JSONObject();
//DECODER = 0 ,表示禁用离线功能,只使用在线功能;DECODER = 2 ,表示启用离线功能,但是SDK强制在线优先。
params.put(SpeechConstant.DECODER, 0);
params.put(SpeechConstant.PID, 1537);
//开启长语音
params.put(SpeechConstant.VAD_ENDPOINT_TIMEOUT, 0);
//关闭自动断句
params.put(SpeechConstant.VAD, SpeechConstant.VAD_TOUCH);
//在选择PID为长句(输入法模式)的时候,是否禁用标点符号
params.put(SpeechConstant.DISABLE_PUNCTUATION, true);
//是否需要语音音量数据回调,开启后有CALLBACK_EVENT_ASR_VOLUME事件回调
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);
//是否需要语音音频数据回调,开启后有CALLBACK_EVENT_ASR_AUDIO事件
params.put(SpeechConstant.ACCEPT_AUDIO_DATA, false);
mJsonString = params.toJSONString();
}
/**
* 开始录音
*/
public void start() {
if (mManager != null) {
mManager.send(SpeechConstant.ASR_START, mJsonString, null, 0, 0);
}
}
/**
* 停止录音
*/
public void stop() {
if (mManager != null) {
mManager.send(SpeechConstant.ASR_STOP, null, null, 0, 0);
mManager.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
}
}
public void release() {
if (mManager != null) {
mManager.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
mManager.unregisterListener(mEventListener);
}
mManager=null;
mCallback=null;
}
public interface AsrCallback {
//void onSpeakStart();
void onResult(String result);
//void onSpeakEnd();
}
public void setAsrCallback(AsrCallback callback){
mCallback=callback;
}
}
public class ChatVoiceInputDialog extends AbsDialogFragment implements View.OnClickListener {
private View mBtnBg;
private ScaleAnimation mAnimation;
private View mBtnInput;
private View mTip;
private View mBtnClose;
private View mSendGroup;
private TextView mContent;
private ImAsrUtil mImAsrUtil;//语音识别
private String mPleaseSayString;
private ChatRoomViewHolder mChatRoomViewHolder;
@Override
protected int getLayoutId() {
return R.layout.chat_voice_input;
}
@Override
protected int getDialogStyle() {
return R.style.dialog2;
}
@Override
protected boolean canCancel() {
return true;
}
@Override
protected void setWindowAttributes(Window window) {
window.setWindowAnimations(R.style.bottomToTopAnim);
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = DpUtil.dp2px(200);
params.gravity = Gravity.BOTTOM;
window.setAttributes(params);
}
public void setChatRoomViewHolder(ChatRoomViewHolder chatRoomViewHolder) {
mChatRoomViewHolder = chatRoomViewHolder;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mBtnBg = mRootView.findViewById(R.id.btn_bg);
mAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
mAnimation.setDuration(700);
mAnimation.setRepeatCount(-1);
mBtnInput = mRootView.findViewById(R.id.btn_input);
mBtnInput.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
startInput();
break;
case MotionEvent.ACTION_UP:
stopInput();
break;
}
return true;
}
});
mTip = mRootView.findViewById(R.id.tip);
mSendGroup = mRootView.findViewById(R.id.group2);
mContent = mRootView.findViewById(R.id.content);
mBtnClose = mRootView.findViewById(R.id.btn_close);
mBtnClose.setOnClickListener(this);
mRootView.findViewById(R.id.btn_cancel).setOnClickListener(this);
mRootView.findViewById(R.id.btn_send).setOnClickListener(this);
mImAsrUtil = new ImAsrUtil(mContext);
mImAsrUtil.setAsrCallback(new ImAsrUtil.AsrCallback() {
@Override
public void onResult(String result) {
if (!TextUtils.isEmpty(result) && mContent != null) {
mContent.setText(result);
}
}
});
mPleaseSayString = WordUtil.getString(R.string.im_please_say);
}
/**
* 开始输入
*/
private void startInput() {
if (mBtnBg != null && mAnimation != null) {
mBtnBg.startAnimation(mAnimation);
}
mContent.setText(mPleaseSayString);
if (mTip != null && mTip.getVisibility() == View.VISIBLE) {
mTip.setVisibility(View.INVISIBLE);
}
if (mBtnClose != null && mBtnClose.getVisibility() == View.VISIBLE) {
mBtnClose.setVisibility(View.INVISIBLE);
}
if (mImAsrUtil != null) {
mImAsrUtil.start();
}
}
/**
* 结束输入
*/
private void stopInput() {
if (mBtnBg != null) {
mBtnBg.clearAnimation();
}
if (mImAsrUtil != null) {
mImAsrUtil.stop();
}
String content = mContent.getText().toString().trim();
if (mPleaseSayString.equals(content)) {
cancel();
} else {
if (mSendGroup.getVisibility() != View.VISIBLE) {
mSendGroup.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.btn_close) {
dismiss();
} else if (i == R.id.btn_cancel) {
cancel();
} else if (i == R.id.btn_send) {
send();
}
}
private void cancel() {
mContent.setText("");
if (mSendGroup.getVisibility() == View.VISIBLE) {
mSendGroup.setVisibility(View.INVISIBLE);
}
if (mBtnClose.getVisibility() != View.VISIBLE) {
mBtnClose.setVisibility(View.VISIBLE);
}
if (mTip.getVisibility() != View.VISIBLE) {
mTip.setVisibility(View.VISIBLE);
}
}
private void send() {
if (mChatRoomViewHolder != null) {
String content = mContent.getText().toString().trim();
mChatRoomViewHolder.sendText(content);
}
dismiss();
}
@Override
public void onDestroy() {
mChatRoomViewHolder = null;
if (mImAsrUtil != null) {
mImAsrUtil.release();
}
mImAsrUtil = null;
super.onDestroy();
}
}
这样就实现了直播带货源码中的语音识别功能,其实,这一功能并非直播带货源码中独有的,很多软件中都有,因此,一些三方也为其开发出了专门的SDK,至于各位在软件开发中想使用哪种方式,就看自己的想法了。