美文网首页
原生语音控件

原生语音控件

作者: 故江 | 来源:发表于2019-03-12 16:33 被阅读0次
public class SystemTTS {

    //单例模式
    private static  SystemTTS singleton;
    //context对象
    private Context mContext;
    //核心播放对象
    private TextToSpeech mTextToSpeech;
    //是否支持
    private boolean isSupport = true;

    private SystemTTS(Context context) {
        this.mContext = context.getApplicationContext();
        mTextToSpeech = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                init(i);
            }
        });
    }

    public static  SystemTTS getInstance(Context context) {
        if (singleton == null) {
            synchronized (SystemTTS.class) {
                if (singleton == null) {
                    singleton = new SystemTTS(context);
                }
            }
        }
        return singleton;
    }

    //textToSpeech的配置
    private void init(int i) {
        if (i == TextToSpeech.SUCCESS) {
            int result = mTextToSpeech.setLanguage(Locale.ENGLISH);
            mTextToSpeech.setPitch(2.0f);
            mTextToSpeech.setSpeechRate(3.0f);
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                isSupport = false;
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void play(String text) {
        if (!isSupport){
            Toast.makeText(mContext,"失败",Toast.LENGTH_SHORT).show();
            return;
        }
        if (mTextToSpeech != null){
            mTextToSpeech.speak(text,TextToSpeech.QUEUE_ADD,null,null);
        }
    }

    public void stop() {
        if (mTextToSpeech != null) {
            mTextToSpeech.stop();
        }
    }

    public void destroy() {
        stop();
        if (mTextToSpeech != null) {
            mTextToSpeech.shutdown();
        }
    }
}
public class MainActivity extends AppCompatActivity {

    private EditText etWords;
    private Button btnPlay;
    private SystemTTS systemTTS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                String words = getWords();
                if (!TextUtils.isEmpty(words)) {
                    systemTTS.play(words);
                }
            }
        });
    }

    private String getWords() {
        return etWords.getText().toString().trim();
    }

    private void initData() {
        systemTTS = SystemTTS.getInstance(getApplicationContext());
    }

    private void initView() {
        etWords = (EditText) findViewById(R.id.et_word);
        btnPlay = (Button) findViewById(R.id.btn_play);
        etWords.setText("Since I was in primary school, my parents hired a tutor to teach me English, because they knew the importance of the international language. Though I have learned English for many years, I never had the chance to talk to the local people. The chance came as I went to high school. The school had a project to promote the communication between countries, so there were some foreign students studied here for a year. I was lucky to become a foreign student's partner. My job was to assist her in solving the daily problems. We talked in English. At first, I was so nervous and spoke very slowly, then my partner encouraged me and she wouldn't correct me. As we communicate more, she taught me some local language, which made me sounded like a local person. This experience is such valuable for me.");
        etWords.getText();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        systemTTS.destroy();
    }
}

相关文章

  • 原生语音控件

  • 【Android】自定义控件

    Android自定义控件可以有三种实现方式 组合原生控件自己绘制控件继承原生控件 1 组合原生控件 1.1 组合原...

  • 安卓高级UI之自定义View实现复杂动画

    自定义控件实现方式组合原生控件自己绘制控件集成原生控件 贝塞尔曲线 心形曲线 UI的绘制流程Measure过程La...

  • 小程序即时通讯

    小程序即时通讯——文本、语音输入控件(一)集成 小程序即时通讯——文本、语音输入控件(一)集成聊天输入组件控件样式...

  • RN笔记-RefreshControl原生态刷新控件

    原生态刷新控件 在开发过程中,经常要用到刷新控件,iOS中比较热门使用第三方的刷新控件,然而却忽略了苹果系统原生的...

  • ios WKWebView 与h5 js数据交互

    note:图中上部分为web页面,下半部分是ios原生控件, 演示的是web页面与ios原生控件之间的数据交互 1...

  • iOS7兼容自定义带动画的AlertViewController

    意为打造类似原生控件样式的控件, 类似于UIAlertViewController优点在于: iOS 7.0 之后...

  • android2019-01-03

    1.View的绘制流程自定义控件:1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控件。...

  • Android评论功能引发的重构

    Android实现评论功能 一,原生控件ExpandableListview 利与弊 利 该控件样式父子列表,一个...

  • 组件-->模块-->插件

    组件化 1) 为什么需要组件化? Android的原生控件,基本上都没法直接拿来用,太丑了。另外一方面,原生控件在...

网友评论

      本文标题:原生语音控件

      本文链接:https://www.haomeiwen.com/subject/onenpqtx.html