Android原生TTS的基本使用以及配合中文语音包实现中文TTS

天在开发中做了一个文字转为语音的功能.
首先Android 系统自带的文字转语音 这种方式很简单也非常好用。(可以借助科大讯飞的在线语音合成技术,设置-无障碍-TTS选择讯飞)

public class SpeechUtils {
    private Context context;
    private static final String TAG = "SpeechUtils";
    private static SpeechUtils singleton;
    private TextToSpeech textToSpeech; // TTS对象

    public static SpeechUtils getInstance(Context context) {
        if (singleton == null) {
            synchronized (SpeechUtils.class) {
                if (singleton == null) {
                    singleton = new SpeechUtils(context);
                }
            }
        }
        return singleton;
    }
    public SpeechUtils(Context context) {
        this.context = context;
        textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if (i == TextToSpeech.SUCCESS) {
                    textToSpeech.setLanguage(Locale.US);
//                    textToSpeech.setLanguage(Locale.CHINA);
                    textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
                    textToSpeech.setSpeechRate(0.85f);

                }
//                else {
//                    ToastUtils.toast(context,"播报引擎加载失败");
//                }
            }
        });
    }
    public void speakText(String text) {
        if (textToSpeech != null) {
            textToSpeech.speak(text,
                    TextToSpeech.QUEUE_FLUSH, null);
        }

    }
    public void speakText(String text, int queueMode) {
        if (textToSpeech != null) {
            textToSpeech.speak(text, queueMode,
                    null, UUID.randomUUID().toString());
        }

    }
    public void close() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
            textToSpeech=null;
            singleton=null;
        }
    }
    public void stop() {
        if (textToSpeech != null) {
            textToSpeech.stop();
        }
    }
}

(1)初始化

 SpeechUtils     speechUtils = SpeechUtils.getInstance(this);

(2) 使用

   speechUtils.speakText(speakStr);

(3) 释放资源

 speechUtils.stop();
  speechUtils.close();
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JavaScript中调用Android原生方法需要使用JavaScript与原生Android之间的交互技术。以下是一些方法: 1. 使用WebView执行JavaScript代码 可以使用WebView在Android应用程序中执行JavaScript代码。WebView对象提供了一个方法,可以通过该方法将JavaScript代码传递给WebView并执行它。在JavaScript代码中,可以使用window.prompt来调用Android方法。例如: ``` window.prompt("methodName:param1:param2", ""); ``` 在Android应用程序中,可以在WebViewClient的shouldOverrideUrlLoading方法中截取这个请求,解析参数,并调用相应的方法。例如: ```java public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("javascript:")) { String[] data = url.substring("javascript:".length()).split(":"); String methodName = data[0]; String param1 = data[1]; String param2 = data[2]; // call the Android method with the given name and parameters callMethod(methodName, param1, param2); return true; } return false; } ``` 2. 使用JavaScriptInterface接口 可以在Android应用程序中使用JavaScriptInterface接口来让JavaScript代码直接调用Android方法。首先,需要在WebView对象上启用JavaScript。然后,需要创建一个Java对象来处理JavaScript传递的调用请求。在Java对象中,需要使用@JavascriptInterface注解来标记要暴露给JavaScript调用的方法。例如: ```java public class MyJavaScriptInterface { private Context mContext; public MyJavaScriptInterface(Context context) { mContext = context; } @JavascriptInterface public void callNativeMethod(String methodName, String param1, String param2) { // call the Android method with the given name and parameters callMethod(methodName, param1, param2); } } ``` 然后,在Android应用程序中,需要将这个Java对象添加到WebView对象中,并使用addJavascriptInterface方法将它暴露给JavaScript代码。例如: ```java MyJavaScriptInterface jsInterface = new MyJavaScriptInterface(this); mWebView.addJavascriptInterface(jsInterface, "MyApp"); String javascript = "function callNativeMethod(methodName, param1, param2) {" + " MyApp.callNativeMethod(methodName, param1, param2);" + "}"; mWebView.loadUrl("javascript:" + javascript); ``` 以上是两种在JavaScript中调用Android原生方法的方法。当然,具体的实现方案需要根据具体的需求和场景进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值