这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions app/src/main/java/com/termux/api/apis/SpeechToTextAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.io.PrintWriter;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.LinkedBlockingQueue;

public class SpeechToTextAPI {
Expand All @@ -38,6 +40,8 @@ public SpeechToTextService(String name) {
}

protected SpeechRecognizer mSpeechRecognizer;
final CountDownLatch latch = new CountDownLatch(1);
final Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
final LinkedBlockingQueue<String> queueu = new LinkedBlockingQueue<>();

private static final String LOG_TAG = "SpeechToTextService";
Expand All @@ -47,6 +51,7 @@ public void onCreate() {
Logger.logDebug(LOG_TAG, "onCreate");

super.onCreate();

final Context context = this;

mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
Expand All @@ -62,6 +67,10 @@ public void onResults(Bundle results) {
List<String> recognitions = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Logger.logError(LOG_TAG, "RecognitionListener#onResults(" + recognitions + ")");
queueu.addAll(recognitions);
// On full mode, stop only when recognition is achieved
if (!recognizerIntent.getBooleanExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)) {
queueu.add(STOP_ELEMENT);
}
}

@Override
Expand Down Expand Up @@ -108,7 +117,11 @@ public void onError(int error) {
@Override
public void onEndOfSpeech() {
Logger.logError(LOG_TAG, "RecognitionListener#onEndOfSpeech()");
queueu.add(STOP_ELEMENT);

// On partial mode, stop recognition when speech ends
if (recognizerIntent.getBooleanExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)) {
queueu.add(STOP_ELEMENT);
}
}

@Override
Expand Down Expand Up @@ -141,13 +154,12 @@ public void onBeginningOfSpeech() {
.create().show();
}

Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Enter shell command");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
try {
latch.await(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
}
mSpeechRecognizer.startListening(recognizerIntent);

}

@Override
Expand All @@ -162,9 +174,20 @@ public void onDestroy() {
protected void onHandleIntent(final Intent intent) {
Logger.logDebug(LOG_TAG, "onHandleIntent:\n" + IntentUtils.getIntentString(intent));


recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Enter shell command");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);

recognizerIntent.putExtras(intent.getExtras());
latch.countDown();

ResultReturner.returnData(this, intent, new ResultReturner.WithInput() {
@Override
public void writeResult(PrintWriter out) throws Exception {
Logger.logError(LOG_TAG, "Start listening");
while (true) {
String s = queueu.take();
if (s == STOP_ELEMENT) {
Expand Down