Sử dụng CMUSphinx thư viện, nơi nó sẽ làm việc trong chế độ ngoại tuyến, Không cần các nút để kích hoạt nó, bạn có thể đặt tên cho nó và bằng cách sử dụng tên mà bạn có thể kích hoạt các mô-đun công nhận Trong bên dưới liên kết bạn có thể tìm thấy mã nguồn đầy đủ
1) nó sẽ làm việc trong chế độ offline 2) bạn có thể đặt tên nó lên 3) nó sẽ bắt đầu Listening khi bạn gọi tên anh
private static final String KEYPHRASE = "ok computer";
private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
private SpeechRecognizer recognizer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
captions = new HashMap<String, Integer>();
captions.put(KWS_SEARCH, R.string.kws_caption);
captions.put(MENU_SEARCH, R.string.menu_caption);
setContentView(R.layout.activity_maini);
}
private void runRecognizerSetup() {
// Recognizer initialization is a time-consuming and it involves IO,
// so we execute it in async task
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(MainActivity.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception result) {
if (result != null) {
((TextView) findViewById(R.id.caption_text))
.setText("Failed to init recognizer " + result);
} else {
switchSearch(KWS_SEARCH);
}
}
}.execute();
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
runRecognizerSetup();
} else {
finish();
}
}
}
public void onResult(Hypothesis hypothesis) {
((TextView) findViewById(R.id.result_text)).setText("");
if (hypothesis != null) {
String text = hypothesis.getHypstr();
makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}}
http://stackoverflow.com/questions/10182893/can-i-keep -the-speech-recognizer-listening-indefinitely –
http://stackoverflow.com/questions/14940657/android-speech-recognitio n-as-a-dịch vụ-trên-android-4-1-4-2/14950616 # 14950616 –