Android 自带的语音识别例子初探
2010-03-25 17:02:00 来源:WEB开发网import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class VoiceRecognition extends Activity implements OnClickListener {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private ListView mList;
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.voice_recognition);
Button speakButton = (Button) findViewById(R.id.btn_speak);
mList = (ListView) findViewById(R.id.list);
// Check to see if a recognition activity is present
PackageManager pm = getPackageManager();
List
if (activities.size() != 0)
{
speakButton.setOnClickListener(this);
}
else
{
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
}
public void onClick(View v)
{
if (v.getId() == R.id.btn_speak)
{
startVoiceRecognitionActivity();
}
}
private void startVoiceRecognitionActivity()
{
//通过Intent传递语音识别的模式
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//语言模式和自由形式的语音识别
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
更多精彩
赞助商链接