Hướng dẫn xây dựng ứng dụng trên Android ppsx

3 369 1
Hướng dẫn xây dựng ứng dụng trên Android ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN [Hướng dẫn] Ứng dụng Text-to-Speech trên Android Trong bài viết này sẽ hướng dẫn xây dựng ứng dụng trên Android có khả năng chuyển từ Text sang Voice (tiếng Anh). Để xây dựng ứng dụng với tính năng Text-to-Speech, phải tạo Project mới với Android SDK 1.6 trở lên. Sau đây là các bước thực hiện: Bước 1: Tạo một Project với tên là MyTextToSpeech. Bước 2: Thay đổi tập tin res/layout/main.xml với thông tin như sau: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/input_text" android:text="" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/speak_button" android:text="Speak to me" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> Bước 3: Import những gói thư viện sau vào trong Project: import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; Bước 4: Thay đổi nội dung tập tin MyTextToSpeech như sau: package com.app.MyTextToSpeech; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; Lập trình Android – http://laptrinhdidong.vnPage 1 Trung tâm Tin học – ĐH KHTN import android.widget.EditText; import android.widget.Toast; public class MyTextToSpeech extends Activity implements OnInitListener{ /** Called when the activity is first created. */ private int MY_DATA_CHECK_CODE = 0; private TextToSpeech tts; private EditText inputText; private Button speakButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); inputText = (EditText) findViewById(R.id.input_text); speakButton = (Button) findViewById(R.id.speak_button); speakButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String text = inputText.getText().toString(); if (text!=null && text.length()>0) { Toast.makeText(MyTextToSpeech.this, "Saying: " + text, Toast.LENGTH_LONG).show(); tts.speak(text, TextToSpeech.QUEUE_ADD, null); } } }); Intent checkIntent = new Intent(); checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == MY_DATA_CHECK_CODE) { if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { // success, create the TTS instance tts = new TextToSpeech(this, this); } else { // missing data, install it Intent installIntent = new Intent(); installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); startActivity(installIntent); } } } Lập trình Android – http://laptrinhdidong.vnPage 2 Trung tâm Tin học – ĐH KHTN @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { Toast.makeText(MyTextToSpeech.this, "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show(); } else if (status == TextToSpeech.ERROR) { Toast.makeText(MyTextToSpeech.this, "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show(); } } } Bước 5: Tiến hành Build và cài đặt thử vào Simulator Lưu ý: Với ứng dụng này, bạn nên cài ứng dụng lên thiết bị thật để có thể kiểm tra chức năng của ứ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.vnPage 3 . KHTN [Hướng dẫn] Ứng dụng Text-to-Speech trên Android Trong bài viết này sẽ hướng dẫn xây dựng ứng dụng trên Android có khả năng chuyển từ Text sang Voice (tiếng Anh). Để xây dựng ứng dụng với. encoding="utf-8"?> <LinearLayout xmlns :android= "http://schemas .android. com/apk/res /android& quot; android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" . android. app.Activity; import android. content.Intent; import android. os.Bundle; import android. speech.tts.TextToSpeech; import android. speech.tts.TextToSpeech.OnInitListener; import android. view.View; import android. view.View.OnClickListener; import

Ngày đăng: 06/08/2014, 00:23

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan