almost 4 years ago


維基百科-翻譯蒟蒻

語音辨識 Google 翻譯 文字語音
查詢語音辨識是否安裝 呼叫 Google Translate Api 初始化文字轉語音引擎
啟動語音辨識 使用文字轉語音引擎
接收辨識結果

1. 加入使用者權限請求

AndroidManifest.xml 使用者權限請求
<uses-permission android:name="android.permission.INTERNET" />  

2. 自行撰寫一個按鈕及 onclick() 事件

點擊事件
private OnClickListener speak = new OnClickListener() {
    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak)
            startVoiceRecognitionActivity();
    }
};

3. 語音辨識

3.1. 查詢語音辨識是否安裝

private List<ResolveInfo> activities;
// 以下寫在 onCreate() 內
PackageManager pm = getPackageManager();
activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if(activities.size() != 0){ // 判斷 activities.size() 不為 0 則該手持裝置有語音辨識功能
    button01.setOnClickListener(speak);
}

3.2. 啟動語音辨識

啟動語音辨識
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
// 以下程式碼讓按鈕點擊事件監聽器的 onclick() 呼叫
private void startVoiceRecognitionActivity() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

3.3. 接收辨識結果

接收辨識結果
private ArrayList<String> matches;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        // 辨識結果是一個 List 包含 Google 語音辨識認為可能的結果
        matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    }
    super.onActivityResult(requestCode, resultCode, data);
}

4. Google 翻譯

4.1. 呼叫 Google Translate Api

Translate Api
public String translate(String sl, String tl, String text) throws IOException {
    URL url = new URL("http://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=" + sl + "&tl=" + tl + "&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=" + URLEncoder.encode(text, "UTF-8"));
    URLConnection urlConnection = url.openConnection();
    urlConnection.setRequestProperty("User-Agent", "Something Else");
    BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    String result = br.readLine();
    br.close();
    result = result.substring(2, result.indexOf("]]") + 1);
    StringBuilder sb = new StringBuilder();
    String[] splits = result.split("(?<!\\\\)\"");
    for (int i = 1; i < splits.length; i += 8)
        sb.append(splits[i]);
    return sb.toString().replace("\\n", "\n").replaceAll("\\\\(.)", "$1");
}

// 在辨識結果後撰寫以下程式碼, 進行翻譯
String result = matches.get(0);
String trans = translate("zh-cn", "en", result);

以下因 Google Translate Api 因為需要收費, 暫不使用
Download com.google.api.translate jar
App.java ~~Google Translate Api
Translator.setHttpReferrer("http://gaduo-chien-blog.logdown.com/");
Language original = Language.CHINESE_TRADITIONAL; // 原文語言
Language target = Language.ENGLISH; // 譯文語言
String ori ; // 語音辨識結果
String trans = Translator.execute(ori, original, target);

5. Text To Speech

5.1. 初始化文字轉語音引擎

TTS Text To Speech 初始化
private TextToSpeech tts;
// 以下在 onCreate() 之內;
tts = new TextToSpeech(this,ttsInitListener);
// 
private OnInitListener ttsInitListener = new OnInitListener(){
    public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) { 
    }else{
        //啟動文字轉語音引擎失敗
    }
    }       
};

5.2. 使用文字轉語音引擎

Text To Speech 使用
// trans 為譯文, 經過 Google Translate Api 翻譯的結果
// 以下程式碼撰寫於翻譯結果之後
tts.speak(trans, TextToSpeech.QUEUE_FLUSH, null);

6. 釋放資源

釋放資源
protected void onDestroy(){
    super.onDestroy();
    tts.shutdown(); 
}

(待釋出原始碼)

7. 參考文獻

如何讓Android說話?
GOOGLE TRANSLATE (or similar API) for Android

← Android - 開發環境安裝 Apache Maven - 建構 Dependency 關聯圖 →
 
comments powered by Disqus