package speech.survivingwithandroid.com.android_speech;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import java.util.Locale;
/*
* Copyright (C) 2015 Francesco Azzola
* Surviving with Android (http://www.survivingwithandroid.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
private ImageView speechButton;
private TextToSpeech engine;
private EditText editText;
private SeekBar seekPitch;
private SeekBar seekSpeed;
private double pitch=1.0;
private double speed=1.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get reference
speechButton = (ImageView) findViewById(R.id.speechImg);
editText = (EditText) findViewById(R.id.sentence);
engine = new TextToSpeech(this, this);
speechButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speech();
}
});
seekPitch = (SeekBar) findViewById(R.id.seekPitch);
seekPitch.setThumbOffset(5);
seekPitch.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Log.d("Speech", "Progress ["+progress+"]");
pitch = (float) progress / (seekBar.getMax() / 2);
//Log.d("Speech", "Pitch ["+pitch+"]");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekSpeed = (SeekBar) findViewById(R.id.seekSpeed);
seekSpeed.setThumbOffset(5);
seekSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Log.d("Speech", "Progress ["+progress+"]");
speed = (float) progress / (seekBar.getMax() / 2);
//Log.d("Speech", "Pitch ["+pitch+"]");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onInit(int status) {
Log.d("Speech", "OnInit - Status ["+status+"]");
if (status == TextToSpeech.SUCCESS) {
Log.d("Speech", "Success!");
engine.setLanguage(Locale.UK);
}
}
private void speech() {
engine.setPitch((float) pitch);
engine.setSpeechRate((float) speed);
engine.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null, null);
}
}