Page Example

Volume Sample Overview

Mobile native

The Volume sample demonstrates how you can use the Sound Manager API to acquire and set the device sound levels. The application window consists of a list of sliders that manage the levels of different volume types.

When the application is launched, it acquires the values of the volume types and sets the sliders accordingly. Apart from manually managing the volumes using sliders, the application also reacts to changes made outside it, for example, in the device settings or using hardware buttons.

The following figure illustrates the main screen of the application.

Figure: Volume screen

Volume screen

Prerequisites

To ensure proper application execution, the following privilege must be set:

  • http://tizen.org/privilege/volume.set

Implementation

The application uses the volume_controller_type enum to manage different volume types easily:

typedef enum 
{
   CONTROLLER_SYSTEM,
   CONTROLLER_NOTIFICATION,
   CONTROLLER_ALARM,
   CONTROLLER_RINGTONE,
   CONTROLLER_MEDIA,
   CONTROLLER_CALL,
   CONTROLLER_MAX
} volume_controller_type;

The enum is designed to be compliant with the sound_type_e enum used by the Sound Manager API to avoid unnecessary types duplication.

typedef enum
{
   SOUND_TYPE_SYSTEM, // Sound type for system
   SOUND_TYPE_NOTIFICATION, // Sound type for notifications
   SOUND_TYPE_ALARM, // Sound type for alarm
   SOUND_TYPE_RINGTONE, // Sound type for ringtones
   SOUND_TYPE_MEDIA, // Sound type for media
   SOUND_TYPE_CALL, // Sound type for call
   SOUND_TYPE_VOIP, // Sound type for voip
   SOUND_TYPE_VOICE, // Sound type for voice
} sound_type_e;

The 2 last sound types of the sound_type_e are not supported in this application.

Volume Manager Module

Before the volume level sliders are created, the maximum possible value must be known for each volume type. To retrieve this information, use the sound_manager_get_max_volume() function. It takes the sound_type_e enum as an in parameter and int as an out parameter.

if (sound_manager_get_max_volume((sound_type_e)type, &max_volume) != SOUND_MANAGER_ERROR_NONE) 
{
   // Error handling
}

The volume_manager_get_volume() and volume_manager_set_volume() functions manage the device sound levels. To get the volume level for each sound type, use the sound_manager_get_volume() function. Similarly, to set the volume level, use the sound_manager_set_volume() function. Both the functions require a sound_type_e value to be passed.

int
volume_manager_get_volume(volume_controller_type type)
{
   int volume = -1;

   if (sound_manager_get_volume((sound_type_e)type, &volume) != SOUND_MANAGER_ERROR_NONE) 
   {
      // Error handling
   }

   return volume;
}

bool
volume_manager_set_volume(volume_controller_type type, int value)
{
   if (sound_manager_set_volume((sound_type_e)type, value) != SOUND_MANAGER_ERROR_NONE) 
   {
      // Error handling
   }

   return true;
}

To react to volume level changes made outside the application, set a callback function for the volume change event with the sound_manager_set_volume_changed_cb() function.

The callback in this sample application invokes the view_set_volume_value() function to update the UI. Both the sound_type_e enum and the new volume value are passed to the callback function as parameters to introduce the changes to the UI correctly.

if (sound_manager_set_volume_changed_cb(__volume_changed_cb, NULL) != SOUND_MANAGER_ERROR_NONE) 
{
    // Error handling
}

static void
__volume_changed_cb(sound_type_e type, unsigned int volume, void *user_data)
{
   view_set_volume_value((volume_controller_type)type, (double)volume);
}

When the callback function is no longer needed, unset it using the sound_manager_unset_volume_changed_cb() function.

if (sound_manager_unset_volume_changed_cb() != SOUND_MANAGER_ERROR_NONE)
   // Error handling

View Manager Module

A controller is created for each of the volume types. The controller consists of a volume type label, volume value label, and a slider for manually changing the value. Each controller has a custom EDJ layout.

static Evas_Object *
__create_controller(volume_controller_type type)
{
   // Variable declaration

   __app_get_resource(EDJ_ITEM_FILE, edj_item_path, (int)PATH_MAX);

   item_layout = elm_layout_add(s_viewdata.layout);
   // Error handling

   elm_layout_file_set(item_layout, edj_item_path, GRP_MAIN);
   elm_object_part_text_set(item_layout, PART_ITEM_TYPE_TEXT, volume_item_title[type]);

   slider = __create_slider(item_layout);
   // Error handling

   evas_object_smart_callback_add(slider, "changed", _slider_changed_cb, (void*)type);
   elm_object_part_content_set(item_layout, PART_ITEM_SLIDER, slider);

   elm_object_part_content_set(s_viewdata.layout, volume_item_part_name[type], item_layout);

   return item_layout;
}

A callback function is set for the slider change event. When the user manually changes the slider value, the _slider_changed_cb() function is invoked, which in turn invokes the volume_manager_set_volume() function to change the proper sound level on the device.

static void
_slider_changed_cb(void *data, Evas_Object *obj, void *event_info)
{
   // Variable declaration

   type = (volume_controller_type)data;
   value = elm_slider_value_get(obj);

   __set_controller_value_label(s_viewdata.volume_controller[type], value);
   volume_manager_set_volume(type, value);
}