2012-03-12 11 views
2

TextToSpeechクラスにUIがない場合、別のクラスからTextToSpeechクラスを呼び出す方法..アプリケーションでメインクラスが使用するレイアウトは1つだけです。アプリはANSTextToSpeech App。UIなしで呼び出す

+0

100%重複http://stackoverflow.com/questions/4771562/call-texttospeech-activity-without-any-ui-display – asktomsk

+0

@asktomsk Sir私はいくつかの解決策を得たいですsomeNoneをコピーするつもりはありませんでした: )..だから.. :) – Shah

答えて

0
 //decleration 
    TextToSpeech talker; 
Button speakButton; 

//onCreate 
talker = new TextToSpeech(this, this); 
    speakButton=new Button(this); 

// Check to see if a recognition activity is present 
     PackageManager pm = getPackageManager(); 
     List<ResolveInfo> activities = pm.queryIntentActivities(
       new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
     if (activities.size() != 0) { 
      speakButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startVoiceRecognitionActivity(); 
      } 
     }); 
     } else { 
      speakButton.setEnabled(false); 
      speakButton.setText("Recognizer not present"); 
     } 


/** 
    * Fire an intent to start the speech recognition activity. 
    */ 
    private void startVoiceRecognitionActivity() { 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo"); 
     startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 


    } 

/** 
    * Handle the results from the recognition activity. 
    */ 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { 
      // Fill the list view with the strings the recognizer thought it could have heard 
      ArrayList<String> matches = data.getStringArrayListExtra(
        RecognizerIntent.EXTRA_RESULTS); 

      // Toast.makeText(VoiceRecognition.this, matches.get(0), 5000).show(); 
     String device = "Bedroom"; 
      /* mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
     matches));  
      */ 
      txt.setText(""+matches.get(0)); 
      String host = "http://web2.anzleads.com/Android/nimboPani/web-service.php?tmote="; 
      String userCommand = URLEncoder.encode(matches.get(0)); 
      String deviceSelected = "&org=" + device; 
      res = getServerResponse(host + userCommand + deviceSelected); 
      say(""+ res); 
      Toast.makeText(VoiceRecognition.this,res ,5000).show(); 


     } 

     super.onActivityResult(requestCode, resultCode, data); 
    } 


    public void say(String text2say){ 
        talker.speak(text2say, TextToSpeech.QUEUE_FLUSH, null); 
       } 


    public void onInit(int status) { 
     // TODO Auto-generated method stub 
    // say("Hello World"); 
    } 


      public void onDestroy() { 
      if (talker != null) { 
       talker.stop(); 
       talker.shutdown(); 
       } 

       super.onDestroy(); 
      } 
public String getServerResponse(String url) 
    { 
     String result = ""; 
     HttpClient hc = new DefaultHttpClient(); 
     HttpResponse hr ; 
     HttpGet hg = new HttpGet(url); 
     try 
     { 
      hr = hc.execute(hg); 
      if(hr.getStatusLine().getStatusCode() == 200) 
      { 
       HttpEntity he = hr.getEntity(); 
       if (he != null) 
       { 
        InputStream is = he.getContent(); 
         result = convertStreamToString(is); 
         is.close(); 

       } 
      } 
     } 



     catch (Exception e) { 
      // TODO: handle exception 
     } 

     return result; 

    } 


    private String convertStreamToString(InputStream instream) { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); 
     StringBuilder sb = new StringBuilder(); 


     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       instream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return sb.toString(); 

    } 
+0

@Sodhi Sir私はStartActivity関数を使用していないので、私はダイアログを表示する必要はありません..あなたはこのコードで何が起こっているか詳細を教えてくださいできます。ありがとう – Shah

+0

@Sodhi Sir wantアプリケーションがデータベースに格納されているいくつかの文章だけを話すようにする..サーバからではありません..ありがとう – Shah

+0

このコードは基本的に音声ウィジェットの音声認識の音声認識に関するものです.Windowsのstart_btnをクリックするとこのコードが実行されます&あなたが話しているものは、自動的にあなたの話し言葉をtext.Butに変換します。しかし、このコードはあなたの要件に精通していないと思います。あなたは特定の言葉とセンテンスのmp3ファイルを使用する過去のアプリでこのような状況があるので、あなたはアンドロイド&そのソリューションは確かに動作するメディアプレーヤーオブジェクトで特定の文のmp3ファイルを使用することがあります..... –

0

あなたはUIやテキスト読み上げを使用するための活動を持っている必要はありません。..状況に応じてユーザと対話話します。

関連する問題