日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android初学者之轻松实现语音识别

發布時間:2025/7/25 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android初学者之轻松实现语音识别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android初學者之輕松實現語音識別

蘋果的iphone有語音識別用的是Google的技術,做為Google力推的Android 自然會將其核心技術往Android 系統里面植入,并結合google 的云端技術將其發揚光大。?

所以Google Voice Recognition在Android 的實現就變得極其輕松。?

語音識別,借助于云端技術可以識別用戶的語音輸入,包括語音控制等技術,下面我們將利用Google 提供的Api 實現這一功能。?

功能點為:通過用戶語音將用戶輸入的語音識別出來,并打印在列表上。?
001* Copyright (C) 2008 The Android Open Source Project
002?
003*
004?
005* Licensed under the Apache License, Version 2.0 (the "License");
006?
007* you may not use this file except in compliance with the License.
008?
009* You may obtain a copy of the License at
010?
011*
012?
013* <a href="http://www.apache.org/licenses/LICENSE-2.0" rel="nofollow" target="_blank">http://www.apache.org/licenses/LICENSE-2.0</a>
014?
015*
016?
017* Unless required by applicable law or agreed to in writing, software
018?
019* distributed under the License is distributed on an "AS IS" BASIS,
020?
021* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
022?
023* See the License for the specific language governing permissions and
024?
025* limitations under the License.
026?
027*/
028?
029??
030?
031package com.example.android.apis.app;
032?
033??
034?
035import com.example.android.apis.R;
036?
037??
038?
039import android.app.Activity;
040?
041import android.content.Intent;
042?
043import android.content.pm.PackageManager;
044?
045import android.content.pm.ResolveInfo;
046?
047import android.os.Bundle;
048?
049import android.speech.RecognizerIntent;
050?
051import android.view.View;
052?
053import android.view.View.OnClickListener;
054?
055import android.widget.ArrayAdapter;
056?
057import android.widget.Button;
058?
059import android.widget.ListView;
060?
061??
062?
063import java.util.ArrayList;
064?
065import java.util.List;
066?
067??
068?
069/**
070?
071* Sample code that invokes the speech recognition intent API.
072?
073*/
074?
075public class VoiceRecognition extends Activity implements OnClickListener {
076?
077??
078?
079private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
080?
081??
082?
083private ListView mList;
084?
085??
086?
087/**
088?
089* Called with the activity is first created.
090?
091*/
092?
093@Override
094?
095public void onCreate(Bundle savedInstanceState) {
096?
097super.onCreate(savedInstanceState);
098?
099??
100?
101// Inflate our UI from its XML layout description.
102?
103setContentView(R.layout.voice_recognition);
104?
105??
106?
107// Get display items for later interaction
108?
109Button speakButton = (Button) findViewById(R.id.btn_speak);
110?
111??
112?
113mList = (ListView) findViewById(R.id.list);
114?
115??
116?
117// Check to see if a recognition activity is present
118?
119PackageManager pm = getPackageManager();
120?
121List activities = pm.queryIntentActivities(
122?
123new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
124?
125if (activities.size() != 0) {
126?
127speakButton.setOnClickListener(this);
128?
129} else {
130?
131speakButton.setEnabled(false);
132?
133speakButton.setText("Recognizer not present");
134?
135}
136?
137}
138?
139??
140?
141/**
142?
143* Handle the click on the start recognition button.
144?
145*/
146?
147public void onClick(View v) {
148?
149if (v.getId() == R.id.btn_speak) {
150?
151startVoiceRecognitionActivity();
152?
153}
154?
155}
156?
157??
158?
159/**
160?
161* Fire an intent to start the speech recognition activity.
162?
163*/
164?
165private void startVoiceRecognitionActivity() {
166?
167Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
168?
169intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
170?
171RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
172?
173intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
174?
175startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
176?
177}
178?
179??
180?
181/**
182?
183* Handle the results from the recognition activity.
184?
185*/
186?
187@Override
188?
189protected void onActivityResult(int requestCode, int resultCode, Intent data) {
190?
191if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
192?
193// Fill the list view with the strings the recognizer thought it could have heard
194?
195ArrayList matches = data.getStringArrayListExtra(
196?
197RecognizerIntent.EXTRA_RESULTS);
198?
199mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,
200?
201matches));
202?
203}
204?
205??
206?
207super.onActivityResult(requestCode, resultCode, data);
208?
209}
210?
211}
評價:

分享 45 分鐘前

總結

以上是生活随笔為你收集整理的Android初学者之轻松实现语音识别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。