首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >语音识别器在android通话过程中将语音转换为文本

语音识别器在android通话过程中将语音转换为文本
EN

Stack Overflow用户
提问于 2018-05-28 19:32:17
回答 1查看 834关注 0票数 2

我一直在寻找一种解决方案,可以让我在通话过程中将语音转换为文本。我想要将说话者的语音转换为文本,以处理该数据以生成可行的输出,然后将该文本转换为语音,该语音将在通话期间通过麦克风发送给用户。

所有这些工作都应该在调用期间完成。

有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2018-05-28 19:36:34

试试这个..。

代码语言:javascript
复制
public class Speech extends AppCompatActivity {

private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;

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

    txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

    // hide the action bar
    getActionBar().hide();

    btnSpeak.setOnClickListener(new View.OnClickListener() {

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

}

/**
 * Showing google speech input dialog
 */
private void promptSpeechInput() {
    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,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

/**
 * Receiving speech input
 */
@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);
                txtSpeechInput.setText(result.get(0));
            }
            break;
        }

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    return true;
}

}

这段xml代码..

代码语言:javascript
复制
<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:background="@drawable/bg_gradient"
android:orientation="vertical" >

<TextView
    android:id="@+id/txtSpeechInput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:textColor="@color/white"
    android:textSize="26dp"
    android:textStyle="normal" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="60dp"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/ico_mic" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/tap_on_mic"
        android:textColor="@color/white"
        android:textSize="15dp"
        android:textStyle="normal" />
</LinearLayout>

</RelativeLayout>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50565449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档