2

我的应用程序使用了 Android 提供的 TTS 功能,但是当我使用它时,它显示此错误提示失败:未绑定到 tts 引擎。

这是我的代码。请如果有人可以帮助我,我搜索了任何人面临的类似问题,但我没有找到解决方案。

 public class WeatherMeActivity extends Activity implements TextToSpeech.OnInitListener{
private ProgressDialog pDialog;


private TextToSpeech tts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;

private static final String TAG = "Error";


/** Called when the activity is first created. */


static String stat_strCity = null;
static String stat_strCountry = null;
static String stat_strTempUnit = null;
static String stat_strTempValue = null;
static String stat_strHumidity = null;
static String stat_strText = null;;
static String stat_strCode = null;;
static String stat_strDate = null;;
static String stat_strVisi = null;


public String location="Jerusalem";
public String  temperature;
public String  humidityTitle;
public String  humidityValue;
public String   visiValue;
public String   dateTime;
private static HttpURLConnection httpConnection;
private static final String URL_YAHOO_API_WEATHER = "http://weather.yahooapis.com/forecastrss?w=%s&u=c";
private static final String PARAM_YAHOO_LOCATION = "yweather:location";
private static final String PARAM_YAHOO_UNIT = "yweather:units";
private static final String PARAM_YAHOO_ATMOSPHERE = "yweather:atmosphere";
private static final String PARAM_YAHOO_CONDITION = "yweather:condition";
private static final String PARAM_YAHOO_VALUE = "yweather:wind";

/** Attribute city */
private static final String ATT_YAHOO_CITY = "city";
private static final String ATT_YAHOO_COUNTRY = "country";
private static final String ATT_YAHOO_TEMP_UNIT = "temperature";
private static final String ATT_YAHOO_HUMIDITY = "humidity";
private static final String ATT_YAHOO_TEXT = "text";
private static final String ATT_YAHOO_CODE = "code";
private static final String ATT_YAHOO_DATE = "date";
private static final String ATT_YAHOO_TEMP = "chill";
private static final String ATT_YAHOO_VISI = "visibility";

/** Image array */
public static final int[][] m_ImageArr = {
    ......
};

int nCode = 0;
/** Location */
 TextView m_TextLocation;

/** Temperature */
  TextView m_Temperature;

/** Humimidy */
  TextView m_Humimidy;

/** State */
  TextView  m_Visibility;

/** Time */
  TextView m_Date;

/** Icon */
  ImageView m_WeatherIcon;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
    Log.e(TAG,"load main view done");


    setContentView(R.layout.weather_layout);
    tts = new TextToSpeech(this, this);
    // Fire off an intent to check if a TTS engine is installed
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

    Log.e(TAG,"load main view");
   // initializeView();
    Log.e(TAG,"intialize view done");

    m_TextLocation = (TextView)findViewById(R.id.location);
    m_Temperature = (TextView)findViewById(R.id.temperature);
    m_Humimidy = (TextView)findViewById(R.id.humidityValue);
    m_Visibility = (TextView)findViewById(R.id.visiValue);
    m_WeatherIcon = (ImageView) findViewById(R.id.weather_icon);
    m_Date = (TextView)findViewById(R.id.dateTime);





    Button lisAgain = (Button)findViewById(R.id.button1);
    lisAgain.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            speakOut("you are in ");
        }
    });

    new weatherGet().execute();


}

public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Resources res = getResources();
    String songsFound = res.getString(R.string.tts_on);

    if( songsFound.equals("true"))
        ;//speakOut("you are in ");
}
ImageButton btn_map,btn_agenda,btn_weather,btn_trip;


public void onInit(int status) {
    // TODO Auto-generated method stub

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        // tts.setPitch(5); // set pitch level

        // tts.setSpeechRate(2); // set speech speed rate

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } else {

            //speakOut("Tempreature is "+stat_strTempValue);
        }

    } else {
        Log.e("TTS", "Initilization Failed");
    }

}
private  void speakOut(String text ) {

    if( text !=null){

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

    }
}

  private boolean initializeView(){

        return true;
  }
    protected void onResume() {
        super.onResume();


    }

