Page Example

(Tutorial) Sound Manager_3.0 Sample Overview

Mobile native

The Sound Manager sample application demonstrates how to use the Sound Manager API to get information about connected audio devices, acquire and release the focus for playback or recording, and manage the volume level.

The following figure illustrates the main view of the Sound Manager application.

Figure: Sound Manager main view

Sound Manager main view

Source Files

You can create and view the sample application project, including the source files, in the IDE.

Table: Source files
File name Description
inc/data.h This file contains the variable and function definitions used in the C files, especially in the data.c file.
inc/main.h This file contains the package name and log declarations.
inc/view.h This file contains the variable and function definitions used in the C files, especially in the view.c file.
src/data.c This file contains the functions for retrieving and making data for the application.
src/main.c This file contains the functions related to the application life-cycle, event callbacks, and view controls.
src/view.c This file contains the functions for implementing the views and handling events.

Implementation

To implement the sound manager:

  1. In the data.c file, the data_initialize() and data_finalize() functions initialize and finalize the data module. The data_initialize() function creates the stream information and registers the focus callback function using the Sound Manager API. The data_finalize() function destroys the stream information.
  2. To create the application main view:
    1. In the view_create() function, create the window, conformant, and naviframe:
      Eina_Bool
      view_create(void *user_data)
      {
          /* Create the window */
          s_info.win = view_create_win(PACKAGE);
          if (s_info.win == NULL) {
              dlog_print(DLOG_ERROR, LOG_TAG, "failed to create a window.");
      
              return EINA_FALSE;
          }
      
          /* Create the conformant */
          s_info.conform = view_create_conformant_without_indicator(s_info.win);
          if (s_info.conform == NULL) {
              dlog_print(DLOG_ERROR, LOG_TAG, "failed to create a conformant");
      
              return EINA_FALSE;
          }
      
          /* Create the naviframe */
          s_info.navi = view_create_naviframe(s_info.conform);
          if (s_info.navi == NULL) {
              dlog_print(DLOG_ERROR, LOG_TAG, "failed to create a naviframe");
      
              return EINA_FALSE;
          }
      
          create_buttons_in_main_window();
      
          /* Show the window after the main view is set up */
          evas_object_show(s_info.win);
      
          return EINA_TRUE;
      }
      
    2. The create_buttons_in_main_window() function creates the buttons for the sound manager:
      void
      create_buttons_in_main_window(appdata_s *ad)
      {
          /* Create the window */
          Evas_Object *display = _create_new_cd_display(ad, "Sound Manager", NULL);
      
          /* Create the buttons */
          _new_button(ad, display, "List connected devices", __sound_devices_button_cb);
          _new_button(NULL, display, "Acquire Focus(Playback)", __acquire_focus_playback_button_cb);
          _new_button(NULL, display, "Release Focus(Playback)", __release_focus_playback_button_cb);
          _new_button(NULL, display, "Acquire Focus(Recording)", __acquire_focus_recording_button_cb);
          _new_button(NULL, display, "Release Focus(Recording)", __release_focus_recording_button_cb);
      
          /* Create a horizontal box for the volume buttons */
          Evas_Object *volume_box = elm_box_add(display);
          elm_box_horizontal_set(volume_box, EINA_TRUE);
          evas_object_size_hint_align_set(volume_box, EVAS_HINT_FILL, EVAS_HINT_FILL);
          evas_object_size_hint_weight_set(volume_box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
          elm_box_pack_end(display, volume_box);
          evas_object_show(volume_box);
      
          /* Create the volume buttons */
          _new_button("-", volume_box, "Volume -", __volume_button_cb);
          _new_button("+", volume_box, "Volume +", __volume_button_cb);
      
      }
      
  3. The application operations are activated when the user clicks the buttons on the main view:
    • When the user clicks List connected devices, invoke the Sound Manager APIs to retrieve information about any connected audio devices and show it on the screen:
      static void
      __sound_devices_button_cb(appdata_s *ad, Evas_Object *obj, void *event_info)
      {
          /* Get the list of all audio devices connected to the device */
          sound_device_list_h device_list = NULL;
      
          int error_code = sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list);
          if (SOUND_MANAGER_ERROR_NONE != error_code) {
              dlog_print(DLOG_ERROR, LOG_TAG,
                         "sound_manager_get_current_device_list() failed! Error code = %d", error_code);
      
              return;
          }
      
          do {
              /* Get the device from the list */
              sound_device_h device = NULL;
      
              error_code = sound_manager_get_next_device(device_list, &device);
              if (SOUND_MANAGER_ERROR_NONE != error_code) {
                  dlog_print(DLOG_ERROR, LOG_TAG,
                             "sound_manager_get_next_device() failed! Error code = %d", error_code);
                  continue;
              }
      
              /* Get the ID of the retrieved device */
              int id;
              error_code = sound_manager_get_device_id(device, &id);
              if (SOUND_MANAGER_ERROR_NONE != error_code) {
                  dlog_print(DLOG_ERROR, LOG_TAG,
                             "sound_manager_get_device_id() failed! Error code = %d", error_code);
                  continue;
              }
      
              PRINT_MSG("Device %d", id);
      
              /* Get the name of the retrieved device */
              char *name;
              error_code = sound_manager_get_device_name(device, &name);
              if (SOUND_MANAGER_ERROR_NONE != error_code) {
                  dlog_print(DLOG_ERROR, LOG_TAG,
                             "sound_manager_get_device_name() failed! Error code = %d", error_code);
                  continue;
              }
      
              PRINT_MSG("Name: %s", name);
      
              /* Get the type of the retrieved device */
              sound_device_type_e type;
              error_code = sound_manager_get_device_type(device, &type);
              if (SOUND_MANAGER_ERROR_NONE != error_code) {
                  dlog_print(DLOG_ERROR, LOG_TAG,
                             "sound_manager_get_device_type() failed! Error code = %d", error_code);
                  continue;
              }
      
              PRINT_MSG("Type: %s", _sound_manager_device_type_to_string(type));
      
              /* Get the state of the retrieved device */
              sound_device_state_e state;
      
              error_code = sound_manager_get_device_state(device, &state);
              if (SOUND_MANAGER_ERROR_NONE != error_code) {
                  dlog_print(DLOG_ERROR, LOG_TAG,
                             "sound_manager_get_device_state() failed! Error code = %d", error_code);
                  continue;
              }
      
              PRINT_MSG("State: %s", _sound_manager_device_state_to_string(state));
      
              /* Get the input/output direction of the retrieved device */
              sound_device_io_direction_e io_direction;
              error_code = sound_manager_get_device_io_direction(device, &io_direction);
              if (SOUND_MANAGER_ERROR_NONE != error_code) {
                  dlog_print(DLOG_ERROR, LOG_TAG,
                             "sound_manager_get_device_io_direction() failed! Error code = %d", error_code);
                  continue;
              }
              PRINT_MSG("I/O direction: %s<br>",
                        _sound_manager_device_io_direction_to_string(io_direction));
          } while (SOUND_MANAGER_ERROR_NO_DATA != error_code);
      }
      
    • When the user clicks the focus buttons:
      • The Acquire Focus(Playback) button invokes the sound_manager_acquire_focus() function with the SOUND_STREAM_FOCUS_FOR_PLAYBACK stream focus type.
      • The Release Focus(Playback) button invokes the sound_manager_release_focus() function with the SOUND_STREAM_FOCUS_FOR_PLAYBACK stream focus type.
      • The Acquire Focus(Recording) button invokes the sound_manager_acquire_focus() function with the SOUND_STREAM_FOCUS_FOR_RECORDING stream focus type.
      • The Release Focus(Recording) button invokes the sound_manager_release_focus() function with the SOUND_STREAM_FOCUS_FOR_RECORDING stream focus type.

      The updated focus information is displayed on the screen.

    • When the user clicks Volume - or Volume +, invoke the Sound Manager APIs to decrease or increase the volume level by 1.

      The volume is defined as an integer value between 0 and 15. The initial value is 9.

      void
      __volume_button_cb(void *change_type, Evas_Object *obj, void *event_info)
      {
          /* Get the maximum possible volume level for the SOUND_TYPE_MEDIA type */
          int max_vol = 0;
          int error_code = sound_manager_get_max_volume(SOUND_TYPE_MEDIA, &max_vol);
          if (SOUND_MANAGER_ERROR_NONE != error_code) {
              dlog_print(DLOG_ERROR, LOG_TAG,
                         "sound_manager_get_max_volume() failed! Error code = %d", error_code);
      
              return;
          }
      
          /* Get the current volume level for the SOUND_TYPE_MEDIA type */
          int cur_vol = 0;
          error_code = sound_manager_get_volume(SOUND_TYPE_MEDIA, &cur_vol);
          if (SOUND_MANAGER_ERROR_NONE != error_code) {
              dlog_print(DLOG_ERROR, LOG_TAG,
                         "sound_manager_get_volume() failed! Error code = %d", error_code);
      
              return;
          }
      
          /* Set the new value for the volume level */
          cur_vol = !strcmp((char *)change_type, "+") ? ++cur_vol : --cur_vol;
          if (cur_vol <= max_vol) {
              /* Increase the volume by one */
              error_code = sound_manager_set_volume(SOUND_TYPE_MEDIA, cur_vol);
              if (SOUND_MANAGER_ERROR_NONE != error_code) {
                  PRINT_MSG("Couldn't set volume to: %d.", cur_vol);
                  dlog_print(DLOG_ERROR, LOG_TAG,
                             "sound_manager_set_volume() failed! Error code = %d", error_code);
      
                  return;
              }
          } else {
              PRINT_MSG("Volume level is set to maximum.");
          }
      }