Speech only first time - android

I am interested in speech recognition in Android but I can't do it: it is not continuous. If you stop speaking, it doesn't continue, and you have to click on the button again.
I do not want this behaviour..
Does anybody have any suggestions as to what I can to fix this?
Recognise speech only first time I do not want this behaviour.
Here is the code:
private SpeechRecognizer speech;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
speech.startListening(intent);
}
#Override
public void onBeginningOfSpeech()
{
}
#Override
public void onBufferReceived(byte[] arg0)
{
}
#Override
public void onEndOfSpeech()
{
}
#Override
public void onError(int e)
{
}
#Override
public void onEvent(int arg0, Bundle arg1)
{
}
#Override
public void onPartialResults(Bundle arg0)
{
}
public void onReadyForSpeech(Bundle arg0)
{
}
#Override
public void onResults(Bundle data)
{
ArrayList<String> matches = data.getStringArrayList(
SpeechRecognizer.RESULTS_RECOGNITION);
}
#Override
public void onRmsChanged(float arg0)
{
}

Try to call again
speech.startListening(intent);
inside On Results and OnError.

Related

Trying to use SpeechRecognizer (Android speech recognition) but it's not starting?

Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_route);
SetupButton();
}
private void SetupButton()
{
Button createNewMessage = (Button) findViewById(R.id.button);
createNewMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListenForNewMessage();
}
});
}
private void ListenForNewMessage()
{
final SpeechRecognizer newDeliverySpeech = SpeechRecognizer.createSpeechRecognizer(this);
RecognitionListener newDeliveryRecognitionListener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
Log.d("SpeechListening","onReadyForSpeech");
}
#Override
public void onBeginningOfSpeech() {
Log.d("SpeechListening","onBeginningOfSpeech");
}
#Override
public void onRmsChanged(float rmsdB) {
//do nothing
}
#Override
public void onBufferReceived(byte[] buffer) {
//do nothing
}
#Override
public void onEndOfSpeech() {
Log.d("SpeechListening","onEndOfSpeech");
}
#Override
public void onError(int error) {
//do nothing
}
#Override
public void onResults(Bundle results) {
ArrayList<String> userMessage;
userMessage = results.getStringArrayList(RESULTS_RECOGNITION);
PushNewDelivery(userMessage);
}
#Override
public void onPartialResults(Bundle partialResults) {
//do nothing
}
#Override
public void onEvent(int eventType, Bundle params) {
//do nothing
}
};
newDeliverySpeech.setRecognitionListener(newDeliveryRecognitionListener);
if (newDeliverySpeech.isRecognitionAvailable(getApplicationContext()))
{
Log.d("SpeechListening","started listening hopefully");
newDeliverySpeech.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
}
}
The problem is, only the started listening hopefully is logged, the RecognitionListener never has onReadyForSpeech() or any of its methods called.
Can someone please tell me what I'm doing wrong here?
You can change this below line
newDeliverySpeech.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
To,
Intent mSpeechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
newDeliverySpeech.startListening(mSpeechIntent);
Ask the user for the permission to record audio before calling startListening:
String[] permissions = {Manifest.permission.RECORD_AUDIO};
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
https://developer.android.com/guide/topics/media/mediarecorder#audio-record-permission

