iT邦幫忙

0

Android Studio TTS 說中文

目前TTS程式碼怎麼寫 他都只會說英文 這讓我很頭疼QAQ 想請問各位大大該如何解決
以下是程式碼:


import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.support.v7.app.AppCompatActivity;
import java.util.Locale;


public class sound extends AppCompatActivity
{
    private EditText et;

    /** TTS 物件 */
    private TextToSpeech tts;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // 建立 TTS
        createLanguageTTS();

        // 輸入文字
        et = new EditText(this);

        // 按鈕
        Button b = new Button(this);
        b.setText("Speech");
        b.setOnClickListener( new View.OnClickListener(){
            @Override
            public void onClick(View arg0)
            {
                //【英文】發音
               // tts.speak( et.getText().toString(), TextToSpeech.QUEUE_FLUSH, null );
                tts.speak("you are  你好  hi", TextToSpeech.QUEUE_ADD, null);
            }
        });

        // 版面配置
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.addView( et, 320, 200 );
        ll.addView( b, 320, 100 );
        addContentView( ll, new LinearLayout.LayoutParams(320, 480) );
    }

    @Override
    protected void onDestroy()
    {
        // 釋放 TTS
        if( tts != null ) tts.shutdown();

        super.onDestroy();
    }

    private void createLanguageTTS()
    {
        if( tts == null )
        {
            tts = new TextToSpeech(this, new TextToSpeech.OnInitListener(){
                @Override
                public void onInit(int arg0)
                {
                    // TTS 初始化成功
                    if( arg0 == TextToSpeech.SUCCESS )
                    {
                        // 指定的語系: 英文(美國)
                       // Locale l = Locale.US;  // 不要用 Locale.ENGLISH, 會預設用英文(印度)
                        //Locale.TAIWAN(locale.Traditional.Han.Taiwan);
                        // 目前指定的【語系+國家】TTS, 已下載離線語音檔, 可以離線發音
                        if( tts.isLanguageAvailable( Locale.CHINESE ) == TextToSpeech.LANG_COUNTRY_AVAILABLE )
                        {
                            tts.setLanguage(Locale.CHINESE);
                        }
                    }
                }}
            );
        }
    }
}

執行過後他會自動跳過中文 只唸英文
但是locale已設定成中文了...

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2017-04-21 12:28:48
最佳解答

我要發表回答

立即登入回答