基于Google的嵌入式系统android开发语音技术 语音搜索,语音朗读文章,语音控制

本文介绍了如何在Android系统上开发语音技术,包括语音识别和语音合成。通过TextToSpeech API实现文本转语音,并展示了语音识别的Intent使用,可用于语音控制和网络搜索。此外,还提到了Android语音搜索的实现,结合WebView实现语音搜索结果的网页加载。作者提供了源代码和参与的Google Android软件开发大赛的相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

               

android是Google推出的嵌入式操作系统,有着广泛的根基与基础。最近一个中关村的一个CEO朋友送了一个摩托罗拉android手机,想玩玩语音技术过把瘾。如何用android开发语音技术应用呢,本人下载的最新SDK,android-2.1_r01-windows,

废话少说,看看老夫一展身手,虽然我本人精通C/C++,.Net,但是玩起java也是一点都不含糊的,老夫我的android手机提前安装了

Linux开源语音识别引擎simon,本人已经完全移植成功,英文识别率非常高只比nuance差一点

Linux开源语音朗读引擎espeak, ,本人已经完全移植成功,英文朗读只比于科大讯飞差一点

先简单介绍下语音朗读技术

详细代码见注释

 

import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.util.Log;import android.view.View;import android.widget.Button;import com.example.android.apis.R;import java.util.Locale;import java.util.Random;public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {    private static final String TAG = "TextToSpeech";    private TextToSpeech mTts;    private Button mAgainButton;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.text_to_speech);        //初始化文本到语音。这是一个异步操作。        //OnInitListener(第二个参数)被调用初始化后完成。        mTts = new TextToSpeech(this,            this  // 创建接口初始化            );         //禁用按钮的布局。        //这将被启用后的TTS引擎的初始化        mAgainButton = (Button) findViewById(R.id.again_button);        mAgainButton.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                sayHello();            }        });    }    public void onDestroy() {        // 删除对象退出        if (mTts != null) {            mTts.stop();            mTts.shutdown();        }        super.onDestroy();    }    // 实现TextToSpeech.OnInitListener    public void onInit(int status) {        // 返回成功或者失败        if (status == TextToSpeech.SUCCESS) {            //设置首选语言为美国英语。            //注意语言可能无法使用,其结果将表明这一点。            int result = mTts.setLanguage(Locale.US);            // 定义结果 mTts.setLanguage(Locale.FRANCE);            if (result == TextToSpeech.LANG_MISSING_DATA ||                result == TextToSpeech.LANG_NOT_SUPPORTED) {               // 语言不受支持                Log.e(TAG, "Language is not available.");            } else {                //检查其他可能的结果代码文件。                //例如,语言可能是可用的语言环境,但不是指定的国家和变体。                //TTS引擎已成功初始化。                //允许用户按下按钮的应用程序再次发言。                mAgainButton.setEnabled(true);                // 开始调用                sayHello();            }        } else {            // Initialization failed.            Log.e(TAG, "Could not initialize TextToSpeech.");        }    }    private static final Random RANDOM = new Random();    private static final String[] HELLOS = {      "Hello",      "yincheng",      "ShanDongUniversity",      "microsoft",      "google is  great",      "My  name   is  yincheng!"    };    private void sayHello() {        // 随机选择朗读文本        int helloLength = HELLOS.length;        String hello = HELLOS[RANDOM.nextInt(helloLength)];        mTts.speak(hello,            TextToSpeech.QUEUE_FLUSH,             null);    }}

 

 

android语音识别是通过一个Intent的Action动作来完成的。主要有以下两种模式:

ACTION_RECOGNIZE_SPEECH:一般语音识别,主要用于语音控制。

ACTION_WEB_SEARCH:网络搜索识别,主要用于语音搜索

 

我们先来分析下语音控制,

