2013年4月23日 星期二

jarvis 000


package com.example.texttospeech;

import java.util.ArrayList;
import java.util.Locale;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import android.util.Log;

public class MainActivity extends Activity implements
TextToSpeech.OnInitListener {
private static final String TAG = "TextToSpeechDemo";

private TextToSpeech mTts;
String resultstr;

private TextView mText;
private SpeechRecognizer sr;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 初始化TextToSpeech对象. 是一个异步操作.
// 初始化监听对象,在完成初始化时调用
mTts = new TextToSpeech(this, this);
listen_speak();


}

@Override
public void onDestroy() {
if (mTts != null) {
// 停止TextToSpeech
mTts.stop();
// 释放TextToSpeech占用的资源
mTts.shutdown();
}
super.onDestroy();
}

// 实现TextToSpeech.OnInitListener接口.
public void onInit(int status) {
// 状态成功
if (status == TextToSpeech.SUCCESS) {
// 设置语言
int result = mTts.setLanguage(Locale.ENGLISH);
// (我试了中文,不好使????)
// int result = mTts.setLanguage(Locale.CHINA);

if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
// 如果不支持该语言
Toast.makeText(getApplicationContext(), "not support this lan",
Toast.LENGTH_SHORT).show();
} else {
// TTS 引擎已经成功初始化
// 按钮设置为可点击
//Button "button1"
Button button1 = (Button) findViewById(R.id.button1);
       button1.setOnClickListener(new Button.OnClickListener()
       {
         public void onClick(View v)
         {
         //To-Do
         listen_speak();
         }
       });
//End of Button "button1"
// 发音朗读
// say("Hello");

}
} else {
// 初始化失败
Toast.makeText(getApplicationContext(), "first fail",
Toast.LENGTH_SHORT).show();
}
}

private void say(String hello) {
// Select a random hello.

// 朗读
mTts.speak(hello, TextToSpeech.QUEUE_FLUSH, null);


}

private void speak() {
// 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_CALLING_PACKAGE,
"voice.recognition.test");

intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
sr.startListening(intent);
Log.i("111111", "11111111");

}

class listener implements RecognitionListener {
public void onReadyForSpeech(Bundle params) {
Log.d(TAG, "onReadyForSpeech");
}

public void onBeginningOfSpeech() {
Log.d(TAG, "onBeginningOfSpeech");
}

public void onRmsChanged(float rmsdB) {
Log.d(TAG, "onRmsChanged");
}

public void onBufferReceived(byte[] buffer) {
Log.d(TAG, "onBufferReceived");
}

public void onEndOfSpeech() {
Log.d(TAG, "onEndofSpeech");
}

public void onError(int error) {
Log.d(TAG, "error " + error);
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT)
.show();
}

public void onResults(Bundle results) {
String str = new String();
Log.d(TAG, "onResults " + results);
ArrayList data = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++) {
Log.d(TAG, "result " + data.get(i));
str += (data.get(i) + ", ");
}
Toast.makeText(getApplicationContext(), "You said: " + data.get(0),
Toast.LENGTH_SHORT).show();
if (data.get(0).equals("hello") || data.get(0).equals("hi"))
say("Ok hi");
else if(data.get(0).equals("fuck you"))
say("don't fuck me");
else if(data.get(0).equals("who are you"))
say("i'm jarvis");
else if(data.get(0).equals("wing man")||data.get(0).equals("wingman"))
say("she is beautiful");
else if(data.get(0).equals("我好悶呀")){
String videoId = "H542nLTTbu0";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId));
intent.putExtra("VIDEO_ID", videoId);
startActivity(intent);
say("watch some video");
}

else
say("i don't know what are you talking about");

}

public void onPartialResults(Bundle partialResults) {
Log.d(TAG, "onPartialResults");
}

public void onEvent(int eventType, Bundle params) {
Log.d(TAG, "onEvent " + eventType);
}
}

public void listen_speak() {
// TODO Auto-generated method stub

sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());

speak();
}
}

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...