Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of converting Voice to text by Android studio

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Android studio how to achieve voice-to-text function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

Register and download SDK on iFLYTEK's website

1. First, go to the iFLYTEK open platform to apply for an account (https://www.xfyun.cn/), then click "console" to enter a new page, create an application, find "voice dictation", and download the corresponding SDK.

The contents of the decompressed file are as follows:

Second, configure Android projects

1. Create a new empty project in android studio and copy the contents of the libs folder to the libs folder of the Android project, where msc.jar right-click to add Add As Library:

two。 Copy the assets folder to the main directory of the project, and create a new folder called jniLibs under the main directory, and copy the two subfolders from the downloaded libs folder into jniLibs:

3. Add the following permissions to AndriodManifest.xml:

4. Add the following code to build.gradle under the app directory:

SourceSets {main {jniLibs.srcDirs = ['libs']}}

5. Modify the code in the layout file activity_main.xml:

6. Create a new interface named XunFeiCallbackListener:

Package com.example.myapplication;import com.iflytek.cloud.RecognizerResult;public interface XunFeiCallbackListener {void onFinish (RecognizerResult results);}

7. Create a new class named XunFeiUtil:

Package com.example.myapplication;import android.content.Context; import android.widget.Toast; import com.iflytek.cloud.RecognizerResult; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechError; import com.iflytek.cloud.SpeechUtility; import com.iflytek.cloud.ui.RecognizerDialog; import com.iflytek.cloud.ui.RecognizerDialogListener; import org.json.JSONArray; import org.json.JSONObject Import org.json.JSONTokener;public class XunFeiUtil {public static String appid = "own appid"; public static void initXunFei (Context context) {SpeechUtility.createUtility (context, SpeechConstant.APPID + "=" + appid);} public static void startVoice (Context context, final XunFeiCallbackListener callbackListener) {RecognizerDialog dialog = new RecognizerDialog (context,null); dialog.setParameter (SpeechConstant.LANGUAGE, "zh_cn"); dialog.setParameter (SpeechConstant.ACCENT, "mandarin") Dialog.setParameter (SpeechConstant.ASR_PTT, "0"); dialog.setListener (new RecognizerDialogListener () {@ Override public void onResult (RecognizerResult recognizerResult, boolean b) {callbackListener.onFinish (recognizerResult);} @ Override public void onError (SpeechError speechError) {}}); dialog.show () / Toast.makeText (this, "Please start talking", Toast.LENGTH_SHORT). Show ();} public static String parseIatResult (String json) {StringBuffer ret = new StringBuffer (); try {JSONTokener tokener = new JSONTokener (json); JSONObject joResult = new JSONObject (tokener); JSONArray words = joResult.getJSONArray ("ws"); for (int I = 0; I

< words.length(); i++) { // 转写结果词,默认使用第一个结果 JSONArray items = words.getJSONObject(i).getJSONArray("cw"); JSONObject obj = items.getJSONObject(0); ret.append(obj.getString("w")); } } catch (Exception e) { e.printStackTrace(); } return ret.toString(); }} 8.修改MainActivity: package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import com.iflytek.cloud.RecognizerResult;import static com.example.myapplication.XunFeiUtil.parseIatResult;import static com.example.myapplication.XunFeiUtil.*;public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_click; private EditText mResultText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initXunFei(this); btn_click = (Button) findViewById(R.id.btn_click); mResultText = ((EditText) findViewById(R.id.result)); btn_click.setOnClickListener(this); } @Override public void onClick(View v) { startVoice(this, new XunFeiCallbackListener() { @Override public void onFinish(RecognizerResult results) { String text = parseIatResult(results.getResultString()); // 自动填写地址 mResultText.append(text); } }); }}三、运行效果展示 按照以上操作进行修改,完成后运行项目,会出现语音开启失败,错误码为20006的错误,出错原因是android系统在非动态申请权限的情况下,默认是把麦克风权限是关闭了的,因此需要打开权限,可以在手机的权限中自己修改权限设置,成功后的界面如下:

Thank you for reading this article carefully. I hope the article "how to achieve voice-to-text function in Android studio" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report