intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Hướng dẫn xây dựng ứng dụng chuyển Speech thành Text

Chia sẻ: New Tech | Ngày: | Loại File: DOCX | Số trang:2

183
lượt xem
28
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Trong bài viết này sẽ hướng dẫn xây dựng ứng dụng chuyển Speech thành Text (tiếng Anh) trên Android. Để ứng dụng SpeechtoText có thể hoạt động, yêu cầu có kết nối Internet. Sau đây là các bước thực hiện:

Chủ đề:
Lưu

Nội dung Text: Hướng dẫn xây dựng ứng dụng chuyển Speech thành Text

  1.  [Hướng dẫn] Xây dựng ứng dụng chuyển Voice thành Text Trong bài viết này sẽ hướng dẫn xây dựng ứng dụng chuyển Speech thành Text (tiếng Anh) trên Android. Để ứng  dụng Speech­to­Text có thể hoạt động, yêu cầu có kết nối Internet. Sau đây là các bước thực hiện: Bước 1: Tạo một Project mới và đặt tên là SpeechToText Bước 2: Mở file res/layout/main.xml và thay đổi các thông tin như sau:           Bước 3: Mở tập tin src/speechtotext.apk/SpeechToTextActivity.java và thay đổi như sau: package speechtotext.apk; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.speech.RecognizerIntent; import android.speech.tts.TextToSpeech; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class SpeechToTextActivity extends Activity {     /** Called when the activity is first created. */ private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         PackageManager pm = getPackageManager();         List activities = pm.queryIntentActivities(new Intent(                 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);         if (activities.size() == 0) {  ((Button)findViewById(R.id.buttonSpeechToText)).setEnabled(false);                  } Lập trình Android – http://laptrinhdidong.vn Page 1
  2.         else         {  ((Button)findViewById(R.id.buttonSpeechToText)).setOnClickListener(new OnClickListener(){          @Override public void onClick(View v) { // TODO Auto­generated method stub if (v.getId() == R.id.buttonSpeechToText) {             startVoiceRecognitionActivity();         } } private void startVoiceRecognitionActivity() { // TODO Auto­generated method stub Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo"); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); }           }           );         }         Intent checkIntent = new Intent(); checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent, VOICE_RECOGNITION_REQUEST_CODE);     }     protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {             // Fill the list view with the strings the recognizer thought it could have heard             ArrayList matches = data.getStringArrayListExtra(                     RecognizerIntent.EXTRA_RESULTS);             ((EditText) findViewById(R.id.editTextSpeechToText)).setText(matches.get(0));         }         super.onActivityResult(requestCode, resultCode, data); } } Bước 4: Tiến hành Build và cài đặt ứng dụng lên thiết bị. Lưu ý: Để ứng dụng hoạt động được, yêu cầu có kết nối Intenet khi chạy ứng dụng. Mọi ý kiến đóng góp vui lòng gởi vào diễn đàn. http://laptrinhdidong.vn Chúc các bạn thành công. Lập trình Android – http://laptrinhdidong.vn Page 2
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2