我有一个服务,我正在尝试将语音发送到文本引擎,但它不起作用..是否可以从服务启动语音到文本?
MyService.java CLASS:
public class MyService extends Service{
OnActRes r;
EditText edittext;
//private Runnable m;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stubry
try
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Toast.makeText(MyService.this,"STEP 1 !", Toast.LENGTH_LONG).show();
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
Toast.makeText(MyService.this,"STEP 2 !", Toast.LENGTH_LONG).show();
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
Toast.makeText(MyService.this,"STEP 3 !", Toast.LENGTH_LONG).show();
//since we only want one, request 1
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
Toast.makeText(MyService.this,"STEP 4 !", Toast.LENGTH_LONG).show();
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
Toast.makeText(MyService.this,"STEP 5 !", Toast.LENGTH_LONG).show();
r.startActivityForResult(intent, 1234);
}
catch(Exception e)
{
Toast.makeText(MyService.this,"ERROR here !", Toast.LENGTH_LONG).show();
}
}
}
Result.java
public class OnActRes extends Activity {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK){
{
Toast.makeText(OnActRes.this,"AR", Toast.LENGTH_LONG).show();
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String topResult = matches.get(0);
Toast.makeText(OnActRes.this,topResult, Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:0)
您应该检查/更改的一些事项:
r
为空。 OnActRes
活动中执行此操作。)如果两者都无效,您可以尝试使用Android's API进行语音识别,而不使用识别活动(Example here)。如果您愿意,可以从服务中调用此方法。