识别听众不听

时间:2015-09-22 06:52:06

标签: android service speech-recognition speech-to-text

我创建了这个服务,在后台监听,职业服务,但不知道听众是否听,但只在服务启动时显示在Logcat消息中...我已经在stackoberflow中读过其他问题但是什么都有在我的情况下运行。

table tr:nth-child(odd) {
background-color:silver;
}
table tr:nth-child(even) {
background-color:gold;
}

}

1 个答案:

答案 0 :(得分: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");
    }

}
如果您需要,

布局......

<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