【问题标题】:Speech to text in emulator: No Activity found to handle Intent模拟器中的语音到文本:未找到处理意图的活动
【发布时间】:2011-12-17 07:05:54
【问题描述】:

我想问一下如何在我的模拟器上使用语音转文本代码。我的代码适用于真实设备,但不适用于模拟器。错误说:

 No Activity found to handle Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras) }

我能做什么?

【问题讨论】:

    标签: android speech-recognition speech-to-text


    【解决方案1】:
    package net.viralpatel.android.speechtotextdemo;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.os.Bundle;
    import android.speech.RecognizerIntent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.widget.Toast;
    
    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(),
                                "Ops! 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;
            }
    
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要在您的模拟器上安装一个应用程序,该应用程序包含一个处理RECOGNIZE_SPEECH-intent 的 Activity。您或许可以在网上找到 Google 的VoiceSearch.apk

      【讨论】:

      • 我安装了谷歌语音,但它也不起作用。如果您有工作 .apk 请提供 url。
      【解决方案3】:

      您无法使用模拟器测试某些内容。语音转文字是其中之一。

      我不确定这一点,但你不能在模拟器中使用这个 android 功能。

      无论如何,您应该使用 try/catch 处理此异常,并向用户提供一些反馈。

      您可以检查当前运行您的应用的设备中是否有 Activity,执行以下操作:

              PackageManager pm = context.getPackageManager();
              List<ResolveInfo> infoList = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
              if (infoList.size() == 0) {
                   /** Show some feedback to user if there is the activity. Something like "Your device is not abl to run this feature..."*/
              }else{
                   /**Your current code goes here.*/
              }
      

      如果有帮助,请告诉我。

      【讨论】:

        【解决方案4】:

        您需要在没有语音识别活动的目标设备上安装com.google.android.voicesearch 应用程序,例如:

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.voicesearch"));
        startActivity(browserIntent);
        

        如果您尝试安装 Google 的搜索应用程序 - 它不会有帮助,因为它内部不包含 VR 引擎,因此它会尝试执行相同的操作 - 安装 com.google.android.voicesearch 应用程序但它可能由于错误而失败在包名中(pname:com.google.android.voicesearch 而不仅仅是纯包名)。但是,com.google.android.voicesearch 可能由于“在您的国家/地区不可用”而无法安装。

        【讨论】:

          【解决方案5】:

          您可能需要虚拟 SD 卡。可以参考here

          【讨论】:

          猜你喜欢
          • 2011-05-15
          • 1970-01-01
          • 1970-01-01
          • 2021-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多