Sound: Controlling the Sound Volume Level and the Sound Devices
This tutorial demonstrates how you can manage the device volume levels and sound devices.
The Sound API is mandatory for both Tizen mobile and wearable profiles, which means that it is supported in all mobile and wearable devices. All mandatory APIs are supported on the Tizen Emulators.
Warm-up
Become familiar with the Sound API basics by learning about:
-
Managing Sound Mode and Volume
Get the current sound mode and set the volume level.
-
Monitoring Sound Mode and Volume Changes
Track changes in the sound mode and volume level.
-
Managing Sound Devices
Get a list of the current sound devices which are in a specified state.
-
Monitoring the Sound Device State
Track changes in the state of sound devices.
Managing Sound Mode and Volume
Managing sound modes and volume levels is a basic multimedia management skill:
- Get the current volume level using the getVolume() method:
var vol = tizen.sound.getVolume("RINGTONE");
- Set a new volume level using the setVolume() method.
The following example increases the ringtone volume by 10% of the maximum volume level:
tizen.sound.setVolume("RINGTONE", vol + 0.1);
- Get the current sound mode using the getSoundMode() method:
var mode = tizen.sound.getSoundMode(); console.log("Sound Mode is " + mode);
Monitoring Sound Mode and Volume Changes
Managing sound and volume changes is a basic multimedia management skill:
- Register the volume change listener to track changes in the volume level:
function onsuccessCB(type, volume) { console.log("SoundType is " + type); console.log("Volume is " + volume); tizen.sound.unsetVolumeChangeListener(); } tizen.sound.setVolumeChangeListener(onsuccessCB);
- Register the sound mode change listener to track changes in the sound mode:
function onsuccessCB(mode) { console.log("Mode is " + mode); tizen.sound.unsetSoundModeChangeListener(); } tizen.sound.setSoundModeChangeListener(onsuccessCB);
Managing Sound Devices
Learning how to list connected and activated sound devices allows you to manage sound devices more effectively:
- Get a list of the current sound devices in a connected state using the getConnectedDeviceList() method:
var infoArr = tizen.sound.getConnectedDeviceList(); for (var i = 0; i < infoArr.length; i++) { console.log(infoArr[i].device); }
- Get a list of the current sound devices in an activated state using the getActivatedDeviceList() method:
var infoArr = tizen.sound.getActivatedDeviceList(); for (var i = 0; i < infoArr.length; i++) { console.log(infoArr[i].device); }
Monitoring the Sound Device State
Learning how to monitor changes in the sound device state makes it easier for you to manage the sound devices:
- Add a sound device state change listener to track changes in the sound device state:
function onchangedCB(info) { if (info.isConnected) { console.log(info.device + " is connected"); } else { console.log(info.device + " is not connected"); } if (info.isActivated) { console.log(info.device + " is activated"); } else { console.log(info.device + " is not activated"); } } var listenerId = tizen.sound.addDeviceStateChangeListener(onChangedCB);
- When no longer needed, use the listener ID to remove the sound device state change listener:
tizen.sound.removeDeviceStateChangeListener(listenerId);