2017-02-21 90 views
0

下面這個簡單的代碼可以調用另一個視圖組,這些都來自基本的第一個應用程序教程。Android Studio中的TextToSpeech未被調用/無法正常工作

https://developer.android.com/training/basics/firstapp/starting-activity.html

我用這個問題爲導向,以獲得TTS,以及應用程序運行吐司和看法,但跳過了文本到語音: Text to speech(TTS)-Android

然後我嘗試添加TTS通話/ class。我對Android很陌生,所以想知道是否有人能向我解釋,或者指出我如何使用這些類並圍繞它們構建幫助我理解總體的正確方向,但總體而言,解決這個問題的方法會很棒, 非常感謝!

package com.example.ollie.myapplication3;

import android.content.Context; 
    import android.content.Intent; 
    import android.net.Uri; 
    import android.os.Build; 
    import android.speech.tts.TextToSpeech; 
    import android.support.annotation.RequiresApi; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.EditText; 
    import android.widget.Toast; 

    import com.google.android.gms.appindexing.Action; 
    import com.google.android.gms.appindexing.AppIndex; 
    import com.google.android.gms.appindexing.Thing; 
    import com.google.android.gms.common.api.GoogleApiClient; 

    import java.util.Locale; 

    public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener { 

     public static final String EXTRA_MESSAGE = "com.example.myapplication.MESSAGE"; 
     /** 
     * ATTENTION: This was auto-generated to implement the App Indexing API. 
     * See https://g.co/AppIndexing/AndroidStudio for more information. 
     */ 
     private GoogleApiClient client; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      // ATTENTION: This was auto-generated to implement the App Indexing API. 
      // See https://g.co/AppIndexing/AndroidStudio for more information. 
      client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
     } 

     /** 
     * Called when user clicks send button 
     */ 
     public void sendMessage(View view) { 

      TextToSpeech tts = new TextToSpeech(this, this); 
      tts.setLanguage(Locale.US); 
      tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null, null); 

      Context context = getApplicationContext(); 
      CharSequence text = "Hello toast!"; 
      int duration = Toast.LENGTH_LONG; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 

      Intent intent = new Intent(this, DisplayMessageActivity.class); 
      EditText editText = (EditText) findViewById(R.id.edit_message); 
      String message = editText.getText().toString(); 
      intent.putExtra(EXTRA_MESSAGE, message); 
      startActivity(intent); 
     } 

     @Override 
     public void onInit(int status) { 
      TextToSpeech tts = new TextToSpeech(this, this); 
      tts.setLanguage(Locale.US); 
      tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null, null); 
     } 
    } 

回答

2

儘量讓你的「TTS」變量當地一個(在活動),所以你要確保你正在使用你的實例,而不是創建你按下按鈕一個新的每次相同的實例。

像這樣:

public class MainActivity extends AppCompatActivity { 

private TextToSpeech tts; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      if(status != TextToSpeech.ERROR) { 
       tts.setLanguage(Locale.US); 
      } 
     } 
    }); 

} 


public void sendMessage(View view) { 
    tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null, null); 

    ... 
} 

} 
相關問題