我的Android应用程序中的离线语音识别

时间:2016-02-06 12:54:39

标签: speech-to-text

我正在尝试在我的Android应用中使用谷歌离线语音识别,它只是将语音转换为文本,但它不起作用。我已经下载了离线语言。相同的应用程序可以正常使用互联网连接,但不能在离线模式下工作,主要代码如下:

主要代码



package com.example.parth.texttospeech;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    TextView resultTEXT;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onButtonClick(View v)
    {
        if(v.getId()==R.id.bttexttospeech)
        {
            resultTEXT=(TextView)findViewById(R.id.tvresult);
            prompSpeechInput();
        }
    }
    public void prompSpeechInput()
    {
        Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
       i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,Locale.getDefault());
        i.putExtra(RecognizerIntent.EXTRA_PROMPT,"say something");
        try{
            startActivityForResult(i,100);

        }catch(ActivityNotFoundException e)
        {
            Toast.makeText(getApplicationContext(),"sorry device doesnt support text to speech",Toast.LENGTH_LONG).show();
        }
    }

    public void onActivityResult(int request_code, int result_code, Intent i) {
        super.onActivityResult(request_code,result_code,i);
        {
            switch(request_code)
            {
                case 100:if(result_code==RESULT_OK && i!=null)
                {
                    ArrayList<String> result=i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    resultTEXT.setText(result.get(0));
                    break;
                }
            }
        }
    }


}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

要在离线模式下工作,语言包必须在那里。下载并更新所有软件包。

此外,您可能想添加intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,15);在你的代码中。

相关问题