模拟器中的语音到文本:找不到处理 Intent 的活动

发布于 2024-12-21 19:20:27 字数 194 浏览 2 评论 0原文

我想问如何在模拟器上使用语音转文本代码。我的代码可以在真实设备上运行,但不能在模拟器上运行。错误说:

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

我能做什么?

I want to ask how I can use speech to text code on my emulator. My codes work on real device but not work on emulator. The error said :

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

What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦毁影碎の 2024-12-28 19:20:27
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;
        }

        }
    }
}
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;
        }

        }
    }
}
迷鸟归林 2024-12-28 19:20:27

您需要在模拟器上安装一个包含处理 RECOGNIZE_SPEECH 意图的 Activity 的应用程序。您也许可以在网络上找到 Google 的 VoiceSearch.apk

You need to install onto your emulator an app that contains an Activity that handles the RECOGNIZE_SPEECH-intent. You might be able to find Google's VoiceSearch.apk on the web.

岁月染过的梦 2024-12-28 19:20:27

有些事情无法使用模拟器进行测试。语音转文本就是其中之一。

我不确定这一点,但你不能在模拟器上使用这个 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.*/
        }

如果有帮助,请告诉我。

There are certain things you can't test using an emulator. Speech to text is on of them.

I'm not sure about this, but you can't use this android feature with the emulator.

No matter what, you should handle this exception with a try/ catch an give some feedback to the user.

You can check if there is that Activity in current device running your app doing something like:

        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.*/
        }

Let me know if it helps.

如何视而不见 2024-12-28 19:20:27

您需要在没有语音识别活动的目标设备上安装 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 安装可能无法进行。

You need to install com.google.android.voicesearch application on target device which has no voice recognition activity like:

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

if you try to install Google's Search app - it won't help since it doesn't contain the VR engine inside and thus it will try to do the same - install com.google.android.voicesearch app but it could fail due to a bug in package name (pname:com.google.android.voicesearch instead of just pure package name). However com.google.android.voicesearch installation might be impossible due to "Not available in your country".

总攻大人 2024-12-28 19:20:27

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

You might need a virtual SD Card. You can refer here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文