繁体   English   中英

Android文本语音转换和语音文本转换

[英]Android Text to speech And Speech to text

我正在同时处理文本到语音和语音到文本。 我正在开发一个应用程序,其中它通过文本到语音来询问问题,并通过语音从用户那里得到答案,并且应用程序将其转换为文本。 但它不能正常工作。 两者都像说话一样在同时工作,然后发回短信。 我们可以给它一些延迟吗,以便当它停止说话时,它会听到声音并返回该文本。 `

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends Activity implementsTextToSpeech.OnInitListener {
    TextView eText1;
    TextToSpeech textToSpeech;
    String speech = "Hey, Can u read me?";
    private final int REQ_CODE_SPEECH_INPUT = 100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        eText1 = (TextView)findViewById(R.id.textView2);
        textToSpeech = new TextToSpeech(this,this);
    }
    @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);
                    eText1.setText(result.get(0));
                }
                break;
            }
        }
    }
    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_PROMPT, speech);
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void speakOut() {
        String text = speech;
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
    @Override
    public void onInit(int status) {
        if(status == TextToSpeech.SUCCESS){
            int result = textToSpeech.setLanguage(Locale.ENGLISH);
            if(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
                Toast.makeText(this, "This language is not supported", Toast.LENGTH_LONG).show();
            }
            else{
                speakOut();
                promptSpeechInput();
            }
        }else{
            Toast.makeText(this, "Initialization failed", Toast.LENGTH_LONG).show();
        }
    }
}

`

尝试更改:

speakOut();
promptSpeechInput();

promptSpeechInput();

然后添加:

speakOut();

eText1.setText(result.get(0));

完成获取文字后,应该说出文字

编辑:使用来确定语音何时结束。 只是在调用提示符时的提示SpeechInput()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM