Pertama buat Project Android. Kemudian pada MainActivity kita tuliskan kode program ini :
package com.amijaya.speechtotextinggris; import java.util.ArrayList; import android.os.Bundle; 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.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { // http://cariprogram.blogspot.com // nuramijaya@gmail.com @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); try { startActivityForResult(intent, 1); EditText editText1 = (EditText)findViewById(R.id.editText1); editText1.setText(""); } catch (ActivityNotFoundException a) { Toast t = Toast.makeText(getApplicationContext(), "Device Not Supported", Toast.LENGTH_SHORT); t.show(); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 1: { if (resultCode == RESULT_OK && null != data) { ArrayList<String> text = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); EditText editText1 = (EditText)findViewById(R.id.editText1); editText1.setText(text.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.activity_main, menu); return true; } /* // CARA MENGECEK APAKAH TTS DISUPPORT DAN MEMANGGIL INSTALLER TTS GOOGLE PLAY //find out whether speech recognition is supported PackageManager packManager = getPackageManager(); List<ResolveInfo> intActivities = packManager.queryIntentActivities (new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); if (intActivities.size() != 0) { //speech recognition is supported - detect user button clicks speechBtn.setOnClickListener(this); //prepare the TTS to repeat chosen words Intent checkTTSIntent = new Intent(); //check TTS data checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); //start the checking Intent - will retrieve result in onActivityResult startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE); } else { //speech recognition not supported, disable button and output message speechBtn.setEnabled(false); Toast.makeText(this, "Oops - Speech recognition not supported!", Toast.LENGTH_LONG).show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //check speech recognition result if (requestCode == VR_REQUEST && resultCode == RESULT_OK) { //store the returned word list as an ArrayList ArrayList<String> suggestedWords = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); //set the retrieved list to display in the ListView using an ArrayAdapter wordList.setAdapter(new ArrayAdapter<String> (this, R.layout.word, suggestedWords)); } //returned from TTS data check if (requestCode == MY_DATA_CHECK_CODE) { //we have the data - create a TTS instance if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) repeatTTS = new TextToSpeech(this, this); //data not installed, prompt the user to install it else { //intent will take user to TTS download page in Google Play Intent installTTSIntent = new Intent(); installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); startActivity(installTTSIntent); } } //call superclass method super.onActivityResult(requestCode, resultCode, data); } */ }
Kemudian buat layout di file activity_main.xml seperti ini :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Speech To Text English" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Speak" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> </LinearLayout>
Dan terakhir file AndroidManifest.xml tidak perlu kita rubah :
<manifest android:versioncode="1" android:versionname="1.0" package="com.amijaya.speechtotextinggris" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-sdk android:minsdkversion="8" android:targetsdkversion="16"> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:label="@string/app_name" android:name="com.amijaya.speechtotextinggris.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"> <category android:name="android.intent.category.LAUNCHER"> </category></action></intent-filter> </activity> </application> </uses-sdk></manifest>
Hasilnya, Setelah kita klik Tombol akan muncul dialog Speak Now :
Kemudian akan diproses :
Kemudian setelah suara dikenali akan muncul tulisannya di EditText :
Agar pengenalan Speech To Text bekerja maksimal dalam mengenali kata dalam Bahasa Inggris (English) silakan ubah di Setting dengan Language English (United States) :
Project selengkapnya silakan didownload. Jika sulit coba baca cara downloadnya ini.
Semoga berhasil :)
getMenuInflater().inflate(R.menu.activity_main, menu);
BalasHapusmf, kata menu di R.menu t mengacu k mana?? kok saya coba copykan listingnya tapi ada tanda merah di kata "menu" t???? mhon bantuannya, terimakasih....
satu lagi,,, di bagian:
public void onClick(View arg0) {
kata "onClick" ny jga merah,,, gmana tuch???
Pada bagian MenuInflater sebaiknya jangan dicopy Mbak jika versi Eclipse-nya tidak sama. Sepertinya versi eclipse-nya masih yang lebih lama jadi Menu tidak terletak di lokasi yang sama. Coba download di http://developer.android.com/tools/sdk/eclipse-adt.html. Atau langkah yang paling mudah, khusus MenuInflater tidak perlu dicopy.
Hapus@Override
public void onClick
ada tanda merah juga karena versinya Eclipse yang lama, coba dihapus saja @Override nya
mau tanya apakah aplikasi ini harus online atau bisa offline juga?
BalasHapusmas mau nanya itu teknik agar bisa terkoneksi dengan database google tekniknya apa ya?
BalasHapusDalam aplikasi ini, menggunakan metode apa mmba ?
BalasHapusTrimakasih untuk ilmunya :)