import android.app.Activity;   import android.content.Intent;   import android.content.pm.PackageManager;   import android.content.pm.ResolveInfo;   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;   import java.util.ArrayList;   import java.util.List;   public class VoiceRecognition extends Activity implements OnClickListener {   private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;     private ListView mList;     public void onCreate(Bundle savedInstanceState)       {          super.onCreate(savedInstanceState);         setContentView(R.layout.voice_recognition);         Button speakButton = (Button) findViewById(R.id.btn_speak);         mList = (ListView) findViewById(R.id.list);          // 检查是否有语音识别活动     PackageManager pm = getPackageManager();        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);          if (activities.size() != 0)          {              speakButton.setOnClickListener(this);          }           else         {              speakButton.setEnabled(false);             speakButton.setText("Recognizer not present");       }     }     public void onClick(View v)     {         if (v.getId() == R.id.btn_speak)          {             startVoiceRecognitionActivity();         }     }    private void startVoiceRecognitionActivity()     {         //借助Intent传递语音识别的模式          Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        //语言模式和自由形式的语音识别        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);        //提示语音开始        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");       //开始语音识别          startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);     }        //当语音结束时的回调函数onActivityResult      @Override       protected void onActivityResult(int requestCode, int resultCode, Intent data)      {           if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK)           {               // 取得语音结果,加入一个选择判断就可以语音控制           ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);              mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));          }            super.onActivityResult(requestCode, resultCode, data);       }   }  

 

 

然后我们来亲自实践一个语音搜索,创建一个webview 来进行搜索,就是完整的语音搜索了,结合上述的语音合成,那简直牛的很,

 

 

import android.app.Activity;   import android.content.Intent;   import android.content.pm.PackageManager;   import android.content.pm.ResolveInfo;   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;   import java.util.ArrayList;   import java.util.List;   public class VoiceRecognition extends Activity implements OnClickListener {   private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;          private ListView mList;     public void onCreate(Bundle savedInstanceState)       {          super.onCreate(savedInstanceState);         setContentView(R.layout.voice_recognition);         Button speakButton = (Button) findViewById(R.id.btn_speak);         mList = (ListView) findViewById(R.id.list);          // 检查是否有语音识别活动     PackageManager pm = getPackageManager();        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);          if (activities.size() != 0)          {              speakButton.setOnClickListener(this);          }           else         {              speakButton.setEnabled(false);             speakButton.setText("Recognizer not present");       }     }     public void onClick(View v)     {         if (v.getId() == R.id.btn_speak)          {             startVoiceRecognitionActivity();         }     }    private void startVoiceRecognitionActivity()     {         //借助Intent传递语音识别的模式   ,注明语音搜索识别模式       Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);        //语言模式和自由形式的语音识别        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);        //提示语音开始        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");       //开始语音识别          startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);     }        //当语音结束时的回调函数onActivityResult      @Override       protected void onActivityResult(int requestCode, int resultCode, Intent data)      {           if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK)           {               // 取得语音结果,加入一个选择判断就可以语音控制           ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);              mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));          //进行访问网页的语音搜索,可以结合上述例子实现语音合成朗读,他妈的,太爽了哈                            WebView webView = (WebView) findViewById(R.id.webView);        // 配置浏览器,使其可支持 JavaScript        WebSettings webSettings = webView.getSettings();        webSettings.setJavaScriptEnabled(true);        // 清除浏览器缓存        webView.clearCache(true);        // 指定浏览器地址,就是google搜索关键词的访问格式,检索语音结果        webView.loadUrl("http://www.google.com.hk/search?hl=zh-CN&source=hp&q="+data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)+"&meta=&aq=f&aqi=g10&aql=&oq=&gs_rfai=");        // 指定浏览器需要解析的 html 数据  ,可以结合语音朗读读出        // webView.loadData("<a href="http://webabcd.cnblogs.com/" mce_href="http://webabcd.cnblogs.com/">webabcd</a>", "text/html", "utf-8");          }            super.onActivityResult(requestCode, resultCode, data);       }   }  

 

至此大功告成,本文未经许可,不准转载。否则侵权后果自负。

需要源代码(Andrroid语音控制拨号sample,Andrroid语音自动搜索并朗读结果的语音浏览器sample),请在本人优快云博客留言,源码将在4.12日VS2010发布大会那一天统一发送源码(仅发送前200楼),请大家踊跃留下Email.后面排队的将等到5.20日。

 

作为语音技术业内专家,提供语音技术开发应用企业咨询顾问,有需要者请联系MSN:yincheng01@163.com,

策划有一套云计算语音交互移动3G商业计划正在实施已经获得天使投资,在移动3G上面有想法的站长们可以联系我共创语音3G时代!

 

另外本人正在参加Google Andrroid软件开发大赛(作品为基于人工智能与语音交互的3D宠物),希望有空大家去捧场!

http://code.google.com/intl/zh-CN/android/

http://code.google.com/intl/zh-CN/android/adc/

 

 本人完成作品时,得到中国科学院陈博士后在人工智能与多线程并行计算上的支持,在此特别感谢!

 

 

 

也特别感谢中国科学院著名的图形学天才专家颜博士后完成了3D智能宠物的所有表情与动作,其效果之炫丽简直是惊天地,泣鬼神!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值