Android通过TextToSpeech实现文字转语音

这篇具有很好参考价值的文章主要介绍了Android通过TextToSpeech实现文字转语音。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、直接上代码:

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 android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
    TextToSpeech textToSpeech;
    EditText ed1;
    Button b1;
    String toSpeak;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1 = findViewById(R.id.editText);
        b1 = findViewById(R.id.button);

        textToSpeech = new TextToSpeech(getApplicationContext(), this);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSpeak = ed1.getText().toString();
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
//                textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_ADD, null, null);
            }
        });
    }


    public void onPause() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onPause();
    }

    @Override
    public void onInit(int status) {
        //判断是否转化成功
        if (status == TextToSpeech.SUCCESS) {
            //设置语言为中文
            int languageCode = textToSpeech.setLanguage(Locale.CHINA);
            //判断是否支持这种语言,Android原生不支持中文,使用科大讯飞的tts引擎就可以了
            if (languageCode == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.d("TAG", "onInit: 不支持这种语言");
            } else {
                //不支持就改成英文
                textToSpeech.setLanguage(Locale.US);
            }
            //设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
            textToSpeech.setPitch(1.0f);
            //设置语速
            textToSpeech.setSpeechRate(1.0f);
            //在onInIt方法里直接调用tts的播报功能
//            textToSpeech.speak("李佩伦打卡成功", TextToSpeech.QUEUE_ADD, null);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="文字转语音示例"
        android:textSize="35sp" />


    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="46dp"
        android:hint="输入文字"
        android:textColor="#ff7aff10"
        android:textColorHint="#ffff23d1" />

    <Button
        android:id="@+id/button"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="文字转语音" />

</RelativeLayout>

二、效果:输入‘中文’点击按钮发音

android 中文转语音,android,java,开发语言

 三、Android高版本兼容

        如果在Android11里转语音不发声,并且报错:speak failed:not bound to TTS engine,则需要在AndroidManifest.xml文件中声明如下内容:

    <queries>
        <intent>
            <action android:name="android.intent.action.TTS_SERVICE"/>
        </intent>
    </queries>

android 中文转语音,android,java,开发语言

四、扩展

1、setLanguage支持的一些语言环境:

语言 常量
美式英语 US
加拿大法语 CANADA_FRENCH
德语 GERMANY
意大利语 ITALY
日语 JAPAN
汉语 CHINA

 2、TextToSpeech类中的一些其他方法:

方法 说明
addSpeech(String text, String filename) 此方法在文本字符串和声音文件之间添加映射。
getLanguage() 此方法返回描述语言的Locale实例。
isSpeaking() 此方法检查TextToSpeech引擎是否正在忙于讲话。
setPitch(float pitch) 此方法设置TextToSpeech引擎的语音音调。
setSpeechRate(float speechRate) 此方法设置语音速率。
shutdown() 此方法释放TextToSpeech引擎使用的资源。
stop() 这种方法停止说话。

3、播放的声音可在手机:设置->语言与输入法->文字转语音(TTS)输出 中进行设置,或安装其他平台语音识别模块并在此配置。文章来源地址https://www.toymoban.com/news/detail-552073.html

应用名称 支持离线 备注 下载地址
ITRI TTS 下载
讯飞语记 需打开一次, 但不需要登陆 下载
科大讯飞语音引擎3.0 推荐 下载
Speech Services by Google 需要科学上网, 离线需要先下载语音包 下载

到了这里,关于Android通过TextToSpeech实现文字转语音的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • android 开发 W/TextToSpeech: speak failed: not bound to TTS engine

    笔者使用TTS(TextToSpeech)对于文本内容进行语音播报,控制台报错 笔者核心代码: 运行控制台报错内容: 步骤1、 在 packageappsrcmainAndroidManifest.xml 中配置如下内容: 具体操作如下图所示: 具体代码: 若进行英语播报,进行到此处问题即可解决。若是需要进行中文播报,需继

    2024年01月20日
    浏览(39)
  • Android 妙用TextView实现左边文字,右边图片

    原文: Android 妙用TextView实现左边文字,右边图片 - Stars-One的杂货小窝 有时候,需要文字在左边,右边有个箭头,我个人之前会有两种做法: 使用线性布局来实现 或者使用约束布局,一个左对齐,一个右对齐 这几天突然想到是否可以使用TextView的设置图标的方式实现,研究发现确实可以

    2024年02月17日
    浏览(33)
  • Android 实现阅读用户协议的文字控件效果

    开发中,经常要用到一些阅读隐私协议的场景,原生的textview控件很难做到在一个控件里有两个点击事件,那现在就来安利一个强大的组件——SpannableStringBuilder。 先看看效果:  直接上代码,布局文件: 布局文件很容易理解,一个checkbox实现选中效果,旁边加一个textview。

    2024年02月15日
    浏览(44)
  • openai开源的whisper在huggingface中使用例子(语音转文字中文)

    openai开源的语音转文字支持多语言在huggingface中使用例子。 目前发现多语言模型large-v2支持中文是繁体,因此需要繁体转简体。 后续编写微调训练例子 GitHub地址: https://github.com/openai/whisper

    2024年02月11日
    浏览(35)
  • 中文语音识别转文字的王者,阿里达摩院FunAsr足可与Whisper相颉顽

    君不言语音识别技术则已,言则必称Whisper,没错,OpenAi开源的Whisper确实是世界主流语音识别技术的魁首,但在中文领域,有一个足以和Whisper相颉顽的项目,那就是阿里达摩院自研的FunAsr。 FunAsr主要依托达摩院发布的Paraformer非自回归端到端语音识别模型,它具有高精度、高

    2024年02月03日
    浏览(40)
  • Android实现超出固定行数折叠文字“查看全文“、“收起全文“

    网上有很多关于这个的代码,实现都过于复杂了,github上甚至还看到一篇文章600多行代码,结果一跑起来全是bug。还是自己写吧!!! 如果我们需要换行的 \\\"查看全文\\\"、\\\"收起全文\\\" 效果那没什么号说的,因为可以直接用两个TextView然后通过判断超过行数还是没有超过行数来判

    2024年02月13日
    浏览(23)
  • Android 实现 TextView 设置中文字体加粗的方法

    Android 实现 TextView 设置中文字体加粗的方法 在 Android 开发中,我们经常需要对 TextView 进行样式调整,包括字体加粗。本文将介绍一种实现方法。 步骤一:准备字体文件 首先,我们需要准备一个自定义的字体文件,该文件用于设置中文字体的加粗效果。可以从合法渠道获取或

    2024年02月04日
    浏览(35)
  • Android 通过scheme跳转支付宝实现支付

    原文地址: Android 通过scheme跳转支付宝实现支付 - Stars-One的杂货小窝 需求的来源是支付功能,由于支付宝不允许个人开通具有webhook的支付服务,所以是对接了一个支付系统(应该算作四方支付)。创建订单后,系统会返回一个h5链接。 对于PC版的软件,将此链接转为二维码,然后提示

    2023年04月25日
    浏览(22)
  • 蓝牙通信 Android开发实现手机间通过蓝牙传输文件

    MainActivity.java 根据以上代码的结构和功能,我会将它们分为以下几部分: 权限请求和检查 requestPermissions() 方法 checkLocationPermission() 方法 onRequestPermissionsResult() 方法 初始化和设置 onCreate() 方法 onStart() 方法 onActivityResult() 方法 蓝牙设备搜索和配对 discoverDevices() 方法 与列表交互

    2024年03月27日
    浏览(38)
  • Android开发:通过Tesseract第三方库实现OCR

    一、引言         什么是 OCR ?OCR(Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程。简单地说,OCR是一种技术,该项技术采用光学的

    2024年02月16日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包