1. 程式人生 > >TextToSpeech語音解決中文不能用問題

TextToSpeech語音解決中文不能用問題

Android 系統自帶的TTS (Text To Speak)語音庫目前只有English ,German , Italian ,French ,Spanish(英文,德語,義大利語,法語,西班牙語)五個音庫.所以中文發音必須要藉助其他的軟體,或者引擎.近些天研究目前有三個方法可以實現: 

1,一種是藉助shoushuotts音庫引擎和jar檔案在自己的專案中呼叫.具體方法可檢視網址:http://dev.10086.cn/cmdn/bbs/thread-58426-1-1.html 

(經過測試,中文發音很慢,用些多音字和特殊符號無法識別) 

2.是用addr軟體,方法同上,要新增兩個jar檔案.有原始碼例項: 



(測試發現,其中文發音是老外的調調,並且嚴重懷疑他的中文也是老外教的) 

3 安裝訊飛語音外掛.需要安裝兩個檔案,訊飛語音TTS.apk 和訊飛語音包.apk,不需要jar檔案,直接到系統配置 -> 語音輸入與輸出 --> 文字轉語音設定 -->訊飛語音後邊打鉤(選擇後才能設定其他項) -- > 預設引擎(訊飛語音) -->語言(中文) (用必要是還可以在訊飛語音合成裡選擇男聲和女聲) 

配置完成後,可直接用系統提供的TextToSpeak的方法直接呼叫即可. 

(測試發現,改發音是中標準的中文發音,據瞭解,國內語音識別系統做的最好的一家,發音連貫,並且可辨認符號如℃等) 


示例程式碼如下: 

    public class NiHaoTTS extends Activity implements OnInitListener {                                                      



private int MY_DATA_CHECK_CODE = 0;                                                                                    

private TextToSpeech tts;                                                                                              


private EditText inputText;                                                                                            

private Button speakButton;                                                                                            



@Override                                                                                                                 

public void onCreate(Bundle savedInstanceState) {                                                                         



  super.onCreate(savedInstanceState);                                                                                      

  setContentView(R.layout.xunfei_tts);                                                                                           



  inputText = (EditText) findViewById(R.id.input_text);                                                                    

  speakButton = (Button) findViewById(R.id.speak_button);                                                                  

  speakButton.setOnClickListener(new OnClickListener() {                                                                   

   @Override                                                                                                               

   public void onClick(View v) 

   {                                                                                           

       String text = inputText.getText().toString();                                                                       

       if (text!=null && text.length()>0) 

       {                                                                                

       Toast.makeText(NiHaoTTS.this, "Saying: " + text, Toast.LENGTH_LONG).show();   

       //開始閱讀 text 

       tts.speak(text, TextToSpeech.QUEUE_ADD, null);                                                                         

       }                                                                                                                   

      }                                                                                                                       

         });                                                                                                                  



    Intent checkIntent = new Intent();                                                                                       

        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);                                                    

        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);                                                             

    }                                                                                                                      

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {                                            

        if (requestCode == MY_DATA_CHECK_CODE) 

        {                                                                           

            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) 

            {                                                 

                // success, create the TTS instance                                                                        

            tts = new TextToSpeech(this, this);                                                                        

            }                                                                                                              

            else 

            {                                                                                                         

            // missing data, install it                                                                                

            Intent installIntent = new Intent();                                                                       

            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);                                      

            startActivity(installIntent);                                                                              

        }                                                                                                              

        }                                                                                                                  



   }                                                                                                                      



@Override                                                                                                              

public void onInit(int status) 

{                                                                                       

    if (status == TextToSpeech.SUCCESS) 

    {                                                                              

        Toast.makeText(NiHaoTTS.this,                                                                               

                "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();                                     

    }                                                                                                                  

    else if (status == TextToSpeech.ERROR) 

    {                                                                           

    Toast.makeText(NiHaoTTS.this,                                                                               

            "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();                  

}                                                                                                                  

    }                                                                                                                      

}