onReadyForSpeech() in Android speech recognition (SpeechRecognizer) never gets called

Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_route);
SetupButton();
}
private void SetupButton()
{
Button createNewMessage = (Button) findViewById(R.id.button);
createNewMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListenForNewMessage();
}
});
}
private void ListenForNewMessage()
{
final SpeechRecognizer newDeliverySpeech = SpeechRecognizer.createSpeechRecognizer(this);
RecognitionListener newDeliveryRecognitionListener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
Log.d("SpeechListening","onReadyForSpeech");
}
#Override
public void onBeginningOfSpeech() {
Log.d("SpeechListening","onBeginningOfSpeech");
}
#Override
public void onRmsChanged(float rmsdB) {
//do nothing
}
#Override
public void onBufferReceived(byte[] buffer) {
//do nothing
}
#Override
public void onEndOfSpeech() {
Log.d("SpeechListening","onEndOfSpeech");
}
#Override
public void onError(int error) {
//do nothing
}
#Override
public void onResults(Bundle results) {
ArrayList<String> userMessage;
userMessage = results.getStringArrayList(RESULTS_RECOGNITION);
PushNewDelivery(userMessage);
}
#Override
public void onPartialResults(Bundle partialResults) {
//do nothing
}
#Override
public void onEvent(int eventType, Bundle params) {
//do nothing
}
};
newDeliverySpeech.setRecognitionListener(newDeliveryRecognitionListener);
if (newDeliverySpeech.isRecognitionAvailable(getApplicationContext()))
{
Log.d("SpeechListening","started listening hopefully");
newDeliverySpeech.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
}
}
The problem is, only the started listening hopefully is logged, the RecognitionListener never has onReadyForSpeech() or any of its methods called.
details
details
details
details
Can someone please tell me what I'm doing wrong here?
You are basically creating a new SpeechRecognizer object and register a new listener each time you click on the button. On top of that you create the SpeechRecognizer using the current Activity Context but you are actually using the Application context when calling: isRecognitionAvailable();
Try to create the SpeechRecognizer as a member object and register your listener when onCreate() is called. Also try to avoid using the Application context to avoid memory leaks.
Here is an example of how you should do it.
private SpeechRecognizer mDeliverySpeech;
private Intent mSpeechIntent;
private boolean mListening = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_route);
SetupButton();
SetupSpeechRecognizer();
mSpeechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
}
private void SetupButton()
{
Button createNewMessage = (Button) findViewById(R.id.button);
createNewMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListenForNewMessage();
}
});
}
private void SetupSpeechRecognizer()
{
mDeliverySpeech = SpeechRecognizer.createSpeechRecognizer(this);
RecognitionListener newDeliveryRecognitionListener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
Log.d("SpeechListening","onReadyForSpeech");
}
#Override
public void onBeginningOfSpeech() {
Log.d("SpeechListening","onBeginningOfSpeech");
}
#Override
public void onRmsChanged(float rmsdB) {
//do nothing
}
#Override
public void onBufferReceived(byte[] buffer) {
//do nothing
}
#Override
public void onEndOfSpeech() {
Log.d("SpeechListening","onEndOfSpeech");
}
#Override
public void onError(int error) {
//do nothing
}
#Override
public void onResults(Bundle results) {
ArrayList<String> userMessage;
userMessage = results.getStringArrayList(RESULTS_RECOGNITION);
PushNewDelivery(userMessage);
}
#Override
public void onPartialResults(Bundle partialResults) {
//do nothing
}
#Override
public void onEvent(int eventType, Bundle params) {
//do nothing
}
};
mDeliverySpeech.setRecognitionListener(newDeliveryRecognitionListener);
}
private void ListenForNewMessage()
{
if (mDeliverySpeech.isRecognitionAvailable(this) && !mListening)
{
Log.d("SpeechListening","started listening hopefully");
mListening = true;
mDeliverySpeech.startListening(mSpeechIntent);
new CountDownTimer(5000, 5000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
mDeliverySpeech.stopListening();
mListening = false;
}
}.start();
}
}
Do not forget to properly handle the activity life cycle when working with listener to avoid memory leak.

can't implement android.speech into surfaceview

