文字转语音等可视化组件

  • 文字转语音服务(TTS)应用
  • 测试结果:点击按钮后将会听到预设的声音
    public class MainActivity extends Activity{
    private TextToSpeech tts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);
    tts=new TextToSpeech(this,new TTSListener());//初始化TTS,如果成功调用会听到'TTS服务已上线'的语音
  • }
     
    private class TTSListener implements OnInitListener{
    public void onInit(int status){
    if (status==TextToSpeech.SUCCESS){//设置语言
    int result=mtts.setLanguage(Locale.CHINA);//若返回-2即不支持该语言
     
    if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
    Toast.makeText(this,"语言不可用,TTS使用失败 ",Toast.LENGTH_LONG).show();}//若调用失败会弹出提醒栏
    else {
    mtts.speak("TTS服务已上线", TextToSpeech.QUEUE_FLUSH,null);} } }
    }

  • @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
  •      //使用此种方法,需要在xml文件的各个button中添加 android:onClick="Click"
         switch (view.getId()){
         case R.id.button:
  • tts.speak("想要转换为语音的文字列", TextToSpeech.QUEUE_FLUSH, null);}
    } }

xml布局文件就很简单:就一个button按钮,

(PS:三星手机居然不支持日语,这就挺有意思

  • 实现振动功能
    首先需要在mainfest文件中加入声明:
    <uses-permission android:name="android.permission.VIBRATE"/>
    final Vibrator vibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE);
    if (vibrator.hasVibrator()==true);//检测硬件是否存振动系统
    long[] vi_pattten={100,1000,200,2000}; vibrator.vibrate(vi_pattten,-1);
    //使用函数,vi_patten为可变long型,2个参数为一组 第一为等待ms启动,第二为持续时间
    取消使用vibrator.cancel();
     
  • 界面通知栏
    Toast.makeText(File_listAc.this,"choose "+message,Toast.LENGTH_LONG).show();
    参数介绍:1.通知栏在哪个界面显示 2.要显示的字符串 3.显示模式(短时,长时) toast需要使用.show()才可以显示
  • 顶部通知栏方法实例

protected void showNotification() {//顶部通知栏+震动

  NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  CharSequence title = "太阳系";
  CharSequence contents = "三号行星";

  PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    new Intent(this, MainActivity.class), 0);

  Notification notification = new Notification(R.drawable.default_icon,
    title, System.currentTimeMillis());

  notification.setLatestEventInfo(this, title, contents, contentIntent);//顶部通知栏内容,可修改

 notificationManager.notify(NOTIFICATIONS_ID, notification);
 }

猜你喜欢

转载自blog.csdn.net/zsx_xn20001/article/details/91697646