Page Example

Voice Control Sample Overview

Mobile native

The Voice Control sample application demonstrates how you can use the Voice control API to initialize, deinitialize, add, and remove voice commands and retrieve information.

The following figure illustrates the views of the Voice Control.

Figure: Voice control views

Voice Control views

To register commands for voice control, click Select Command List and select the commands you want to register (command lists are shown in the second screenshot above).

A voice manager application (created by the device manufacturer) is used to start and stop the voice control. To start or stop the voice control, use the voice button in the quick panel (third screenshot above).

After the voice control has been start from the quick panel voice button, the voice manager application is shown at the bottom of the Voice Control screen (last screenshot above).

Implementation

To implement the Voice Control application:

  1. Initialize voice control.

    To use the Voice Control library, you must initialize voice control using the init_vc() function.

    The function sets callback functions to receive the state change events and results from the Voice Control service. The application tries to connect to the Voice Control service for handshaking information and data using the vc_prepare() function.

    static void 
    __vc_service_state_changed_cb(vc_service_state_e previous, vc_service_state_e current, void* user_data)
    {
       // Callback actions
    }
    
    static void 
    __vc_result_cb(vc_result_event_e event, vc_cmd_list_h vc_cmd_list, const char* result, void *user_data)
    {
       // Callback actions
    }
    
    static int 
    init_vc(appdata_s *ad)
    {
       ret = vc_initialize();
       if (VC_ERROR_NONE != ret) 
       {
          return -1;
       }
    
       ret = vc_set_result_cb(__vc_result_cb, ad);
       if (VC_ERROR_NONE != ret) 
       {
          return -1;
       }
    
       ret = vc_set_service_state_changed_cb(__vc_service_state_changed_cb, ad);
       if (VC_ERROR_NONE != ret) 
       {
          return -1;
       }
    
       ret = vc_prepare();
       if (VC_ERROR_NONE != ret) 
       {
          return -1;
       }
    
       return 0;
    }
    
  2. Create and set the command list.

    To set commands, create a command list and add a command on the list. You can also remove commands from the list or change the list. When you no longer need the commands, destroy the list.

    static void 
    __vc_create_command_list(cmd_list_index_e cmd_list_idx)
    {
       if (0 != vc_cmd_list_create(&g_cmd_list)) 
       {
          // Take action
       }
    
       int i;
       for (i = 0; i < MAX_COMMAND_NUM; i++) 
       {
          vc_cmd_h cmd;
          if (0 != vc_cmd_create(&cmd)) 
          {
             // Take action
          }
          if (0 != vc_cmd_set_command(cmd, "play")) 
          {
             vc_cmd_destroy(cmd);
          }
          if (0 != vc_cmd_set_type(cmd, VC_COMMAND_TYPE_FOREGROUND)) 
          {
             vc_cmd_destroy(cmd);
          }
          if (0 != vc_cmd_list_add(g_cmd_list, cmd)) 
          {
             vc_cmd_destroy(cmd);
          }
       }
    
       if ( 0 != vc_set_command_list(g_cmd_list, VC_COMMAND_TYPE_FOREGROUND)) 
       {
          vc_cmd_list_destroy(g_cmd_list, true);
       }
    }
    
  3. Check the service state.

    If you set the service state change callback, you can check the voice control service state: ready, recording, or processing. Based on the service state, you can change the application mode, state, and look and feel.

    static void 
    __vc_service_state_changed_cb(vc_service_state_e previous, vc_service_state_e current, void* user_data)
    {
       // Take action
    }
    
    static int 
    init_vc(appdata_s *ad)
    {
       ret = vc_set_service_state_changed_cb(__vc_service_state_changed_cb, ad);
       if (VC_ERROR_NONE != ret) 
       {
          return -1;
       }
    
       return 0;
    }
    
  4. Get a recognized result.

    If you want to get the recognized command, you must set a result callback function.

    You can get both the recognized result text and a command list with command information.

    static void 
    __vc_result_cb(vc_result_event_e event, vc_cmd_list_h vc_cmd_list, const char* result, void *user_data)
    {
       // Take action
    }
    
    static int 
    init_vc(appdata_s *ad)
    {
       ret = vc_set_result_cb(__vc_result_cb, ad);
       if (VC_ERROR_NONE != ret) 
       {
          return -1;
       }
    
       return 0;
    }
    
  5. Deinitialize voice control.

    The deinit_vc() function is called to disconnect the service and to destroy the command list when the application is terminated. If you only want to disconnect the service, use the vc_unprepare() function.

    static void 
    __vc_destroy_command_list(void)
    {
       if (0 != vc_cmd_list_destroy(g_cmd_list, true)) 
       {
          // Take action
       }
    }
    
    static void 
    deinit_vc(appdata_s *ad)
    {
       __vc_destroy_command_list();
    
       if (0 != vc_deinitialize()) 
       {
          // Take action
       }
    }