आशा है कि आप बहुत अच्छा और सुरक्षित महसूस कर रहे हैं। मेरे पास एक ऐप है जो उपयोगकर्ता से ऑडियो रिकॉर्ड करता है और फिर इसे स्क्रीन पर संपादन टेक्स्ट में टेक्स्ट के रूप में प्रदर्शित करता है। मेरे पास समस्या यह है कि टेक्स्ट-केवल उपयोगकर्ता के बोलने के बाद ही प्रकट होता है, मैं जो चाहता हूं वह "Google सहायक की तरह संक्षेप में" बोलते समय टेक्स्ट प्रदर्शित करना है।
मैंने क्या किया है:
मैंने इंटरनेट पर खोज की है और मुझे इसे करने का एक तरीका मिल गया है, ऐसा कहा जाता है कि आप पार्टियल रिसेट्स पर ओवरराइड कर सकते हैं और फिर डेटा "नीचे कोड देखें" प्राप्त करने के लिए ArrayList बनाते हैं। जिस तरह से
@Override public void onPartialResults(Bundle bundle) { micButton.setImageResource(R.drawable.ic_mic_on); ArrayList data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); String word = (String) data.get(data.size() - 1); editTextWriteText.setText(word); Log.i("TEST", "partial_results: " + word); }
लेकिन समस्या का समाधान नहीं हुआ, तो मैं इस समस्या का समाधान कैसे कर सकता हूं?
यहां मेरा कोड नीचे दिया गया है।
.
MainActivity.java
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
EditText editTextWriteText;
Button speakButton;
private SpeechRecognizer speechRecognizer;
ImageView micButton;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SpeechRecorder();
}
/*
* Methods for SpeechRecorder button
*/
private void SpeechRecorder () {
micButton = findViewById(R.id.speak_button);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
micButton.setImageResource(R.drawable.ic_mic_on);
}
@Override
public void onBeginningOfSpeech() {
editTextWriteText.setHint("Listening...");
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
micButton.setImageResource(R.drawable.ic_mic_off);
editTextWriteText.setHint("Tap to enter text ...");
}
@Override
public void onResults(Bundle bundle) {
micButton.setImageResource(R.drawable.ic_mic_off);
ArrayList<String> data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editTextWriteText.setText(data.get(0));
editTextWriteText.setHint("Tap to enter text ...");
}
@Override
public void onPartialResults(Bundle bundle) {
micButton.setImageResource(R.drawable.ic_mic_on);
ArrayList data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String word = (String) data.get(data.size() - 1);
editTextWriteText.setText(word);
Log.i("TEST", "partial_results: " + word);
}
@Override
public void onEvent(int i, Bundle bundle) {
}
});
micButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
speechRecognizer.stopListening();
}
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
micButton.setImageResource(R.drawable.ic_mic_on);
speechRecognizer.startListening(speechRecognizerIntent);
textToSpeech.stop();
}
return false;
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
speechRecognizer.destroy();
}
}
activity_main.xml
<EditText
android:id="@+id/text_write"
android:layout_width="match_parent"
android:layout_height="@dimen/_390sdp"
android:layout_above="@+id/speech_button"
android:layout_below="@+id/text_name"
android:layout_marginStart="@dimen/_11sdp"
android:layout_marginTop="@dimen/_24sdp"
android:layout_marginEnd="@dimen/_11sdp"
android:layout_marginBottom="@dimen/_47sdp"
android:background="@drawable/rounder_edit_text"
android:elevation="2dp"
android:gravity="center"
android:hint="@string/edittext_hint"
android:inputType="textMultiLine"
android:ems="10"
android:maxLength="900"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingRight="8dp"
app:counterEnabled="true" />
<ImageView
android:id="@+id/speak_button"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="@dimen/_12sdp"
android:layout_marginBottom="@dimen/_13sdp"
android:background="@drawable/ic_mic_off" />
1 उत्तर
नीचे की तरह होने के लिए अपना भाषण पहचानकर्ता इरादा बदलें:
final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); //this was missing
जब आप RecognizerIntent.EXTRA_PARTIAL_RESULTS को अपने स्पीच रिकॉग्नाइज़र इंटेंट में सही पर सेट करते हैं, तो ऑनपार्टियल रिज़ल्ट्स (बंडल बंडल) को आपको स्पीकिंग के दौरान वांछित परिणाम देकर कॉल किया जाना चाहिए।
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।