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

语音识别游戏开发技术疑问咨询(附相关代码)

解答你的语音识别游戏技术疑问

嘿,我来帮你一步步解决这三个问题,结合你现有的代码给你具体的实现方案:

1. 是否必须为词汇类别创建数据库?

其实不是必须的,分两种场景来看:

  • 如果你的游戏只是固定的少量指令-答案对(比如目前的A/B/C/D几个字母),直接用HashMap或者静态数组就能搞定,不用折腾数据库,开发效率更高;
  • 但如果后续你想扩展大量词汇、支持动态添加/删除指令,或者需要多类别(比如字母、数字、词语)的持久化管理,那数据库会是更合适的选择,能让数据管理更灵活。

2. 如何实现"单指令对应单答案"的判定逻辑?

核心思路是:先保存当前显示的指令对应的正确答案,再把语音识别结果和这个正确答案做对比。结合你的MainGame类,具体修改如下:

步骤1:在MainGame中添加变量保存当前正确答案

public class MainGame extends AppCompatActivity { 
    private TextView jawabanTxt; 
    private String textA; 
    TextView textView; 
    // 新增:保存当前指令对应的正确答案
    private String currentCorrectAnswer;
    // ... 其余原有代码不变

步骤2:修改HurufActivity,传递指令和正确答案

让按钮点击时把对应指令和答案传给MainGame:

aBtn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
        Intent intent = new Intent(HurufActivity.this, MainGame.class);
        // 传递指令文本和正确答案
        intent.putExtra("INSTRUCTION", "说出A");
        intent.putExtra("CORRECT_ANSWER", "A");
        startActivity(intent); 
    } 
}); 

bBtn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
        Intent intent = new Intent(HurufActivity.this, MainGame.class);
        intent.putExtra("INSTRUCTION", "说出B");
        intent.putExtra("CORRECT_ANSWER", "B");
        startActivity(intent); 
    } 
}); 

步骤3:在MainGame中接收并显示指令

在onCreate里拿到传递过来的数据,显示指令并保存正确答案:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_game_act); 
    jawabanTxt = (TextView) findViewById(R.id.jawabanTxt); 
    textView = (TextView) findViewById(R.id.textView);

    // 接收从HurufActivity传来的数据
    Intent intent = getIntent();
    String instruction = intent.getStringExtra("INSTRUCTION");
    currentCorrectAnswer = intent.getStringExtra("CORRECT_ANSWER");
    // 显示指令到textView
    textView.setText(instruction);
} 

步骤4:添加判定逻辑到onActivityResult

拿到语音结果后和正确答案对比,给出反馈:

protected void onActivityResult(int requestCode,int resultCode, Intent data){ 
    super.onActivityResult(requestCode, resultCode,data); 
    switch (requestCode){ 
        case 10: 
            if ((resultCode == RESULT_OK &&data != null)) { 
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
                String userAnswer = result.get(0);
                jawabanTxt.setText(userAnswer);

                // 判定逻辑:忽略大小写对比,适配不同发音习惯
                if(userAnswer.equalsIgnoreCase(currentCorrectAnswer)){
                    Toast.makeText(this, "回答正确!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "回答错误,正确答案是:"+currentCorrectAnswer, Toast.LENGTH_SHORT).show();
                }
            } 
            break; 
    } 
} 

3. 若必须创建数据库,应在Speech Recognition类(MainGame)的哪个位置实现?

如果确定要用数据库,首先要修改你的dbGame表结构(目前只有huruf字段,没法存储“指令-答案”的对应关系),再在MainGame中初始化并加载数据:

修改dbGame的表结构与查询方法

public class dbGame extends SQLiteOpenHelper { 
    private static final String DATABASE_NAME = "huruf.db"; 
    private static final int DATABASE_VERSION = 1; // 建议用int类型版本号

    public dbGame(Context context){ 
        super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
        // 新增instruction字段存储指令,answer字段存储正确答案
        String sql = "create table game(id INTEGER PRIMARY KEY AUTOINCREMENT, instruction TEXT, answer TEXT);"; 
        db.execSQL(sql); 
        // 插入示例数据
        sql = "INSERT INTO game (instruction, answer) VALUES('说出A','A'),('说出B','B'),('说出C','C'),('说出D','D');"; 
        db.execSQL(sql); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { 
        // 版本升级时更新表结构
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS game");
        onCreate(sqLiteDatabase);
    } 

    // 新增方法:查询所有指令-答案对
    public HashMap<String, String> getAllInstructionAnswerPairs(){
        HashMap<String, String> pairs = new HashMap<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT instruction, answer FROM game", null);
        if(cursor.moveToFirst()){
            do{
                String instruction = cursor.getString(cursor.getColumnIndexOrThrow("instruction"));
                String answer = cursor.getString(cursor.getColumnIndexOrThrow("answer"));
                pairs.put(instruction, answer);
            }while(cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return pairs;
    }
}

在MainGame中初始化数据库并加载数据

在onCreate方法中完成数据库初始化,加载指令-答案对:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_game_act); 
    jawabanTxt = (TextView) findViewById(R.id.jawabanTxt); 
    textView = (TextView) findViewById(R.id.textView);

    // 初始化数据库并加载所有指令-答案对
    dbGame gameDb = new dbGame(this);
    HashMap<String, String> instructionPairs = gameDb.getAllInstructionAnswerPairs();

    // 示例:随机选一个指令显示(也可以根据传递的类别筛选)
    String randomInstruction = instructionPairs.keySet().iterator().next();
    currentCorrectAnswer = instructionPairs.get(randomInstruction);
    textView.setText(randomInstruction);
} 

后续的判定逻辑和之前一致,只是正确答案从数据库加载而来。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:05:16