訊飛語音整合(語音轉文字,文字轉語音)
語音聽寫SDK適配安卓6.0需要手動申請許可權
關於語音聽寫SDK的開發,參考科大訊飛開放平臺官網為準
在百度搜索訊飛科技開放平臺,自己註冊賬號 自己建立一個新應用
在控制檯介面建立完應用,然後選擇SDK下載
選擇需要的SDK進行下載(這裡我們選擇的是組合下載前兩個)
解壓下載包,在libs中對應匯入Android Studio中 jar包需要Add As Library操作 jnilibs資料夾自建
語音識別返回的是JSON資料需要新增Gson依賴
複製assets資料夾到專案中
專案結構 —— 下面只有Demo資料夾中的類和佈局,忽略MainActivity
新增許可權
<!--連線網路許可權,用於執行雲端語音能力 -->
<uses-permission android:name="android.permission.INTERNET" />
<!--獲取手機錄音機使用許可權,聽寫、識別、語義理解需要用到此許可權 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!--讀取網路資訊狀態 -->
<uses-permission android:name ="android.permission.ACCESS_NETWORK_STATE" />
<!--獲取當前wifi狀態 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允許程式改變網路連線狀態 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<!--讀取手機資訊許可權 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--讀取聯絡人許可權,上傳聯絡人需要用到此許可權 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!--外儲存寫許可權,構建語法需要用到此許可權 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--外儲存讀許可權,構建語法需要用到此許可權 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--配置許可權,用來記錄應用配置資訊 -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!--手機定位資訊,用來為語義等功能提供定位,提供更精準的服務--> <!--定位資訊是敏感資訊,可通過Setting.setLocationEnable(false)關閉定位請求 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
以下程式碼根據專案結構複製
Main2Activity介面
package com.example.xunfei.Demo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.xunfei.R;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
private EditText et_input;
private TextView text2;
private Button btn_startspeech;
private Button btn_startspeektext;
// 用HashMap儲存聽寫結果
private HashMap<String, String> mIatResults = new LinkedHashMap<String, String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//載入佈局
setContentView(R.layout.activity_main2);
//// 將“xxxxxxx”替換成您申請的 APPID = 不要刪除
SpeechUtility.createUtility(this, SpeechConstant.APPID + "=xxxxxxx");//初始化SDK
initView();
}
private void speekText() {
//1. 建立 SpeechSynthesizer 物件 , 第二個引數: 本地合成時傳 InitListener
SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, null);
//2.合成引數設定,詳見《 MSC Reference Manual》 SpeechSynthesizer 類
//設定發音人(更多線上發音人,使用者可參見 附錄 13.2
mTts.setParameter(SpeechConstant.VOICE_NAME, "vixyun"); // 設定發音人
mTts.setParameter(SpeechConstant.SPEED, "50");// 設定語速
mTts.setParameter(SpeechConstant.VOLUME, "100");// 設定音量,範圍 0~100
mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //設定雲端
//設定合成音訊儲存位置(可自定義儲存位置),儲存在 “./sdcard/iflytek.pcm”
//儲存在 SD 卡需要在 AndroidManifest.xml 新增寫 SD 卡許可權
//僅支援儲存為 pcm 和 wav 格式, 如果不需要儲存合成音訊,註釋該行程式碼
mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");
//3.開始合成
mTts.startSpeaking(et_input.getText().toString(), new MySynthesizerListener());
}
class MySynthesizerListener implements SynthesizerListener {
@Override
public void onSpeakBegin() {
showTip(" 開始播放 ");
}
@Override
public void onSpeakPaused() {
showTip(" 暫停播放 ");
}
@Override
public void onSpeakResumed() {
showTip(" 繼續播放 ");
}
@Override
public void onBufferProgress(int percent, int beginPos, int endPos,
String info) {
// 合成進度
}
@Override
public void onSpeakProgress(int percent, int beginPos, int endPos) {
// 播放進度
}
@Override
public void onCompleted(SpeechError error) {
if (error == null) {
showTip("播放完成 ");
} else if (error != null) {
showTip(error.getPlainDescription(true));
}
}
@Override
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
// 以下程式碼用於獲取與雲端的會話 id,當業務出錯時將會話 id提供給技術支援人員,可用於查詢會話日誌,定位出錯原因
// 若使用本地能力,會話 id為null
//if (SpeechEvent.EVENT_SESSION_ID == eventType) {
// String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
// Log.d(TAG, "session id =" + sid);
//}
}
}
private void startSpeechDialog() {
//1. 建立RecognizerDialog物件
RecognizerDialog mDialog = new RecognizerDialog(this, new MyInitListener());
//2. 設定accent、 language等引數
mDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");// 設定中文
mDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
// 若要將UI控制元件用於語義理解,必須新增以下引數設定,設定之後 onResult回撥返回將是語義理解
// 結果
// mDialog.setParameter("asr_sch", "1");
// mDialog.setParameter("nlp_version", "2.0");
//3.設定回撥介面
mDialog.setListener(new MyRecognizerDialogListener());
//4. 顯示dialog,接收語音輸入
mDialog.show();
}
class MyRecognizerDialogListener implements RecognizerDialogListener {
/**
* @param results
* @param isLast 是否說完了
*/
@Override
public void onResult(RecognizerResult results, boolean isLast) {
String result = results.getResultString(); //為解析的
showTip(result);
System.out.println(" 沒有解析的 :" + result);
String text = JsonParser.parseIatResult(result);//解析過後的
System.out.println(" 解析後的 :" + text);
String sn = null;
// 讀取json結果中的 sn欄位
try {
JSONObject resultJson = new JSONObject(results.getResultString());
sn = resultJson.optString("sn");
} catch (JSONException e) {
e.printStackTrace();
}
mIatResults.put(sn, text);//沒有得到一句,新增到
StringBuffer resultBuffer = new StringBuffer();
for (String key : mIatResults.keySet()) {
resultBuffer.append(mIatResults.get(key));
}
et_input.setText(resultBuffer.toString());// 設定輸入框的文字
et_input.setSelection(et_input.length());//把游標定位末尾
}
@Override
public void onError(SpeechError speechError) {
}
}
class MyInitListener implements InitListener {
@Override
public void onInit(int code) {
if (code != ErrorCode.SUCCESS) {
showTip("初始化失敗 ");
}
}
}
/**
* 語音識別
*/
private void startSpeech() {
//1. 建立SpeechRecognizer物件,第二個引數: 本地識別時傳 InitListener
SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(this, null); //語音識別器
//2. 設定聽寫引數,詳見《 MSC Reference Manual》 SpeechConstant類
mIat.setParameter(SpeechConstant.DOMAIN, "iat");// 簡訊和日常用語: iat (預設)
mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");// 設定中文
mIat.setParameter(SpeechConstant.ACCENT, "mandarin");// 設定普通話
//3. 開始聽寫
mIat.startListening(mRecoListener);
}
// 聽寫監聽器
private RecognizerListener mRecoListener = new RecognizerListener() {
// 聽寫結果回撥介面 (返回Json 格式結果,使用者可參見附錄 13.1);
//一般情況下會通過onResults介面多次返回結果,完整的識別內容是多次結果的累加;
//關於解析Json的程式碼可參見 Demo中JsonParser 類;
//isLast等於true 時會話結束。
public void onResult(RecognizerResult results, boolean isLast) {
Log.e("yyyy", results.getResultString());
System.out.println(results.getResultString());
showTip(results.getResultString());
}
// 會話發生錯誤回撥介面
public void onError(SpeechError error) {
showTip(error.getPlainDescription(true));
// 獲取錯誤碼描述
Log.e("yyyyy", "error.getPlainDescription(true)==" + error.getPlainDescription(true));
}
// 開始錄音
public void onBeginOfSpeech() {
showTip(" 開始錄音 ");
}
//volume 音量值0~30, data音訊資料
public void onVolumeChanged(int volume, byte[] data) {
showTip(" 聲音改變了 ");
}
// 結束錄音
public void onEndOfSpeech() {
showTip(" 結束錄音 ");
}
// 擴充套件用介面
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
}
};
private void showTip(String data) {
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
}
private void initView() {
et_input = (EditText) findViewById(R.id.et_input);
text2 = (TextView) findViewById(R.id.text2);
btn_startspeech = (Button) findViewById(R.id.btn_startspeech);
btn_startspeektext = (Button) findViewById(R.id.btn_startspeektext);
btn_startspeech.setOnClickListener(this);
btn_startspeektext.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_startspeech:
//語音識別(把聲音轉文字)
startSpeechDialog();
break;
case R.id.btn_startspeektext:
// 語音合成(把文字轉聲音)
speekText();
break;
}
}
private void submit() {
// validate
String input = et_input.getText().toString().trim();
if (TextUtils.isEmpty(input)) {
Toast.makeText(this, "請輸入文字資訊 ...", Toast.LENGTH_SHORT).show();
return;
}
}
}
JsonParser類
package com.example.xunfei.Demo;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
/**
* Created by 墨鴉 on 2017/12/27.
*/
public class JsonParser {
public static String parseIatResult(String json) {
StringBuffer ret = new StringBuffer() ;
try {
JSONTokener tokener = new JSONTokener(json) ;
JSONObject joResult = new JSONObject(tokener) ;
JSONArray words = joResult.getJSONArray("ws" );
for (int i = 0; i < words.length(); i++) {
// 轉寫結果詞,預設使用第一個結果
JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
JSONObject obj = items.getJSONObject(0 );
ret.append(obj.getString("w" ));
}
} catch (Exception e) {
e.printStackTrace();
}
return ret.toString();
}
public static String parseGrammarResult(String json) {
StringBuffer ret = new StringBuffer() ;
try {
JSONTokener tokener = new JSONTokener(json) ;
JSONObject joResult = new JSONObject(tokener) ;
JSONArray words = joResult.getJSONArray("ws" );
for (int i = 0; i < words.length(); i++) {
JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
for (int j = 0; j < items.length() ; j++)
{
JSONObject obj = items.getJSONObject(j);
if (obj.getString("w").contains( "nomatch"))
{
ret.append( "沒有匹配結果.") ;
return ret.toString();
}
ret.append( "【結果】" + obj.getString("w" ));
ret.append("【置信度】 " + obj.getInt("sc" ));
ret.append("\n ");
}
}
} catch (Exception e) {
e.printStackTrace();
ret.append(" 沒有匹配結果 .");
}
return ret.toString();
}
public static String parseLocalGrammarResult(String json) {
StringBuffer ret = new StringBuffer() ;
try {
JSONTokener tokener = new JSONTokener(json) ;
JSONObject joResult = new JSONObject(tokener) ;
JSONArray words = joResult.getJSONArray("ws" );
for (int i = 0; i < words.length(); i++) {
JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
for (int j = 0; j < items.length() ; j++)
{
JSONObject obj = items.getJSONObject(j);
if (obj.getString("w").contains( "nomatch"))
{
ret.append( "沒有匹配結果.") ;
return ret.toString();
}
ret.append( "【結果】" + obj.getString("w" ));
ret.append("\n ");
}
}
ret.append("【置信度】 " + joResult.optInt("sc" ));
} catch (Exception e) {
e.printStackTrace();
ret.append(" 沒有匹配結果 .");
}
return ret.toString();
}
}
佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Demo.Main2Activity">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:hint="請輸入文字資訊 ..."
android:textSize="20sp"/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="這是文字"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_startspeech"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="點選按鈕語音輸入 "
android:textSize="20sp" />
<Button
android:id="@+id/btn_startspeektext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="語音合成(把文字轉聲音) "
android:textSize="20sp" />
</LinearLayout>
相關推薦
訊飛語音整合(語音轉文字,文字轉語音)
語音聽寫SDK適配安卓6.0需要手動申請許可權 關於語音聽寫SDK的開發,參考科大訊飛開放平臺官網為準 在百度搜索訊飛科技開放平臺,自己註冊賬號 自己建立一個新應用 在控制檯介面建立完應用,然後選擇SDK下載 選擇需要的SDK進行下載(這裡我們選
unity3d:百度語音線上語音轉文字,文字轉語音,跨平臺
轉自洪流學堂 語音轉文字 1.開啟麥克風記錄 _clipRecord = Microphone.Start(null, false, 30, 16000); 2.將Unity的AudioClip資料轉化為PCM格式16bit資料 /// <s
訊飛 SDK整合記錄
1 下載SDK 2 copy資源,lib 1.使用demo測試時,需將res中資源拷貝到demo中assets相應的路徑下; 2.使用帶UI介面時,請將assets下檔案拷貝到專案中; 3 init Call.nbf initListener 4
實現word轉pdf,HTML轉pdf(探索篇)
ner ase node eth ack line prope fin -o 筆者找依賴的jar包,找的好辛苦。 ITextRenderer、ITextFontResolver這兩個類依賴的jar包到底是哪個,還有怎麽下載?苦苦糾結了3個小時。終於找到你了!記錄個網址:ht
Fastjson 序列化,反序列化Map物件排序問題(字串轉map,map轉字串)
背景 記錄專案中遇到的 關於fastjson jsonobject轉string亂序,string轉jsonObject亂序問題的解決方案 fastJson issues 問題來源描述參見: https://github.com/alibaba/fastjson/issues/359
常用格式轉換整理(byte轉string, int轉string....)
1、String和int轉換 (1)Sting 轉為int String str = "123"; try { int a = Integer.parseInt(str); } catch (NumberFormatException e) { e.printStackT
二進位制轉十進位制,十進位制轉二進位制(百度經驗)
轉成二進位制主要有以下幾種:正整數轉二進位制,負整數轉二進位制,小數轉二進位制; 1、 正整數轉成二進位制。要點一定一定要記住哈:除二取餘,然後倒序排列,高位補零。 也就是說,將正的十進位制數除以二,得到的商再除
FFmpeg命令:幾種常見場景下的FFmpeg命令(攝像頭採集推流,桌面錄製推流、轉流,拉流等等)
前提: 再者,推流你得有個流媒體服務,個人測試用小水管:rtmp://eguid.cc:1935/rtmp/test(小水管,請儘量錯開時間使用,另切記推流視訊位元速率不要太高,避免佔用太多頻寬)
整合ssm(spring3.2.0,mybatis3.2.3)時報“ERROR ContextLoader:307 - Context initialization failed”錯誤
在進行SSM(jdk1.7, eclipse Mars.2)整合時,老是出現: ERROR ContextLoader:307 - Context initialization failed java.lang.IllegalArgumentException.... 在百度查了一天還是
圖片如何轉換成可編輯的文字,圖片轉文字的教程
你是否經常遇到要將紙質文件錄入到文件裡情況,工作中需要將圖片上的資料電子化?難道面對圖片上的文字,我們要一個個的敲?應該很多人都有上述的煩惱。那麼該如何快速把圖片轉換成文字呢?這裡小編給大家總結了兩種圖片轉文字的方法。 方法一、使用線上網站(電腦端) 具體操作步
awk字串操作(字串連結、傳入傳出shell變數) awk 字串連線操作(字串轉數字,數字轉字串) awk當中使用外部變數 awk中使用shell的環境變數 awk如何向shell傳值
1.awk基礎 awk的環境變數及其意義 https://blog.csdn.net/snowpay/article/details/52451718 linux awk命令詳解 https://www.cnblogs.com/xudong-bupt/p/3721210.html 2.aw
懸浮球(多機型懸浮窗許可權設定,狀態列適配,可自動或手動設定大小,點選跳轉WebView,拖拽處理)
懸浮球:多機型懸浮窗許可權設定,狀態列適配,可自動或手動設定大小,點選跳轉WebView,拖拽處理, 應用內和應用外都可以顯示(可設定取消)可做SDK 和依賴類,橫豎屏切換處理 專案地址: GitHub:https://github.com/gitUserBoy/flow_balls.
eclipse 整合maven及maven的使用(入門級教程,高手略過)
eclipse整合maven及maven使用教程 網上看了很多eclipse 和maven整合的教程,對於懂maven的人來說一眼都能看懂,但對於新手來說還是有困難,說以些了這篇教程分享一下(我也是新
實現登入頁面登入成功後跳轉過程(個人理解學習,程式碼不完整)
function login() { var userName = document.getElementById("loginName").value;//通過ID獲取元素 var pwd = document.getElementById
Ubuntu16.04下手動配置pixhawk飛控--PX4韌體版本開發環境(ROS+mavros版本,超級完整!)
參考px4官網流程: 配置過程中網速一定要好!!!!一定要好!!!!最好翻牆!!!不然會出很多莫名其妙的bug的!!!!! --------------------------------------------------------------------
inupt textarea提示文字(點選消失,不輸入恢復)
原始碼: <input name="textfield" type="text" maxlength="20" value="請輸入文字.." onfocus="if (value =='請輸入文字..'){value =''}" onblur="
SSH整合(struts2.2.1 + spring3.0 + hibernate3.3)
轉載請註明出處:http://blog.csdn.net/cl61917380/article/details/6265620 培訓終於到了最後一個階段了! 使用工具MyEclipse8.6 今天初探Hibernate老師提前說了SSH的整合,雞動人心! 過程: 版
HDU 2648(搜索題,哈希表)
cin ostream pac cstring using mem == ring scan #include<iostream> #include<map> #include<string> #include<cstring&
hdu 4549 M斐波那契數列(矩陣高速冪,高速冪降冪)
else if stdlib.h article 1.0 ostream void 我們 memset font http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b
用什麽軟件進行CAD轉PDF,CAD轉PDF批量怎麽轉
對於設計行業的小夥伴來說,CAD圖紙是我們每天都需要接觸到的文件格式,但是對於其他行業的人來說CAD文件還是接觸比較少的,因此為了更加方便查看,我們會選擇將CAD文件轉換成PDF文件,那麽CAD轉換PDF怎麽多文件同時轉換?通過這篇文章,就來告訴大家如何將多個CAD文件轉換成PDF文件? 一、CAD轉換