android 实现语音识别效果

      前段时间,在google官方,看语音识别这里, 觉得挺有意思的,所以自己写了一个小小的例子,和大家一起分享!注意如果手机的网络没有开启,就无法实现识别声音的!所以一定要开启手机的网络,如果手机不存在语音识别功能的话,就无法启用识别!

 

下面是activity中的代码:

package com.zhangke.spring.sky.yuyin;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
/**
 * android语言识别功能
 * 特别注意:如果手机有语言设别功能,请开启网络,因为系统会根据你的声音数据到google云端获取声音数据
 * @author spring sky
 * Email vipa1888@163.com
 * QQ:840950105
 * My name :石明政
 *
 */
public class MainActivity extends Activity implements OnClickListener{
	private Button btn ;
	private ListView listView;
	private static final int REQUEST_CODE = 1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btn = (Button) this.findViewById(R.id.btn);
        listView = (ListView) this.findViewById(R.id.listview);
        
        /**
         * 下面是判断当前手机是否支持语音识别功能
         */
        PackageManager pm = getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if(list.size()!=0)
        {
        	btn.setOnClickListener(this);
        }else{
        	btn.setEnabled(false);
        	btn.setText("当前语音识别设备不可用...");
        }
        
    }

	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.btn)
		{
			/**
			 * 启动手机内置的语言识别功能
			 */
			Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  //设置为当前手机的语言类型
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "请说话,我识别");//出现语言识别界面上面需要显示的提示
			startActivityForResult(intent,REQUEST_CODE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		/**
		 * 回调获取从谷歌得到的数据
		 */
		if(requestCode==REQUEST_CODE&&resultCode==RESULT_OK)
		{
			
			List<String> list = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list)); //把数据显示在listview中
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	
}


layout中的代码:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="实现语音识别效果"
	    android:id="@+id/tvTitle"
    />
    <Button  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="实现语音识别效果"
	    android:id="@+id/btn"
    />
      <ListView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:id="@+id/listview"
    />
</LinearLayout>

 

这个功能不需要任何权限,只要确保手机有网络就可以了!

我也只是一个初学者,和大家一起分享!  微笑

   

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
Android实现语音识别可以使用Android提供的SpeechRecognizer类。下面是一个基本的示例代码: 1. 添加权限 在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> ``` 2. 实现语音识别 在需要实现语音识别的Activity中,实例化SpeechRecognizer类,并调用startListening()方法开启语音识别: ```java public class MainActivity extends AppCompatActivity { private SpeechRecognizer speechRecognizer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); speechRecognizer.setRecognitionListener(new RecognitionListener() { @Override public void onReadyForSpeech(Bundle params) { } @Override public void onBeginningOfSpeech() { } @Override public void onRmsChanged(float rmsdB) { } @Override public void onBufferReceived(byte[] buffer) { } @Override public void onEndOfSpeech() { } @Override public void onError(int error) { } @Override public void onResults(Bundle results) { ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (matches != null) { String result = matches.get(0); Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); } } @Override public void onPartialResults(Bundle partialResults) { } @Override public void onEvent(int eventType, Bundle params) { } }); Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something"); speechRecognizer.startListening(intent); } @Override protected void onDestroy() { super.onDestroy(); if (speechRecognizer != null) { speechRecognizer.destroy(); } } } ``` 在上面的代码中,我们首先实例化SpeechRecognizer类,并设置RecognitionListener监听器。在onResults()方法中,我们可以获取到语音识别的结果,并将其显示在Toast中。最后,我们使用Intent启动语音识别,并在startListening()方法中传入intent参数。 需要注意的是,在Activity销毁时,应该调用speechRecognizer.destroy()方法,以释放资源。 以上是一个基本的语音识别示例,你可以根据实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值