android開發中系統內建語音模組的使用

來源:互聯網
上載者:User

需求:項目中需要添加語音搜尋模組,增加使用者體驗
解決過程:在網上搜到語音搜尋例子,參考網上代碼,加入到了自己的項目,完成產品要求。這個問題很好解決,網上能找到很多的資料,但是沒有直接匯入工程就能用的例子,我這裡寫了一個完整的Demo,代碼可以直接粘貼到自己項目中去,實現了語音搜尋,並將搜尋結果展示。
語音搜尋大致流程:啟動系統內建的Intent,Intent參數設定為RecognizerIntent.ACTION_RECOGNIZE_SPEECH,再加上一些提示參數
[java]
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "請開始說話"); 

當完成語音搜尋之後在onActivityResult裡進行資料接收,我這裡只是展示了語音搜尋的第一個結果,系統提供的結果有很多,是根據相似性排列的。我在Demo裡使用了handler,在handler裡把資料展示了出來。項目中這種情況下,對資料的一些處理都是在handler中進行。開發的過程中遇到一些語音搜尋崩潰的現象,最後發現在結果展示的時候不要做一些費時的操作。
代碼如下
[java] 
public class MainActivity extends Activity { 
    private Button btnVoice; 
    private TextView tvVoiceResult; 
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 123456; 
     
    private Handler jumpHandler = new Handler() { 
        public void handleMessage(android.os.Message msg) { 
            tvVoiceResult.setText((String)msg.obj); 
        }; 
    }; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        btnVoice = (Button) findViewById(R.id.btnVoice); 
        tvVoiceResult = (TextView) findViewById(R.id.tvVoiceResult); 
        btnVoice.setOnClickListener(new OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                try { 
                    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
                    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "請開始說話"); 
                    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
                }catch(ActivityNotFoundException e) { 
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
                    builder.setTitle("語音辨識"); 
                    builder.setMessage("您的手機暫不支援語音搜尋功能,點擊確定下載安裝Google語音搜尋軟體。您也可以在各市集搜尋“語音搜尋”進行下載安裝。"); 
                    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { 
                        @Override 
                        public void onClick(DialogInterface dialog, int which) { 
//                          跳轉到下載語音網頁 
                        } 
                    }); 
                    builder.setNegativeButton("取消", null); 
                    builder.show(); 
                } 
            } 
        }); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        return true; 
    } 
 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == MainActivity.RESULT_OK) { 
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
            if(matches.size() > 0) { 
                Message msg = new Message(); 
                msg.obj = matches.get(0); 
                jumpHandler.sendMessage(msg); 
            } 
        } 
    } 
     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.