2015-09-22 3 views
1

Я создал эту услугу, чтобы послушать в фоновом режиме, карьерный сервис, но не знаю, прослушивает ли слушатель или нет, но показывается только в сообщении Logcat, когда начинается сервис ... Я прочитал другие вопросы в stackoberflow, но что-то работает в моем случае.Признание слушателя не слушайте

public class ServicioVoz extends RecognitionService { 

private SpeechRecognizer sr; 
private VoiceResultsListener vrl; 
public Principal principal; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.i("Dins del servei", "Arrancat"); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Log.i("SimpleVoiceService", "Service stopped"); 
} 

@Override 
protected void onCancel(Callback listener) { 
    sr.cancel(); 
} 

@Override 
protected void onStartListening(Intent recognizerIntent, Callback listener) { 
    Log.e("escoltant", "________________________________"); 
    sr.setRecognitionListener(new VoiceResultsListener(listener)); 
    sr.startListening(recognizerIntent); 
} 

@Override 
protected void onStopListening(Callback listener) { 
    sr.stopListening(); 
    Log.e("Atura d'escoltar", "________________________________"); 
} 

/* INICIALIZAMOS UNA NUEVA CLASE*/ 
private class VoiceResultsListener implements RecognitionListener { 

    private Callback UserListener; 

    public VoiceResultsListener(Callback userSpecifiedListener) { 
     UserListener = userSpecifiedListener; 
    } 

    @Override 
    public void onBeginningOfSpeech() { 
     try { 
      UserListener.beginningOfSpeech(); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
      Log.e("ERROR", "No comença ha escoltar"); 
     } 
    } 

    @Override 
    public void onBufferReceived(byte[] buffer) { 
     try { 
      UserListener.bufferReceived(buffer); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onEndOfSpeech() { 
     try { 
      UserListener.endOfSpeech(); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onError(int error) { 
     try { 
      UserListener.error(error); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onEvent(int eventType, Bundle params) { 
     ; 
    } 

    @Override 
    public void onPartialResults(Bundle partialResults) { 
     try { 
      UserListener.partialResults(partialResults); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onReadyForSpeech(Bundle params) { 
     try { 
      UserListener.readyForSpeech(params); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onResults(Bundle results) { 
     Log.e("resultats", "nose que possar"); 
     try { 
      UserListener.results(results); 
      ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
      String text = ""; 
      for (String result : matches) 
       text += result + "\n"; 

      Log.e("", text); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onRmsChanged(float rmsdB) { 
     try { 
      UserListener.rmsChanged(rmsdB); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

}

ответ

1

Преобразование голоса в текст с пользовательский интерфейс, используя ниже класса ..

import java.util.ArrayList; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.RecognitionListener; 
import android.speech.RecognizerIntent; 
import android.speech.SpeechRecognizer; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class SRActivity extends Activity implements RecognitionListener { 

    Context context; 
    private SpeechRecognizer speech; 

    ProgressDialog progressDialog; 
    String tag = getPackageName().toString(); 
    TextView textView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sr); 
     context = SRActivity.this; 
     Button speakBtn = (Button) findViewById(R.id.button1); 
     textView = (TextView) findViewById(R.id.text1); 
     speakBtn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       speech = SpeechRecognizer 
         .createSpeechRecognizer(SRActivity.this); 
       speech.setRecognitionListener(SRActivity.this); 

       Intent intent = new Intent(
         RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, 
         "en"); 
       intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, 
         SRActivity.this.getPackageName()); 

       // workin fine 
       intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); 

       // use judiciously! Additionally, depending on the recognizer 
       // implementation, these values may have no effect. 
       intent.putExtra(
         RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 
         3000L); 

       // use judiciously! Additionally, depending on the recognizer 
       // implementation, these values may have no effect. 
       intent.putExtra(
         RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 
         5000L); 

       speech.startListening(intent); 
       progressDialog = new ProgressDialog(context); 
       progressDialog.show(); 
       progressDialog.setMessage("My Custom Dialog here"); 
      } 
     }); 

     Button button2 = (Button) findViewById(R.id.button2); 
     button2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // Stop Listning 
       speech.stopListening(); 

      } 
     }); 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.speech.RecognitionListener#onBeginningOfSpeech() 
    */ 
    @Override 
    public void onBeginningOfSpeech() { 
     Log.e(tag, "onBeginningOfSpeech"); 

    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.speech.RecognitionListener#onBufferReceived(byte[]) 
    */ 
    @Override 
    public void onBufferReceived(byte[] arg0) { 
     // Log.e(tag, "onBufferReceived"); 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.speech.RecognitionListener#onEndOfSpeech() 
    */ 
    @Override 
    public void onEndOfSpeech() { 
     progressDialog.dismiss(); 

     Log.e(tag, "onEndOfSpeech"); 

    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.speech.RecognitionListener#onError(int) 
    */ 
    @Override 
    public void onError(int error) { 

     String mError = ""; 
     switch (error) { 
     case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: 
      mError = " network timeout"; 
      break; 
     case SpeechRecognizer.ERROR_NETWORK: 
      mError = " network"; 
      return; 
     case SpeechRecognizer.ERROR_AUDIO: 
      mError = " audio"; 
      break; 
     case SpeechRecognizer.ERROR_SERVER: 
      mError = " server"; 
      break; 
     case SpeechRecognizer.ERROR_CLIENT: 
      mError = " client"; 
      break; 
     case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: 
      mError = " speech time out"; 
      break; 
     case SpeechRecognizer.ERROR_NO_MATCH: 
      mError = " no match"; 
      break; 
     case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: 
      mError = " recogniser busy"; 
      break; 
     case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: 
      mError = " insufficient permissions"; 
      break; 

     } 
     textView.setText(mError); 
     Log.e(tag, "onError " + mError); 

    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.speech.RecognitionListener#onEvent(int, android.os.Bundle) 
    */ 
    @Override 
    public void onEvent(int eventType, Bundle params) { 
     Log.e(tag, "onEvent"); 

    } 

    /* 
    * 
    */ 
    @Override 
    public void onPartialResults(Bundle partialResults) { 
     Log.e(tag, "onPartialResults"); 

    } 

    /* 
    * Called when the endpointer is ready for the user to start speaking. 
    */ 
    @Override 
    public void onReadyForSpeech(Bundle params) { 
     Log.e(tag, "onReadyForSpeech"); 
    } 

    /* 
    * Called when recognition results are ready. 
    */ 
    @Override 
    public void onResults(Bundle results) { 
     Log.e(tag, "onResults"); 
     ArrayList<String> matches = results 
       .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     textView.setText("" + matches.get(0)); 
     Log.e(tag, "onResults" + matches.toString()); 

    } 

    /* 
    * The sound level in the audio stream has changed. There is no guarantee 
    * that this method will be called. 
    */ 
    @Override 
    public void onRmsChanged(float rmsdB) { 
     // Log.e(tag, "onRmsChanged"); 
    } 

} 

Layout, если вам нужно ...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="28dp" 
     android:text="Speak Results" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:text="3 Sec delay Silence before auto stop, 5 Sec Minimum length of recording " /> 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="42dp" 
     android:text="Speak" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="93dp" 
     android:text="Stop Speak" /> 

</RelativeLayout> 

Предоставлено:https://github.com/Mohammed-khurram-Ahmed/SpeechToTextWithoutDefaultUI

+0

Спасибо, но я хочу в службе. – Gannon

Смежные вопросы