1. MainActivity.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/speechtotext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:text="Hello "
android:textSize="25sp"
android:textStyle="bold"
android:textColor="@android:color/black" />
<ImageButton
android:id="@+id/mike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
2. MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageButton mike;
TextView speechtotext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speechtotext = (TextView) findViewById(R.id.speechtotext);
mike = (ImageButton) findViewById(R.id.mike);
mike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speech();
}
});
}
private void speech() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hi Speak Something");
try {
startActivityForResult(intent, 1000);
}catch (Exception ex){
//Toast.makeText(MainActivity.this, ""+ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1000:
if(resultCode == RESULT_OK && data != null){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
speechtotext.setText(result.get(0));
}
default:
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/speechtotext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:text="Hello "
android:textSize="25sp"
android:textStyle="bold"
android:textColor="@android:color/black" />
<ImageButton
android:id="@+id/mike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
2. MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageButton mike;
TextView speechtotext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speechtotext = (TextView) findViewById(R.id.speechtotext);
mike = (ImageButton) findViewById(R.id.mike);
mike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speech();
}
});
}
private void speech() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hi Speak Something");
try {
startActivityForResult(intent, 1000);
}catch (Exception ex){
//Toast.makeText(MainActivity.this, ""+ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1000:
if(resultCode == RESULT_OK && data != null){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
speechtotext.setText(result.get(0));
}
default:
}
}
}
No comments:
Post a Comment