美文网首页
原生语音控件

原生语音控件

作者: 故江 | 来源:发表于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();
        }
    }
    

    相关文章

      网友评论

          本文标题:原生语音控件

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