Android實(shí)現(xiàn)語(yǔ)音合成與識(shí)別功能
Android語(yǔ)音合成與語(yǔ)音識(shí)別,供大家參考,具體內(nèi)容如下
這里調(diào)用科大訊飛語(yǔ)音的API,語(yǔ)音云開(kāi)放平臺(tái)介紹
調(diào)用科大訊飛語(yǔ)音的API,需要加添庫(kù)文件Msc.jar,添加libmsc.so文件,還需添加權(quán)限,具體步驟可參看SDK里的文檔
參看開(kāi)發(fā)的文檔寫了一個(gè)簡(jiǎn)單的語(yǔ)音合成和識(shí)別demo,圖示如下

在EditText里輸入文字,點(diǎn)擊語(yǔ)音合成,可以實(shí)現(xiàn)文字轉(zhuǎn)化為語(yǔ)音

點(diǎn)擊語(yǔ)音合成,輸入語(yǔ)音,識(shí)別的文字以提示的形式顯示,并且顯示在EditText中

主要代碼如下,注意appid需要自己申請(qǐng)
package com.example.voice;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.iflytek.cloud.speech.*;
public class VoiceActivity extends Activity {
private static final String APPID = "appid=52cddb99";
private EditText et = null;
private Button btn1 = null;
private Button btn2 = null;
String text = "";
String temp="";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);
et = (EditText) findViewById(R.id.et);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(mylistener);
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(mylistener);
}
private OnClickListener mylistener = new OnClickListener() {
public void onClick(View v) {
SpeechUser.getUser().login(VoiceActivity.this, null, null, APPID,
loginListener);
Button btn = (Button) v;
switch (btn.getId()) {
case R.id.btn1:
SpeechSynthesizer mSpeechSynthesizer = SpeechSynthesizer
.createSynthesizer(VoiceActivity.this);
mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME,
"xiaoyu");
mSpeechSynthesizer.setParameter(SpeechConstant.SPEED, "50");
mSpeechSynthesizer.startSpeaking(et.getText().toString(),
mSynListener);
break;
case R.id.btn2:
text = "";
temp="";
SpeechRecognizer recognizer = SpeechRecognizer
.createRecognizer(VoiceActivity.this);
recognizer.setParameter(SpeechConstant.DOMAIN, "iat");
recognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
recognizer.setParameter(SpeechConstant.ACCENT, "accent");
recognizer.startListening(mRecoListener);
break;
}
}
};
private SynthesizerListener mSynListener = new SynthesizerListener() {
public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
}
public void onCompleted(SpeechError arg0) {
}
public void onSpeakBegin() {
}
public void onSpeakPaused() {
}
public void onSpeakProgress(int arg0, int arg1, int arg2) {
}
public void onSpeakResumed() {
}
};
private RecognizerListener mRecoListener = new RecognizerListener() {
public void onBeginOfSpeech() {
}
public void onEndOfSpeech() {
}
public void onError(SpeechError error) {
}
public void onEvent(int arg0, int arg1, int arg2, String arg3) {
}
public void onVolumeChanged(int arg0) {
}
public void onResult(RecognizerResult results, boolean isLast) {
//將解析后的字符串連在一起
temp=results.getResultString();
JsonParser json = new JsonParser();
text+=json.parseIatResult(temp);
if(isLast==true)
{
et.setText(text, null);
Toast.makeText(VoiceActivity.this,text,Toast.LENGTH_LONG).show();
}
}
};
private SpeechListener loginListener = new SpeechListener() {
public void onCompleted(SpeechError arg0) {
}
public void onData(byte[] arg0) {
}
public void onEvent(int arg0, Bundle arg1) {
}
};
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.voice, menu);
return true;
}
}
布局文件
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".VoiceActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="VoiceApplication" /> <EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="top" android:layout_weight="0.32" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.03" android:text="語(yǔ)音合成" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.03" android:text="語(yǔ)音識(shí)別" /> </TableLayout>
解析Json格式的數(shù)據(jù)是參照訊飛的文檔中的
package com.example.voice;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.text.TextUtils;
//import com.iflytek.speech.ErrorCode;
//import com.iflytek.speech.SpeechError;
/**
* 對(duì)云端返回的Json結(jié)果進(jìn)行解析
*
* @author iFlytek
* @since 20131211
*/
public class JsonParser {
/**
* 聽(tīng)寫結(jié)果的Json格式解析
*
* @param json
* @return
*/
public static String parseIatResult(String json) {
if (TextUtils.isEmpty(json))
return "";
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++) {
// 聽(tīng)寫結(jié)果詞,默認(rèn)使用第一個(gè)結(jié)果
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
JSONObject obj = items.getJSONObject(0);
ret.append(obj.getString("w"));
// 如果需要多候選結(jié)果,解析數(shù)組其他字段
// for(int j = 0; j < items.length(); j++)
// {
// JSONObject obj = items.getJSONObject(j);
// ret.append(obj.getString("w"));
// }
}
} catch (Exception e) {
e.printStackTrace();
}
return ret.toString();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android kotlin RecyclerView遍歷json實(shí)現(xiàn)列表數(shù)據(jù)的案例
這篇文章主要介紹了Android kotlin RecyclerView遍歷json實(shí)現(xiàn)列表數(shù)據(jù)的案例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-08-08
Android自定義View實(shí)現(xiàn)簡(jiǎn)單的圓形Progress效果
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)簡(jiǎn)單的圓形Progress效果的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android開(kāi)發(fā)之判斷有無(wú)虛擬按鍵(導(dǎo)航欄)的實(shí)例
下面小編就為大家分享一篇Android開(kāi)發(fā)之判斷有無(wú)虛擬按鍵(導(dǎo)航欄)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android開(kāi)發(fā)筆記之:如何安全中止一個(gè)自定義線程Thread的方法
本篇文章是對(duì)Android中如何安全中止一個(gè)自定義線程Thread的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android實(shí)現(xiàn)圖片添加陰影效果的2種方法
這篇文章主要介紹了Android實(shí)現(xiàn)圖片添加陰影效果的2種方法,第一種方法是自定義drawable,第二種方式就是自定義view,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android6.0藍(lán)牙出現(xiàn)無(wú)法掃描設(shè)備或閃退問(wèn)題解決辦法
這篇文章主要介紹了Android6.0藍(lán)牙出現(xiàn)無(wú)法掃描設(shè)備或閃退問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android入門之Style與Theme用法實(shí)例解析
這篇文章主要介紹了Android入門之Style與Theme用法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android 自定義View的構(gòu)造函數(shù)詳細(xì)介紹
這篇文章主要介紹了Android 自定義View的構(gòu)造函數(shù)詳細(xì)介紹的相關(guān)資料,這里對(duì)構(gòu)造函數(shù)進(jìn)行了對(duì)比按需使用,需要的朋友可以參考下2016-12-12
android實(shí)現(xiàn)滑動(dòng)標(biāo)簽頁(yè)效果的代碼解析
這篇文章主要介紹了android實(shí)現(xiàn)滑動(dòng)標(biāo)簽頁(yè)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

