Android的数据存储和IO - 自动朗读(TTS)

本文介绍了一种在Android平台上实现文本转语音(TTS)的方法。通过创建一个名为Speech的应用,可以将输入的文本转换为语音播放,并支持将语音合成后的音频文件保存到设备上。
部署运行你感兴趣的模型镜像

Android的数据存储和IO - 自动朗读(TTS)

自动朗读又是Android提供的另一种另类的IO,蛮不错的哦,支持对指定文本内容进朗读,学习完这个内容我立马就让它朗读:wwj is a good man.作为一个自我满足。

创建项目:Speech

运行效果:

Activity文件:Speech.java

package wwj.speech;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Speech extends Activity {
	
	TextToSpeech tts;
	EditText editText;
	Button speech;
	Button record;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //初始化TextToSpeech对象
        tts = new TextToSpeech(this, new OnInitListener() {
			
			public void onInit(int status) {
				// TODO Auto-generated method stub
				//如果装载TTS引擎成功
				if(status == TextToSpeech.SUCCESS){
					//设置使用美式英语朗读
					int result = tts.setLanguage(Locale.US);
					//如果不支持所设置的语言
					if(result != TextToSpeech.LANG_COUNTRY_AVAILABLE 
							&& result != TextToSpeech.LANG_AVAILABLE){
						Toast.makeText(Speech.this, "TTS暂时不支持这种语言的朗读。", 50000).show();
					}
				}
			}
		});
        editText = (EditText)findViewById(R.id.txt);
        speech = (Button) findViewById(R.id.speech);
        record = (Button) findViewById(R.id.record);
        speech.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//执行朗读
				tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
			}
		});
        record.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//将朗读文本的音频记录到指定文件
				tts.synthesizeToFile(editText.getText().toString(), null, "/mnt/sdcard/sound.wav");
				Toast.makeText(Speech.this, "声音记录成功! ", 50000).show();
			}
		});
    }
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	//关闭TextToSpeech对象
    	if(tts != null){
    		tts.shutdown();
    	}
    	super.onDestroy();
    }
}


您可能感兴趣的与本文相关的镜像

HunyuanVideo-Foley

HunyuanVideo-Foley

语音合成

HunyuanVideo-Foley是由腾讯混元2025年8月28日宣布开源端到端视频音效生成模型,用户只需输入视频和文字,就能为视频匹配电影级音效

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值