Android 开发汉字转带声调的拼音

复制代码
一、引入:
implementation("com.github.stuxuhai:jpinyin:1.1.7")

二、布局

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp">
    <TextView
        android:id="@+id/textViewPinyin"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/edittext_background" />

    <EditText
        android:id="@+id/editTextPinyin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入文字" />
    <Button
        android:id="@+id/btnSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="朗读" />

</LinearLayout>

三、java 代码

复制代码
package com.wzt.lovestudy;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.util.Locale;
public class StudyActivity extends Activity {
    private TextToSpeech tts;

    private Button btnSpeak;

    private EditText editTextPinyin;
    private TextView textViewPinyin; // 假设


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initClick();
        initTTS();
        addTextChangedListener();

    }

    private void initView() {

        btnSpeak = findViewById(R.id.btnSpeak);
        editTextPinyin = findViewById(R.id.editTextPinyin);
        textViewPinyin = findViewById(R.id.textViewPinyin);
    }

    private void initClick() {
        btnSpeak.setOnClickListener(v -> {
            String text = editTextPinyin.getText().toString();
            if (text.isEmpty()) {
                Toast.makeText(StudyActivity.this, "请输入文本", Toast.LENGTH_SHORT).show();
                return;
            }
            // 执行朗读
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
        });
    }

    private void addTextChangedListener() {
        editTextPinyin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String result ;
                try {
                    result = PinyinHelper.convertToPinyinString(s.toString(), " ", PinyinFormat.WITH_TONE_MARK);
                } catch (PinyinException e) {
                    throw new RuntimeException(e);
                }
                textViewPinyin.setText(result);

            }
        });
    }


    private void initTTS() {
        tts = new TextToSpeech(this, status -> {
            if (status == TextToSpeech.SUCCESS) {
                // 设置语言为中文
                int result = tts.setLanguage(Locale.CHINA);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "当前设备不支持中文朗读");
                    Toast.makeText(StudyActivity.this, "不支持中文朗读", Toast.LENGTH_SHORT).show();
                } else {
                    // 可选:调整语速和音调
                    tts.setSpeechRate(1.0f);
                    tts.setPitch(1.0f);
                    Log.e("TTS", "TTS 初始化成功");
                }
            } else {
                Log.e("TTS", "TTS 初始化失败");
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
}
相关推荐
铅笔侠_小龙虾19 分钟前
Rust 学习(2)-变量、常量与 shadowing
学习·算法·rust
it小生010125 分钟前
从底层思维到高效行动,顶尖人才进阶核心修炼指南
学习·学习方法
cfm_291425 分钟前
Spring监听器
java·spring boot·后端
SamDeepThinking33 分钟前
Java面向对象在JVM里怎么实现
java·后端·面试
恣逍信点44 分钟前
《凌微经》通俗解读——哲学第一因
学习·职场和发展·创业创新·学习方法·业界资讯·交友·哲学
red_redemption1 小时前
自由学习记录(207)
学习
咸鱼翻身小阿橙2 小时前
多线程解析
java·开发语言
AOwhisky2 小时前
Python 学习笔记(第四期)——字符串:格式化、操作与文本处理——核心知识点自测与详解
开发语言·笔记·python·学习·列表·元组·字典
光学补偿2 小时前
微服务学习 --- Spring Cloud概述
学习·spring cloud·微服务
一枚码农出身的猎头2 小时前
岗位招聘:架构师,base湖南长沙,70-80w
java·架构