Android development and learning: Using Baidu Speech Recognition SDK for Speech Recognition (Part 1)

Source: Internet
Author: User


As the mobile Internet killer interaction method, voice recognition has been attracting more and more attention since its publication, from IOS Siri to xunfei voice in China, speech recognition technology is the most promising and promising technology in mobile development. Android, as a mobile operating system, inherits Google's inherent search genes. Therefore, Android provides better support for speech recognition and speech synthesis. However, due to various reasons, many features of Android can only be discussed on paper. As a new model of human-computer interaction in the future, Chinese speech recognition technology itself has a relatively high threshold, which does not block many developers. However, there are already some manufacturers in China that provide speech recognition-related technical services. By understanding the current situation of speech recognition technology in China, this helps us select a proper direction when developing speech recognition-related applications. Now, the two well-known manufacturers in China that provide speech recognition technology are KEDA xunfei and Baidu, and both vendors have opened corresponding API interfaces, through API interfaces, we can easily implement Speech Recognition in our own applications. KEDA xunfei provides a series of solutions from text synthesis to speech recognition, while Baidu only provides solutions for speech recognition. For details, refer to the following address:

 

 

After completing the preparations, we can start learning today. First let's create an Android project, and introduce the jar library file: VoiceRecognition-1.2.jar of Baidu voice in the project, and then add the following permissions in the AndroidManifest. xml file:

Then we make a simple layout, a Button, and a text box: 
In this article, we only have a preliminary understanding of Baidu speech. Therefore, the method we adopt today is to directly call a Speech Recognition dialog box class BaiduASRDigitalDialog encapsulated by Baidu for us, let's take a look at the Code:

 

 

package com.Android.BaiduVoice;

import java.util.ArrayList;

import com.baidu.voicerecognition.android.ui.BaiduASRDigitalDialog;
import com.baidu.voicerecognition.android.ui.DialogRecognitionListener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    
// start button
private Button BtnStart;
// text box
private EditText InputBox;
// Baidu speech recognition dialog
private BaiduASRDigitalDialog mDialog = null;
private DialogRecognitionListener mDialogListener = null;
// Apply authorization information. The parameters in the official SDK are used here. If necessary, please apply for it yourself and modify it to your own authorization information.
    private String API_KEY = 8MAxI5o7VjKSZOKeBzS4XtxO;
private String SECRET_KEY = Ge5GXVdGQpaxOmLzc8fOM8309ATCz9Ha;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
if (mDialog == null) {
                if (mDialog! = null) {
                mDialog.dismiss ();
            }
            Bundle params = new Bundle ();
            // Set API_KEY, SECRET_KEY
            params.putString (BaiduASRDigitalDialog.PARAM_API_KEY, API_KEY);
            params.putString (BaiduASRDigitalDialog.PARAM_SECRET_KEY, SECRET_KEY);
            // Set the speech recognition dialog to blue highlight theme
            params.putInt (BaiduASRDigitalDialog.PARAM_DIALOG_THEME, BaiduASRDigitalDialog.THEME_BLUE_LIGHTBG);
            // Instantiate Baidu speech recognition dialog
            mDialog = new BaiduASRDigitalDialog (this, params);
            // Set Baidu speech recognition callback interface
            mDialogListener = new DialogRecognitionListener ()
            {

@Override
public void onResults (Bundle mResults)
{
ArrayList rs = mResults! = Null? MResults.getStringArrayList (RESULTS_RECOGNITION): null;
if (rs! = null && rs.size ()> 0) {
InputBox.setText (rs.get (0));
}
Ranch
}
            Hell
            };
            mDialog.setDialogRecognitionListener (mDialogListener);
        }
// Set the voice recognition mode to input mode
        mDialog.setSpeechMode (BaiduASRDigitalDialog.SPEECH_MODE_INPUT);
        // Disable semantic recognition
        mDialog.getParams (). putBoolean (BaiduASRDigitalDialog.PARAM_NLU_ENABLE, false);
        
        // Interface elements
BtnStart = (Button) findViewById (R.id.BtnStart);
InputBox = (EditText) findViewById (R.id.InputBox);
BtnStart.setOnClickListener (new OnClickListener ()
{
@Override
public void onClick (View v)
{
mDialog.show ();
}
});
}
} 
In this way, we have achieved the goal of implementing Speech Recognition in our applications. It seems quite simple. The following shows how the program runs:

 



Calling Baidu voice in this way is very simple, but it is difficult to meet the needs of personalized customization. By default, the official website has provided us with 8 color schemes, if these eight color schemes cannot meet our actual needs, we need to customize them using the APIS provided by Baidu voice. You can continue to follow my blog for specific content, in the next article, we will implement our speech recognition Interface Based on Baidu voice API. Thank you for your attention!

Source code download

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.