android下TTS的传感器切换听筒和扬声器测试

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;

	@Override
	protected 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 {

		@Override
		public 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 {
		@Override
		public 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 {
		@Override
		public void onClick(View v) {
			if(flag)
				flag = false;
			else
				flag = 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+
		}
	}

	@Override
	protected void onResume() {
		mSensorManager.registerListener(this, mSensor,
				SensorManager.SENSOR_DELAY_NORMAL);
		super.onResume();
	}

	@Override
	protected void onPause() {
		mSensorManager.unregisterListener(this);
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		if (mSpeech != null) {
			mSpeech.stop();
			mSpeech.shutdown();
			mSpeech = null;
		}
		audioManager.setSpeakerphoneOn(true);
		audioManager.setMode(AudioManager.MODE_NORMAL);
		super.onDestroy();
	}

	@Override
	public 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);
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}
}

权限:

布局:



     
相关取值:




    TTS
    Settings
    播报
    扬声器

    
        CANADA
        CHINA
        CHINESE
        ENGLISH
        US
    


页面:

android下TTS的传感器切换听筒和扬声器测试_第1张图片


你可能感兴趣的:(Android)