语音朗读在android中不起作用

时间:2018-07-14 16:12:41

标签: android speech-to-text

我正在开发一个应用程序,其中我想使用RecognizerIntent将语音转换为文本并将其放入TextView中。我单击了一个ImageButton,函数startVoiceInput()运行,它应该执行该操作。但是在单击按钮时,什么也没有发生。这是我用于activity_main.xml

的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.softglobe.speechtotext.MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/btnSpeakContainer"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="20dp"
    android:padding="20dp">

    <TextView
        android:id="@+id/voiceInput"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</ScrollView>

<LinearLayout
    android:id="@+id/btnSpeakContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#f5f5f5"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="20dp">

    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:padding="16dp"
        android:scaleType="fitCenter"
        android:src="@mipmap/ic_microphone_2" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnSpeak"
        android:layout_margin="10dp"
        android:text="@string/hint" />
  </LinearLayout>
</RelativeLayout>

这是MainActivity.java代码

package net.softglobe.speechtotext;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private static final int REQ_CODE_SPEECH_INPUT = 100;
private TextView mVoiceInputTv;
private ImageButton mSpeakBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mVoiceInputTv = (TextView) findViewById(R.id.voiceInput);
    mSpeakBtn = (ImageButton) findViewById(R.id.btnSpeak);
    mSpeakBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startVoiceInput();
        }
    });
}

private void startVoiceInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, How can I help 
you?");
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> result = 
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                mVoiceInputTv.setText(result.get(0));
            }
            break;
        }

    }
}
}

当我使用API​​ 25 android Naugat在模拟器上运行此代码时,它可以工作。但是,当我在棒棒糖设备上运行它时,它什么也没做。可能是什么问题?

0 个答案:

没有答案