Android开发之语音识别

2.调用Google原生语音识别

@Override

public void onClick(View v) {

//开启语音识别功能

Intent intent = new Intent(

RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

//设置模式,这里设置成自由模式

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

//提示语音开始文字

intent.putExtra(RecognizerIntent.EXTRA_PROMPT,“Please start your voice”);

//开始进行语音识别,这里先检测手机(模拟器)是否支持语音识别并且捕获异常

try {

startActivityForResult(intent, RESULT_SPEECH);

txtText.setText(“”);

} catch (ActivityNotFoundException a) {

Toast t = Toast.makeText(getApplicationContext(),

“Opps! Your device doesn’t support Speech to Text”,

Toast.LENGTH_SHORT);

t.show();

}

}

});

3.使用onActivityResult接收返回的结果

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

case RESULT_SPEECH: {

if (resultCode == RESULT_OK && data != null) {

ArrayList text = data

.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

//这里集合列表中第一个值为匹配度最高的值

txtText.setText(text.get(0));

}

break;

}

}

}

完整代码


1.MainActivity.java代码

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.content.ActivityNotFoundException;

import android.content.Intent;

import android.speech.RecognizerIntent;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

import android.widget.ImageButton;

import android.widget.TextView;

import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

protected static final int RESULT_SPEECH = 1;

private ImageButton btnSpeak;

private EditText txtText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

txtText = 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”);

//提示语音开始文字

intent.putExtra(RecognizerIntent.EXTRA_PROMPT,“Please start your voice”);

//开始进行语音识别,这里先检测手机(模拟器)是否支持语音识别并且捕获异常

try {

startActivityForResult(intent, RESULT_SPEECH);

txtText.setText(“”);

} catch (ActivityNotFoundException a) {

Toast t = Toast.makeText(getApplicationContext(),

“Opps! Your device doesn’t support Speech to Text”,

Toast.LENGTH_SHORT);

t.show();

}

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

case RESULT_SPEECH: {

if (resultCode == RESULT_OK && data != null) {

ArrayList text = data

.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

//这里集合列表中第一个值为匹配度最高的值

txtText.setText(text.get(0));

}

break;

}

}

}

}

2.activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:layout_toLeftOf=“@+id/txtText”

android:gravity=“center”

android:orientation=“vertical”>

<EditText

android:id=“@+id/txtText”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:layout_gravity=“left”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“10dp”

android:layout_marginTop=“10dp”

android:hint=“@string/edit”/>

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

开发是面向对象。我们找工作应该更多是面向面试。哪怕进大厂真的只是去宁螺丝,但你要进去得先学会面试的时候造飞机不是么?

作者13年java转Android开发,在小厂待过,也去过华为,OPPO等,去年四月份进了阿里一直到现在。等大厂待过也面试过很多人。深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

960页全网最全Android开发笔记

资料太多,全部展示会影响篇幅,暂时就先列举这些部分截图

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

[外链图片转存中…(img-lyTAly4V-1713252979674)]

[外链图片转存中…(img-LL9cKRIF-1713252979675)]

资料太多,全部展示会影响篇幅,暂时就先列举这些部分截图

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值