调用语音识别应用程序的小部件

Posted

技术标签:

【中文标题】调用语音识别应用程序的小部件【英文标题】:Widget that calls speech recognition app 【发布时间】:2011-05-30 15:36:55 【问题描述】:

我正在尝试创建一个包含单个 ImageView 的小部件,单击该小部件会启动语音识别应用程序。我从来没有使用过小部件和待处理的意图,所以我很困惑:如何创建待处理的意图来启动语音识别活动?

我尝试过这样的事情,但它当然失败了:

意图意图 = new Intent(); 意图 voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 识别器意图.LANGUAGE_MODEL_FREE_FORM); voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, “语音识别演示”); voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT,voiceIntent); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 意图,0); RemoteViews 视图 = 新 RemoteViews(context.getPackageName(), R.layout.main); views.setOnClickPendingIntent(R.id.button, pendingIntent);

【问题讨论】:

我想从我的应用程序中获得小部件,当点击它时,它会开始显示 SOUND REORGANIZATION 对话框并继续应用程序。我读了你的答案,但很困惑!你能帮帮我吗? 【参考方案1】:

我明白了!我需要两个包裹在两个待处理意图中的常规意图,如下所示:

// this intent points to activity that should handle results
Intent activityIntent = new Intent(context, ResultsActivity.class);
// this intent wraps results activity intent
PendingIntent resultsPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);

// this intent calls the speech recognition
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, resultsPendingIntent);

// this intent wraps voice recognition intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, voiceIntent, 0);
rv.setOnClickPendingIntent(R.id.btn, pendingIntent);

【讨论】:

你能发布你的ResultsActivity的代码吗?我正在尝试实现类似的东西...... @IgorGanapolsky 我也很感兴趣,所以我发布了这个相关问题***.com/questions/32723249/…【参考方案2】:

我也遇到了同样的问题。 抱歉,我没有足够的声誉来发表评论。

无需使用透明 Activity 来发送识别意图。 喜欢zorglub76的回答

Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, resultsPendingIntent);

识别结果只会在resultingPendingIntent的额外部分中 所以你需要做的就是:

ResultsActivity.onCreate()

ArrayList<String> voiceResults = this.getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);

注意NullPointerException,你会从 ArrayList 中得到结果!!

【讨论】:

不适合我,请参阅***.com/questions/32723249/…【参考方案3】:

我想创建类似 google 的小部件。我尝试了 zorglub76 解决方案,但我无法得到声音结果...

我通过创建一个处理端到端语音识别的虚拟透明活动来解决这个问题。

它的工作原理如下:Widget->VoiceRecognitionStarterActivity->RecognizerIntent->VoiceRecognitionStarterActivity.onActivityResult。

我的小部件类:

public class MyWidgetProvider extends AppWidgetProvider 

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) 

    // Get all ids
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) 
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        Intent activityIntent = new Intent(context, VoiceRecognitionStarterActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.mic_image, pendingIntent);

        activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(context.getString(R.string.search_url)));
        pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.search_box_image, pendingIntent);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);

    
    

我的透明活动:

   public class VoiceRecognitionStarterActivity extends Activity

    private static final String TAG = "VoiceRecognitionStarterActivity";
    private int SPEECH_REQUEST_CODE = 1;

    @Override

public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    sendRecognizeIntent();


private void sendRecognizeIntent()

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to search");
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
    startActivityForResult(intent, SPEECH_REQUEST_CODE);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)

    if (requestCode == SPEECH_REQUEST_CODE)
    
        if (resultCode == RESULT_OK) 
            Log.d(TAG, "result ok");
            Intent searchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.search_url)));
            startActivity(searchIntent);
            finish();   
          else 
            Log.d(TAG, "result NOT ok");
            finish();
        

    

    super.onActivityResult(requestCode, resultCode, data);
    


要使活动透明,请参阅this 帖子

【讨论】:

【参考方案4】:

这是功能齐全的,它基于 android SDK 中的 ListView 小部件。它不是特别适用于小部件,但我相信您可以对其进行修改,使其适用于小部件。

创建一个名为 SearchActivity 的活动:

// CustomSearch (View) & ISearch (Interface) are objects that I created and are irrelevant
public class SearchActivity extends AppCompatActivity implements ISearch

    // Variables
    private CustomSearch mSearchView;


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

        mSearchView = (CustomSearch)findViewById(R.id.search);
        mSearchView.setPendingComponentName(getComponentName());
        mSearchView.setSearchListener(this);
    

    @Override
    protected void onNewIntent(Intent intent)
    
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        
            String query = intent.getStringExtra(SearchManager.QUERY);
            Log.i("SEARCH >", "You said: " + query);
        
    

将活动添加到 AndroidManifest.xml

<activity
    android:name=".activities.SearchActivity"
    android:label="@string/app_name"
    android:theme="@style/CustomTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH"/>
    </intent-filter>
</activity>

在您的自定义小部件/视图中:

buttonVoice.setOnClickListener(new View.OnClickListener() 

    @Override
    public void onClick(View v)
    
        // Get activity from either SearchableInfo or ComponentName
        ComponentName searchActivity = mComponentName;

        // Wrap component in intent
        Intent queryIntent = new Intent(Intent.ACTION_SEARCH);
        queryIntent.setComponent(searchActivity);

        // Wrap query intent in pending intent
        PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent, PendingIntent.FLAG_ONE_SHOT);

        // Create bundle now because if we wrap it in pending intent, it becomes immutable
        Bundle queryExtras = new Bundle();

        // Create voice intent
        Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZER_SPEECH);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak");
        voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, searchActivity
        voiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Wrap the pending intent & bundle inside the voice intent
        voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras);

        // Start the voice search
        getContext().startActivity(voiceIntent);
    

【讨论】:

以上是关于调用语音识别应用程序的小部件的主要内容,如果未能解决你的问题,请参考以下文章

树莓派怎么调用科大讯飞的语音库实现语音识别

测试语音识别+麦克风效果

python调用百度语音(语音识别-斗地主语音记牌器)

android如何自定义语音识别

Python使用websocket调用语音识别,语音转文字

如何用python调用百度语音识别