Cara Buat Aplikasi Android Specch To Text
Ini Adalah Source Code Untuk MainActivity.java
import java.util.ArrayList;
import java.util.Locale;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("NewApi") public class MainActivity extends Activity {
private static final int REQ_CODE_SPEECH_INPUT = 100;
private TextView txtSpeachInput;
private Button btnSpeak;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeachInput = (TextView) findViewById(R.id.txtSpeechInput);
btnSpeak = (Button) findViewById(R.id.btnspeak);
// hide the action bar
getActionBar().hide();
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
promptSpeechInput();
}
});
}
private void promptSpeechInput(){
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, getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
}catch (ActivityNotFoundException a){
Toast.makeText(getApplicationContext(), getString(R.string.speech_not_suported), Toast.LENGTH_SHORT).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeachInput.setText(result.get(0));
}
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Next Ini Adalah Source Code Untuk activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/back" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:layout_marginLeft="100dp"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<Button
android:id="@+id/btnspeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mic" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:text="@string/tap_on_mic"
android:textSize="15dp"
android:textStyle="normal" />
</LinearLayout>
<TextView
android:id="@+id/txtSpeechInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:textSize="26dp"
android:textStyle="normal" />
</LinearLayout>
Ini Adalah Hasil Running Akhir Dari Source Code Di Atas
Komentar
Posting Komentar