Convert speech to text in Android

Hi, I need help converting speech to text in android, and now I start looking for a topic for an hour, and every help I find shows me how to convert text to speech, and not vice versa

http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html

the two links above also show me how to convert text to speech, im basically creating an application that records everything that the user looks in and then converts it to text, im has problems with audio conversion

please tell me if it’s possible, and if so, you can give me a link,

+4
source share
3 answers

The user uses RecognizerIntent to use speech input in your application. You can see the sample VoiceRecognition code from Google.

+1
source

This is the code

    public class MainActivity extends Activity {

    protected static final int RESULT_SPEECH = 1;

    private ImageButton btnSpeak;
    private TextView txtText;

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

        txtText = (TextView) findViewById(R.id.txtText);

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

        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    txtText.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "Opps! Your device doesn't support Speech to Text",
                            Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

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

        switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                txtText.setText(text.get(0));
            }
            break;
        }

        }
    }
}
0
source

.

https://github.com/MaryamAzhdari/speechToTextMultiLanguage

:

imv_arabic.setOnClickListener{
        val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
        intent.putExtra(
            RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
        )
        //For some county you can use both of the below lines
        //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.FRANCE)
        //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "fr-FR")//French (France)

        //Some countries not define in Locale
        //You can use this page for finding your language
        //https://cloud.google.com/speech-to-text/docs/languages
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-AE")//Arabic (United Arab Emirates)
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Need to speak")

        try {
            startActivityForResult(intent, REQ_CODE)
        } catch (a: ActivityNotFoundException) {
            Toast.makeText(
                applicationContext,
                "Sorry! Your device not supported",
                Toast.LENGTH_SHORT
            ).show()
        }
    }


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if(requestCode==REQ_CODE){
        if (resultCode == RESULT_OK && data!=null) {
            val result = data
                .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
            tv_result?.text=result[0].toString()
        }
    }
}

:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

Android 6:

  private fun setupPermissions() {
    val permission = ContextCompat.checkSelfPermission(this,
        Manifest.permission.RECORD_AUDIO)

    if (permission != PackageManager.PERMISSION_GRANTED) {
        //Log.i(Tag, "Permission to record denied")
    }
}

:

https://cloud.google.com/speech-to-text/docs/languages

0

All Articles