i'm trying to implement
android.speech
in a surface view but i can't figure out how can i use
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
inside a case
MotionEvent.ACTION_DOWN:
any help trough this will be appreciated n.n
i already saw this post "Speech Recognition in Surface View [android]" but it doesn't completely answer my question u.u
You can write your own RecognitionListener implementation.
Here's an example of an Activity that recognizes speech immediately on startup. If you are doing this inside a SurfaceView, you'll need to pass the SurfaceView's context in when you create the speech recognizer.
public class BackgroundListenerTest extends Activity implements RecognitionListener {
/** Text display */
private TextView blurb;
/** Parameters for recognition */
private Intent recognizerIntent;
/** The ear */
private SpeechRecognizer recognizer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speech);
blurb = (TextView) findViewById(R.id.blurb);
recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.example.yourpackage");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
recognizer.startListening(recognizerIntent);
}
#Override
public void onBeginningOfSpeech() {
blurb.append("[");
}
#Override
public void onBufferReceived(byte[] arg0) {
}
#Override
public void onEndOfSpeech() {
blurb.append("] ");
}
#Override
public void onError(int arg0) {
}
#Override
public void onEvent(int arg0,
Bundle arg1) {
}
#Override
public void onPartialResults(Bundle arg0) {
}
#Override
public void onReadyForSpeech(Bundle arg0) {
blurb.append("> ");
}
#Override
public void onResults(Bundle bundle) {
ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
blurb.append(results.toString() + "\n");
recognizer.startListening(recognizerIntent);
}
#Override
public void onRmsChanged(float arg0) {
}
}

Use Android Speech Recognition so that it stops only at the press of a button

I want to use speech recognition to record until you press the stop button .
Do you know any way to do this?
I tried a solution but it only works as long as you do not take breaks too long(4-5s).
If you press the STOP button when it stops then starts again to work...
How can I correct it?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView) findViewById(R.id.STT);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizer = SpeechRecognizer
.createSpeechRecognizer(getApplicationContext());
recognizer.setRecognitionListener(this);
}
public void start(View view) {
recognizer.startListening(intent);
}
public void stop(View view) {
recognizer.stopListening();
}
#Override
public void onResults(Bundle res) {
ArrayList<String> results = res
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
myText.setText(results.get(0));
// ...
recognizer.startListening(intent);
}
#Override
public void onRmsChanged(float rmsdB) {
//...
}
#Override
public void onReadyForSpeech(Bundle params) {
//...
}
#Override
public void onPartialResults(Bundle data) {
//...
}
#Override
public void onEvent(int eventType, Bundle params) {
//...
}
#Override
public void onError(int error) {
//...
}
#Override
public void onEndOfSpeech() {
//...
}
#Override
public void onBufferReceived(byte[] buffer) {
//...
}
#Override
public void onBeginningOfSpeech() {
//...
}
This is a JB problem maybe by design. For a work around you can implement the voice recognition in a service and then send update to your UI based on the results. For an implementation of service work around see Android Speech Recognition as a service on Android 4.1 & 4.2

Android speech To Text Handling

in softkeyboard i have the option for speech to text , when i spoke it show a list of suggestion , when i select a text ,i need to fill my editText with this text, how can i done this i have see SpeechRecognizer class ,i don't know how can i use this ,please help me
SpeechRecognizer rec=SpeechRecognizer.createSpeechRecognizer(context);
RecognitionListener listener = new RecognitionListener() {
#Override
public void onRmsChanged(float rmsdB) {
}
#Override
public void onResults(Bundle results) {
ArrayList<String> voiceResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onPartialResults(Bundle partialResults) {
}
#Override
public void onEvent(int eventType, Bundle params) {
}
#Override
public void onError(int error) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onBufferReceived(byte[] buffer) {
}
#Override
public void onBeginningOfSpeech() {
}
};
rec.setRecognitionListener(listener);
Assuming your text edit is named "te":
public void onResults(Bundle results) {
ArrayList<String> voiceResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
StringBuilder sb = new StringBuilder();
for(String p: voiceResults) {
sb.append(p);
sb.append("\n"); } te.setText(sb.toString());
}
Normally, you are only interested in the first result (i.e voiceResults (0)) since that is the most probable match but the code above shows all of them so you can see what is returned.

Categories

Resources