2011-01-25 4 views
1

ねえ、私はCountDownTimerにテキスト読み上げを入れようとしています。一定の時間が経過した後、「残っているx秒があります」と言いたい。 tts = new TextToSpeech(this, this)私のCountDownTimerのテキスト読み上げで何が問題になっていますか?

package com.android.countdown; 


import java.util.Locale; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.view.View; 
import android.speech.tts.TextToSpeech; 
import android.widget.Button; 
import android.widget.TextView; 
import android.view.View.OnClickListener; 

public class countdown extends Activity implements TextToSpeech.OnInitListener{ 
CountDownTimer Counter1; 
CountDownTimer Counter2; 
CountDownTimer Counter3; 
int Interval = 1; 
TextToSpeech tts; 

    public String formatTime(long millis) { 
      String output = "0:00"; 
      long seconds = millis/1000; 
      long minutes = seconds/60; 

      seconds = seconds % 60; 
      minutes = minutes % 60; 


      String secondsD = String.valueOf(seconds); 
      String minutesD = String.valueOf(minutes); 

      if (seconds < 10) 
      secondsD = "0" + seconds; 
      if (minutes < 10) 
      minutesD = "0" + minutes; 

      output = minutesD + " : " + secondsD; 
      return output; 
     } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 




//Declare Start/Stop timer 
Button btnstart = (Button)findViewById(R.id.btnstart); 
Button btnstop = (Button)findViewById(R.id.btnstop); 

//Text field to show time left 
final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1); 
final TextView mCounter2TextField = (TextView)findViewById(R.id.counter2); 
final TextView mCounter3TextField=(TextView)findViewById(R.id.counter3); 



//Counter 1 
Counter1 = new CountDownTimer(20000 , Interval) { 
public void onTick(long millisUntilFinished){ 
    mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished)); 
    if (millisUntilFinished == 10000) { 
    instantiate(); 
    } 


} 

public void onFinish() { 
    Counter1.start(); 
} 
}; 

//Counter 2 
Counter2 = new CountDownTimer(80000 , Interval) { 
public void onTick(long millisUntilFinished) { 
    mCounter2TextField.setText("Seconds left: " + formatTime(millisUntilFinished)); 

} 

public void onFinish() { 
    mCounter2TextField.setText("Finished!"); 
    Counter2.start(); 
} 
}; 

//Counter 3 
Counter3 = new CountDownTimer(3000 , Interval) { 
    public void onTick(long millisUntilFinished) { 
     mCounter3TextField.setText("Seconds left: " + formatTime(millisUntilFinished)); 
    } 

    public void onFinish() { 
    mCounter3TextField.setText("Finished!"); 
    Counter3.start(); 
    } 
    }; 


//Start Button 
btnstart.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
    Counter1.start(); 
    Counter2.start(); 
    Counter3.start(); 
    } 
}); 

//Stop Button 
btnstop.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
    Counter1.cancel(); 
    Counter2.cancel(); 
    Counter3.cancel(); 
    if (tts != null) { 
    tts.stop(); 
     tts.shutdown(); 
    } 
    } 
}); 
} 

    public void instantiate() { 
     tts = new TextToSpeech(this, this); 
     tts.setLanguage(Locale.US); 
     tts.speak("You have 10 seconds remaining", TextToSpeech.QUEUE_ADD, null); 

    } 

@Override 
public void onInit(int status) { 

} 

    @Override 
    public void onDestroy() { 
     // Don't forget to shutdown! 
     if (tts != null) { 
      tts.stop(); 
      tts.shutdown(); 
     } 

     super.onDestroy(); 
    } 


} 
+0

私の答えが間違っている場合は、より詳細に受け取ったエラーを説明するスタックトレースを投稿する必要があります。 – McStretch

+0

また、tts = new TextToSpeech(..はtrueの場合は条件付きですが、他の2つの行には中括弧はありません)以下の2つは常に実行されます。 – NickT

+0

@NickT:良いキャッチ、私はそこにいたことに気がつきませんでした。私はあなたの答えを私の中に合併し、あなたに信じ込んでしまいました。 – McStretch

答えて

1

あなたの第二引数はTextToSpeech.OnInitListenerを実装していません..私はちょうどテキスト読み上げを使用して開始し、私は私がやっている、本当にわかりません。

あなたがcountdownを持っている必要がありますまたは別のクラスはTextToSpeech.OnInitListenerを実装します。そして、言ったクラスでのOnInit()を実装

public class countdown extends Activity implements TextToSpeech.OnInitListener { 

を:

void onInit(int status){ 
    // implementation 
} 

を最後にTextToSpeechコンストラクタにOnInitListenerを実装するクラスを渡します:

// The second 'this' will be replaced with another class if you 
// decide to use a class other than countdown to implement the interface. 
tts = new TextToSpeech(this, this); 

チュートリアルTextToSpeechActivity.javaを参照してください。 として

EDIT

NickTが述べたように、あなたもonTickでif文あなたに中括弧を追加する必要があります。

if (millisUntilFinished == 10000) { 
    tts = new TextToSpeech(this, this); 
    tts.setLanguage(Locale.US); 
    tts.speak("You have 10 seconds remaining", TextToSpeech.QUEUE_ADD, null); 
} 
エルス

あなたがなる、常にsetLanguagespeakを実行しますmillisUntilFinished == 10000が真でない限り、NullPointerExceptionを与えてください。


http://developer.android.com/reference/android/speech/tts/TextToSpeech.OnInitListener.html

+0

さて、私はtts = new TextToSpeech(this、this);を私の公開void OnInit(){これをif(millisUntilFinished == 100000)のところに置いておくと、私はまだ同じエラーが出るからです。それは10000ミリ秒が残っているときにテキストを話すことはありません。しかし、私は(millisUntilFinished <11000 && millisUntilFinished> 9000)を置くと、それは動作しますが、私はエミュレータを閉じるまで音声が繰り返され続けます。 – MJ93

+0

だから基本的に今私はtts = new TextToSpeech(this、this)を持っています。私のif(millisUntilFinished)== 10000 {プロシージャの下。 "新しいTextToSpeech(new CountDownTimer(){}、new CountDownTimer(){}は未定義です)" – MJ93

+0

ああ、申し訳ありませんが、onTickがそのオブジェクトのインスタンス化にネストされていることはわかりませんでした。スペーシングは本当に質問に役立ちます:P。クラスにTextToSpeech.OnInitListenerを実装し、その型のオブジェクトをインスタンス化し、コンストラクタの2番目の引数として渡す必要があります。 – McStretch

1

(私の意見では)クリーンなコードを意味し、あなたの活動にTextToSpeech.OnInitListenerを実装する必要がdoen't別のアプローチは、次のとおりです。

tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() { 
    @Override 
    public void onInit(int status){ 
     if(status == TextToSpeech.SUCCESS) { 
      Log.d("myapp", "TextToSpeech enabled"); 
     } 
    } 
}); 

あなたがしたい場合は " ttsデータを尋ねる。私はこうです:

// I have this code inside onCreate(), but you can call it elsewhere. 
Intent checkIntent = new Intent(); 
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
startActivityForResult(checkIntent, 6); 

// Then, in the activity add this code: 
@Override 
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) { 
    if (requestCode == 6) { 
     if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { 
      tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() { 
        @Override 
        public void onInit(int status){ 
         if(status == TextToSpeech.SUCCESS) { 
          Log.d("myapp", "TextToSpeech prepared"); 
         } 
        } 
       }); 
     } 
    } 
} 

TextToSpeech.isLanguageAvailable()とほぼ同じです。私は将来それを変えるつもりです。