如何從TextToSpeech類調用TextToSpeech類沒有任何UI ..在應用程序中只有一個佈局,主類使用..現在我的任務是使app speak ans根據情況與用戶互動..TextToSpeech應用程序..調用沒有UI
回答
//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();
}
@Sodhi先生我不使用StartActivity功能,因爲我不想顯示對話..請你詳細說明這個代碼是怎麼回事..謝謝 – Shah 2012-03-12 12:53:51
@Sodhi先生我想要使應用程序只講一些存儲在數據庫中的句子..不從服務器..謝謝 – Shah 2012-03-12 12:56:07
這個代碼基本上是關於在android throgh文本到語音小工具中的語音識別。當你點擊start_btn時運行此代碼將出現一個對話框無論你說什麼,它都會自動將你說出的單詞轉換爲文本。但我認爲這段代碼並不熟悉你的需求,因爲你有一個db ich句子被存儲,你可以在android中使用媒體播放器對象的特定句子的mp3文件,並且該解決方案一定會工作,因爲我在過去的應用程序中也有這樣的情況,我使用特定單詞和句子的mp3文件..... – 2012-03-12 13:11:24
您不需要有UI或Activity來使用TextToSpeech。
- 1. 沒有UI的BlackBerry應用程序
- 2. 沒有UI的背景應用程序
- 3. 在沒有任何UI顯示的情況下調用TextToSpeech活動
- 4. Twilio調用集成到ios應用程序沒有服務器應用程序
- 5. 如何通過c#應用程序用UI調用外部Windows應用程序?
- 6. 片段調用導致應用程序沒有響應
- 7. 瞭解沒有應用程序調用的iOS線程轉儲
- 8. 現有Java應用程序的Web UI
- 9. TextToSpeech服務沒有啓動
- 10. Twitterizer OAuth登錄桌面應用程序沒有Web UI
- 11. 沒有UI的Android設置應用程序
- 12. NSManagedObjects在沒有UI的應用程序中的基本方法
- 13. 沒有ui應用程序在前臺運行?
- 14. 我的應用程序似乎並沒有從UI文件
- 15. Scala在沒有Web UI的情況下播放應用程序?
- 16. 從一個應用程序發送短信沒有UI
- 17. 沒有UI的C#程序
- 18. 沒有UI的VB.net程序
- 19. 有沒有辦法讓VB 6應用程序調用.NET程序集?
- 20. 在線程中調用UI應用程序
- 21. Web應用程序UI
- 22. UI強化應用程序
- 23. asp.net應用程序調用服務說它沒有初始化?
- 24. 在Angular 2應用程序中沒有調用服務
- 25. 在J2EE應用程序時沒有監聽器被調用?
- 26. 從沒有Ksoap的android應用程序調用webservice
- 27. Android調試應用程序沒有使用branch.io開放
- 28. motionBegan沒有在整個應用程序中調用
- 29. 沒有回調工作與我的新通用應用程序
- 30. fbDidLogin沒有在我的iPhone應用程序中調用
100%重複http://stackoverflow.com/questions/4771562/call-texttospeech-activity-without-any-ui-display – asktomsk 2012-03-12 10:47:47
@asktomsk先生我想得到一些解決方案我沒有打算複製someOne: )..所以請.. :) – Shah 2012-03-12 12:39:05