public static void parserYahooWeatherInfo(Document docWeather){
    if (docWeather == null){
        //Log.e(TAG,"Invalid doc weatehr");
    //  return null;
    }

    String strCity = null;
    String strCountry = null;
    String strTempUnit = null;
    String strTempValue = null;
    String strHumidity = null;
    String strText = null;
    String strCode = null;
    String strDate = null;
    String strVisi = null;
    try {
        Element root = docWeather.getDocumentElement();
        root.normalize();

        NamedNodeMap locationNode = root.getElementsByTagName(PARAM_YAHOO_LOCATION).item(0).getAttributes();

        if (locationNode != null){
            strCity = locationNode.getNamedItem(ATT_YAHOO_CITY).getNodeValue();
            strCountry = locationNode.getNamedItem(ATT_YAHOO_COUNTRY).getNodeValue();
        }

        NamedNodeMap unitNode = root.getElementsByTagName(PARAM_YAHOO_UNIT).item(0).getAttributes();
        if (unitNode != null){
            strTempUnit = unitNode.getNamedItem(ATT_YAHOO_TEMP_UNIT).getNodeValue();
        }

        NamedNodeMap atmosNode = root.getElementsByTagName(PARAM_YAHOO_ATMOSPHERE).item(0).getAttributes();
        if (atmosNode != null){
            strHumidity = atmosNode.getNamedItem(ATT_YAHOO_HUMIDITY).getNodeValue();
            strVisi = atmosNode.getNamedItem(ATT_YAHOO_VISI).getNodeValue();
        }

        NamedNodeMap conditionNode = root.getElementsByTagName(PARAM_YAHOO_CONDITION).item(0).getAttributes();
        if (conditionNode != null){
            strText = conditionNode.getNamedItem(ATT_YAHOO_TEXT).getNodeValue();
            strCode = conditionNode.getNamedItem(ATT_YAHOO_CODE).getNodeValue();
            strDate = conditionNode.getNamedItem(ATT_YAHOO_DATE).getNodeValue();
        }

        NamedNodeMap temNode = root.getElementsByTagName(PARAM_YAHOO_VALUE).item(0).getAttributes();
        if (temNode != null){
            strTempValue = temNode.getNamedItem(ATT_YAHOO_TEMP).getNodeValue();
        }


    } catch (Exception e){
    //  Log.e(TAG,"Something wroing with parser data");
        //return null;
    }

    stat_strCity = strCity ;
    stat_strCountry = strCountry ;
    stat_strTempUnit =strTempUnit ;
    stat_strTempValue = strTempValue;
    stat_strHumidity = strHumidity ;
    stat_strText = strText ;
    stat_strCode = strCode ;
    stat_strDate = strDate ;
    stat_strVisi = strVisi ;


    //return yahooWeatherInfo;
}
  private static int getImageByCode(String strCode){
        int nImageCode = R.drawable.a0;

        if (strCode == null){
            //Log.e(TAG,"Code is null");
            return nImageCode;
        }

        int nCode = Integer.parseInt(strCode);

        int nNumber= m_ImageArr.length;
        for (int i=0; i < nNumber; i++){
            if (nCode == m_ImageArr[i][1]){
                return m_ImageArr[i][0];
            }
        }
        return nImageCode;
  }

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

private static void requestConnectServer(String strURL) throws IOException {    
    strURL = "http://weather.yahooapis.com/forecastrss?w=1937180&u=c";

    httpConnection = (HttpURLConnection)new URL(strURL).openConnection();
    httpConnection.connect();

    if(httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK){
     Log.e(TAG,"Something wrong with connection");
        httpConnection.disconnect();
        throw new IOException("Error in connection: "+ httpConnection.getResponseCode());
    }
}

 **************************************************************************/
private static void requestDisconnect(){
    if(httpConnection != null){
        httpConnection.disconnect();
    }
}

public static Document getDocumentFromURL(String strURL) throws IOException {
    /* Verify URL */
    if (strURL == null){
        Log.e(TAG,"Invalid input URL");
        return null;
    }
    String strRegURL = String.format(URL_YAHOO_API_WEATHER, strURL);
    System.out.println(strRegURL);

    /* Connect to server */
    requestConnectServer(strRegURL);
    Log.e(TAG,"Connect server done");

    /* Get data from server */
    String strDocContent = getDataFromConnection();
    Log.e(TAG,"getDataFromConnection done");

    /* Close connection */
    requestDisconnect();
    Log.e(TAG,"requestDisconnect");
    if (strDocContent == null){
        Log.e(TAG,"Can not get xml content");
        return null;
    }

    int strContentSize = strDocContent.length();
    StringBuffer strBuff = new StringBuffer();
    strBuff.setLength(strContentSize+1);
    strBuff.append(strDocContent);
    ByteArrayInputStream is = new ByteArrayInputStream(strDocContent.getBytes());

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    Document docData = null;

    try {
        db = dbf.newDocumentBuilder();
        docData = db.parse(is);
    } catch (Exception e) {
        Log.e(TAG,"newDocumentBuilder");
        return null;
    }

    return docData;
}

private static String getDataFromConnection() throws IOException {
    if (httpConnection == null){
        return null;
    }

    String strValue = null;
    InputStream inputStream = httpConnection.getInputStream();
    if (inputStream == null){
        return null;
    }

    StringBuffer strBuf = new StringBuffer();       
    BufferedReader buffReader = new BufferedReader(new InputStreamReader(inputStream));
    String strLine = "";

    while ((strLine = buffReader.readLine()) != null){
        strBuf.append(strLine+"\n");
        strValue +=strLine+"\n";
    }

    /* Release resource to system */
    buffReader.close();
    inputStream.close();

    return strBuf.toString();
}

class weatherGet extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        Document dd = null;
        try {
             dd = getDocumentFromURL(location);
        } catch (IOException e) {
            Log.e(TAG,"intialize view done 2");

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        parserYahooWeatherInfo(dd);

        return null;
    }

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(WeatherMeActivity.this);
        pDialog.setMessage("Loading Weather Information. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected void onPostExecute(String file_url) {

        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                nCode = getImageByCode(stat_strCode);

                m_WeatherIcon.setImageResource(nCode);
                m_TextLocation.setText(stat_strCity);
                m_Temperature.setText(stat_strTempValue);
                m_Date.setText(stat_strDate);

                m_Humimidy.setText(stat_strHumidity);
                m_Visibility.setText(stat_strVisi);
                speakOut("Tempreature is "+stat_strTempValue);

            }
        });


    }

}

}

4

0 回答 0