2014-03-18 56 views
0

儘管我的代碼沒有錯誤且準確,但Android並不會說話。我使用了吐司功能來查看問題發生的位置。但是,這兩個敬酒都被稱爲。文本到語音功能不起作用

我已經刪除了所有不相關的代碼。

下面的代碼大多是相關的。

package com.example.android.BluetoothChat; 


import android.R.string; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.os.Handler; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener; 
import android.telephony.SmsManager; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.Locale; 
import java.util.Set; 
import android.bluetooth.BluetoothDevice; 

import java.io.InputStream; 
import java.io.OutputStream; 
import android.bluetooth.BluetoothSocket; 
import android.widget.EditText; 
import java.io.IOException; 
import java.util.UUID; 

public class MainActivity extends Activity implements LocationListener{ 
    Button start, stop; 
    TextView tv; 
    TextView tv2; 
    TextView sum; 
    LocationManager lm; 
    static TextToSpeech Talker; 
    TextView myLabel; 
    Handler mhandler = new Handler(); 
    int i; 
    int sum1 = 0; 
    static BluetoothDevice mmDevice; 
    static BluetoothAdapter mBluetoothAdapter; 
    static BluetoothSocket mmSocket; 
    String lat; 
    String lon; 

    EditText myTextbox; 
    static OutputStream mmOutputStream; 
    static InputStream mmInputStream; 
    static Thread workerThread; 
    static byte[] readBuffer; 
    static int readBufferPosition; 
    static int counter; 
    static volatile boolean stopWorker; 
    TextToSpeech talker; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     setTitle("Seizure Detection Helmet Application"); 

     myTextbox = (EditText)this.findViewById(R.id.name); 
     talker = new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener() { 
      @Override 

      public void onInit(int status) { 
       if(status != TextToSpeech.ERROR) 
       { 
       talker.setLanguage(Locale.US); 
       } 

      } 
     }); 
     Button talk = (Button)this.findViewById(R.id.talk); 
     talk.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       speakOut(); 
      } 

     }); 


    public void speakOut() 

    { 
     String toSpeak = myTextbox.getText().toString(); 
     Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show(); 
     String full = ("My name is"+toSpeak+ "and I am having a Seizure at 38.901 Latitude and -77.031 Longitude "); 
     Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show(); 
     talker.speak(full, TextToSpeech.QUEUE_FLUSH, null); 
     Toast.makeText(getApplicationContext(), full, Toast.LENGTH_SHORT).show(); 
     talker.speak("HELLO",TextToSpeech.QUEUE_FLUSH, null); 
    } 


} 
+0

您正在測試哪個版本的設備? –

+0

哦,那很重要嗎?我在Galaxy S2上測試過它。 – user3385015

回答

1

您的代碼有幾個問題。您應該檢查由setLanguage返回的錯誤。在調用onInit之前,您不能撥打speakOut。確保這一點的一種方法是在xml佈局文件中禁用通話按鈕並在onInit中啓用它。

+0

我沒有setLanguage返回的錯誤。 setLanguage有什麼問題?我如何致電onInit? – user3385015

+0

你不調用onInit,它是一個回調,當你創建TextToSpeech對象時會調用它。在setLanguage put talk.setEnabled(true)之後,只需在佈局xml和onInit中禁用通話按鈕, –