You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android原生/React Native语音识别触发任务及语音计数Counter app开发咨询

1. Android Native/React Native中监听用户语句并识别匹配执行任务

1.1 Android Native 实现方案

  • 权限配置
    先在AndroidManifest.xml中添加必要权限:
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" /> <!-- 云端识别需此权限 -->
    
  • 实时语音识别与匹配
    用系统自带的SpeechRecognizer实现基础识别逻辑:
    SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(context);
    recognizer.setRecognitionListener(new RecognitionListener() {
        @Override
        public void onResults(Bundle results) {
            ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            if (matches != null && !matches.isEmpty()) {
                String recognizedText = matches.get(0);
                // 匹配目标语句并执行任务
                if (recognizedText.equalsIgnoreCase("打开相机")) {
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivity(cameraIntent);
                }
            }
        }
    
        // 实现其他必填回调(onReadyForSpeech、onError等)
    });
    
    // 启动识别
    Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    recognizer.startListening(recognizerIntent);
    
  • 持续监听优化
    在onResults或onError回调中重新启动识别,实现持续监听,注意在页面销毁时调用recognizer.destroy()释放资源。
  • 离线识别替代
    若无需云端支持,可集成PocketSphinx等离线语音库,将目标语句的关键词模型打包进应用,实现本地匹配。

1.2 React Native 实现方案

  • 依赖安装
    使用@react-native-voice/voice库处理语音识别:
    npm install @react-native-voice/voice --save
    
    同时在iOS的Info.plist和Android的AndroidManifest.xml中配置对应录音权限。
  • 核心代码实现
    import Voice from '@react-native-voice/voice';
    import { useEffect, useState } from 'react';
    
    const VoiceListener = () => {
        useEffect(() => {
            Voice.onSpeechResults = handleSpeechResults;
            Voice.onSpeechError = handleSpeechError;
    
            // 请求权限并启动识别
            Voice.requestPermissions().then(() => Voice.start('zh-CN'));
    
            // 清理资源
            return () => {
                Voice.destroy().then(Voice.removeAllListeners);
            };
        }, []);
    
        const handleSpeechResults = (event) => {
            const recognizedText = event.value[0];
            if (recognizedText.toLowerCase() === "启动任务") {
                // 执行指定任务
                console.log("任务已启动");
            }
        };
    
        const handleSpeechError = () => {
            // 出错后重启识别
            Voice.start('zh-CN');
        };
    
        return null;
    };
    

2. 语音触发式Counter应用开发思路

核心流程

用户录制目标语句 → 应用持续监听语音 → 匹配到目标语句自动计数

具体实现步骤

步骤1:录音功能实现

  • Android Native
    用MediaRecorder实现录音并保存本地:
    MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    String savePath = getExternalFilesDir(null) + "/target_voice.mp4";
    recorder.setOutputFile(savePath);
    recorder.prepare();
    recorder.start();
    // 停止录音时调用recorder.stop()和recorder.release()
    
  • React Native
    使用react-native-audio-recorder-player库:
    npm install react-native-audio-recorder-player --save
    
    代码示例:
    import AudioRecorderPlayer from 'react-native-audio-recorder-player';
    const audioRecorderPlayer = new AudioRecorderPlayer();
    const [targetVoicePath, setTargetVoicePath] = useState('');
    
    const startRecording = async () => {
        const path = await audioRecorderPlayer.startRecorder();
        console.log("录音路径:", path);
    };
    
    const stopRecording = async () => {
        const result = await audioRecorderPlayer.stopRecorder();
        setTargetVoicePath(result);
    };
    

步骤2:目标语句校准

将录制的音频转成文本(用上述语音识别API),保存为目标匹配文本——这种方式实现简单,适合短句匹配;若需更高准确率,可提取音频MFCC特征,用余弦相似度对比实时音频与录制音频的特征(需借助音频处理库实现)。

步骤3:实时监听与计数更新

沿用语音监听逻辑,匹配到目标文本时更新计数,添加防抖机制避免重复触发:

const [count, setCount] = useState(0);
const [lastTriggerTime, setLastTriggerTime] = useState(0);
const COOLDOWN_TIME = 1000; // 1秒冷却

const handleSpeechResults = (event) => {
    const recognizedText = event.value[0];
    const now = Date.now();
    if (recognizedText === targetText && now - lastTriggerTime > COOLDOWN_TIME) {
        setCount(prev => prev + 1);
        setLastTriggerTime(now);
    }
};

步骤4:UI设计

  • 核心元素:当前计数显示、录音按钮(设置目标语句)、监听启停按钮
  • 状态提示:显示“正在监听”“匹配成功”等实时状态

内容的提问来源于stack exchange,提问作者InfiniteForLoop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 11:03:26