TextToSpeech应用程序..调用没有UI
如何从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。
100%重复http://*.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