public class MainActivity extends Activity implements SensorEventListener {private TextToSpeech mSpeech = null;private Button btn = null;private Button btnAudio = null;private AudioManager audioManager;private SensorManager mSensorManager;private Sensor mSensor;private Boolean flag = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btn = (Button) findViewById(R.id.btn);btn.setOnClickListener(new BtnListener());btnAudio = (Button) findViewById(R.id.btnAudio);btnAudio.setOnClickListener(new BtnAudioListener());mSpeech = new TextToSpeech(MainActivity.this, new TTSListener());Toast.makeText(MainActivity.this, "start", Toast.LENGTH_SHORT).show();audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);audioManager.setMode(AudioManager.MODE_NORMAL);}private class TTSListener implements OnInitListener {@Overridepublic void onInit(int status) {if (status == TextToSpeech.SUCCESS) {int result = mSpeech.setLanguage(Locale.CHINA);Toast.makeText(MainActivity.this, "result = " + result,Toast.LENGTH_LONG).show();if (result == TextToSpeech.LANG_MISSING_DATA|| result == TextToSpeech.LANG_NOT_SUPPORTED) {System.out.println("don't support ");mSpeech.speak("don't support Chinese", TextToSpeech.QUEUE_FLUSH, null);} else {mSpeech.speak("语音已经准备好", TextToSpeech.QUEUE_FLUSH, null);}}}}private class BtnListener implements OnClickListener {@Overridepublic void onClick(View v) {mSpeech.speak("test data", TextToSpeech.QUEUE_FLUSH, null);mSpeech.speak("12345678", TextToSpeech.QUEUE_ADD, null);mSpeech.speak("中文语音支持", TextToSpeech.QUEUE_ADD, null);mSpeech.speak("TTS是android自带的语音合成库,使用方便,效果不错", TextToSpeech.QUEUE_ADD,null);}}private class BtnAudioListener implements OnClickListener {@Overridepublic void onClick(View v) {if(flag)flag = false;elseflag = true;setMySpeakerphoneOn(flag);}}private void setMySpeakerphoneOn(boolean on) {if (on) {audioManager.setSpeakerphoneOn(true);Toast.makeText(this, "扬声器模式", Toast.LENGTH_LONG).show();audioManager.setMode(AudioManager.MODE_NORMAL);} else {audioManager.setSpeakerphoneOn(false);// 关闭扬声器Toast.makeText(this, "听筒模式", Toast.LENGTH_LONG).show();      setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);// 把声音设定成Earpiece(听筒)出来,设定为正在通话中audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); //android 5.0+}}@Overrideprotected void onResume() {mSensorManager.registerListener(this, mSensor,SensorManager.SENSOR_DELAY_NORMAL);super.onResume();}@Overrideprotected void onPause() {mSensorManager.unregisterListener(this);super.onPause();}@Overrideprotected void onDestroy() {if (mSpeech != null) {mSpeech.stop();mSpeech.shutdown();mSpeech = null;}audioManager.setSpeakerphoneOn(true);audioManager.setMode(AudioManager.MODE_NORMAL);super.onDestroy();}@Overridepublic void onSensorChanged(SensorEvent event) {float range = event.values[0];if (range == mSensor.getMaximumRange()) {audioManager.setSpeakerphoneOn(true);Toast.makeText(this, "扬声器模式", Toast.LENGTH_LONG).show();audioManager.setMode(AudioManager.MODE_NORMAL);} else {audioManager.setSpeakerphoneOn(false);// 关闭扬声器Toast.makeText(this, "听筒模式", Toast.LENGTH_LONG).show();audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}}

权限:

布局:

     
相关取值:

<?xml version="1.0" encoding="utf-8"?>    TTS    Settings    播报    扬声器            CANADA        CHINA        CHINESE        ENGLISH        US    

页面:




更多相关文章

  1. 【Android】Vibrator(震动模式)设置长短震动
  2. Android(安卓)情景模式提示音默认修改
  3. Android(安卓)studio 实现圆形ImageView的方法步骤
  4. Android(安卓)Base64编码
  5. Android(安卓)数据存储与读取:文件
  6. Android(安卓)深色模式适配
  7. Android和设计模式:代理模式
  8. Android是否可以实现静默安装模式
  9. 年后面试,差点就痛失了字节跳动Android岗的Offer,原因竟是因为性能

随机推荐

  1. Android键盘自适应方法.
  2. [Android] 环境配置之Android Studio开发
  3. android GradLayout实现计算器
  4. android之Spinner控件用法
  5. Android控件及API说明(三)
  6. android 无界面通过开机下载
  7. Android 多线程之线程池
  8. android:layout_weight属性的简单使用
  9. 详解如何让Android(安卓)UI设计性能更高
  10. 基于Android平台下的科学计算器