Android 原生语音播报

Android系统本身自带的语音引擎不支持中文,所以需要依赖 如 科大讯飞、度秘 等,类似语音引擎才可以。

三步:

1:安装语音引擎。  安装包百度找一下。安装到需要播报的机子上

如果没找到的话,这里有:https://download.csdn.net/download/qq_39731011/15047992        

2:设置采用该引擎:

设置  —  语音和输入法  —  文字转语音(TTS)输出  —  首选引擎 — 选择你安装的引擎

3:将以下工具类复制到项目中,一行代码直接用

一行代码调用:

SpeechUtils.getInstance(this).speakText("语音播报的内容");//app启动时 先在Application调用一下

/**
 * Created by Xinghai.Zhao on 2021/2/4
 * android自带语音识别工具,如果要支持中文需要配合其他语音引擎
 */
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.text.TextUtils;

import java.util.Locale;

public class SpeechUtils {
    private Context context;


    private static final String TAG = "SpeechUtils";
 
  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个Android原生持续检测麦克风并语音转文字的代码示例: 1. 在Manifest文件中添加麦克风权限: ```xml <uses-permission android:name="android.permission.RECORD_AUDIO" /> ``` 2. 在MainActivity中创建SpeechRecognizer对象,并实现RecognitionListener接口: ```java public class MainActivity extends AppCompatActivity implements RecognitionListener { private SpeechRecognizer speechRecognizer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); speechRecognizer.setRecognitionListener(this); } @Override public void onReadyForSpeech(Bundle params) { Log.d("Speech", "Ready for speech"); } @Override public void onBeginningOfSpeech() { Log.d("Speech", "Beginning of speech"); } @Override public void onRmsChanged(float rmsdB) { Log.d("Speech", "RMS changed"); } @Override public void onBufferReceived(byte[] buffer) { Log.d("Speech", "Buffer received"); } @Override public void onEndOfSpeech() { Log.d("Speech", "End of speech"); } @Override public void onError(int error) { Log.d("Speech", "Error: " + error); } @Override public void onResults(Bundle results) { Log.d("Speech", "Results received"); ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (matches != null && matches.size() > 0) { String text = matches.get(0); Log.d("Speech", "Text: " + text); } } @Override public void onPartialResults(Bundle partialResults) { Log.d("Speech", "Partial results received"); ArrayList<String> matches = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (matches != null && matches.size() > 0) { String text = matches.get(0); Log.d("Speech", "Partial text: " + text); } } @Override public void onEvent(int eventType, Bundle params) { Log.d("Speech", "Event: " + eventType); } } ``` 3. 在需要开始检测麦克风的地方调用startListening方法: ```java Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); speechRecognizer.startListening(intent); ``` 这将启动语音识别,并在检测到语音时调用RecognitionListener接口中的方法。 4. 在需要停止检测麦克风的地方调用stopListening方法: ```java speechRecognizer.stopListening(); ``` 这将停止语音识别,并停止调用RecognitionListener接口中的方法。 注意:以上代码仅为示例,实际应用中需要根据需求进行修改和调整。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值