Android 语音识别接口

来源:互联网 发布:自动打码软件 编辑:程序博客网 时间:2024/05/18 00:13

标签:Android SDK

[1].[代码] VoiceRecognition.java 跳至 [1] [2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
packagecom.VoiceRecognition;
importAndroid.app.Activity; 
importAndroid.content.Intent; 
importAndroid.content.pm.PackageManager; 
 
importAndroid.content.pm.ResolveInfo; 
importAndroid.os.Bundle; 
importAndroid.speech.RecognizerIntent; 
importAndroid.view.View; 
importAndroid.view.View.OnClickListener; 
importAndroid.widget.ArrayAdapter; 
importAndroid.widget.Button; 
importAndroid.widget.ListView; 
importjava.util.ArrayList; 
importjava.util.List; 
  
publicclass VoiceRecognition extendsActivity implementsOnClickListener { 
       
    privatestatic final int VOICE_RECOGNITION_REQUEST_CODE = 1234
       
    privateListView mList; 
  
    /** 
     * Called with the activity is first created. 
     */ 
    @Override 
    publicvoid onCreate(Bundle savedInstanceState)  
    
        super.onCreate(savedInstanceState); 
  
        setContentView(R.layout.main); 
  
        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<ResolveInfo> activities = pm.queryIntentActivities(newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
        if(activities.size() != 0
        
            speakButton.setOnClickListener(this); 
        
        else 
        
            speakButton.setEnabled(false); 
            speakButton.setText("Recognizer not present"); 
        
    
  
  
    publicvoid onClick(View v) 
    
        if(v.getId() == R.id.btn_speak) 
        
            startVoiceRecognitionActivity(); 
        
    
  
    privatevoid startVoiceRecognitionActivity() 
    
        //通过Intent传递语音识别的模式 
        Intent intent = newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
        //语言模式和自由形式的语音识别 
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
        //提示语音开始 
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speech recognition demo"); 
        //开始执行我们的Intent、语音识别 
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
    
  
  
    //当语音结束时的回调函数onActivityResult 
    @Override 
    protectedvoid onActivityResult(intrequestCode, intresultCode, Intent data) 
    
        if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) 
        
            // 取得语音的字符 
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
            mList.setAdapter(newArrayAdapter<String>(this, Android.R.layout.simple_list_item_1, matches)); 
        
  
        super.onActivityResult(requestCode, resultCode, data); 
    
}

[2].[代码] main.xml 跳至 [1] [2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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"
    >
<Button
 Android:id="@+id/btn_speak"
    Android:layout_width="fill_parent"
    Android:layout_height="wrap_content"
    />
    <ListView
    Android:id="@+id/list"
    Android:layout_width="fill_parent"
    Android:layout_height="wrap_content"
    />
</LinearLayout>

0 0