1 /* 2 * This is sound cordova_plugin (TV specific API). 3 * Apache License (2004). See http://www.apache.org/licenses/LICENSE-2.0 4 * 5 * Copyright (c) 2014, LG Electronics, Inc. 6 */ 7 8 /** 9 * This represents the sound API itself, and provides a global namespace for operating sound service. 10 * @class 11 */ 12 cordova.define('cordova/plugin/sound', function (require, exports, module) { // jshint ignore:line 13 14 function log(msg) { 15 //console.log//will be removed // jshint ignore:line 16 } 17 18 var service; 19 if (window.PalmSystem) { // jshint ignore:line 20 log("Window.PalmSystem Available"); 21 service = require('cordova/plugin/webos/service'); 22 } else { 23 service = { 24 Request : function(uri, params) { 25 log(uri + " invoked. But I am a dummy because PalmSystem is not available"); 26 27 if (typeof params.onFailure === 'function') { 28 params.onFailure({ returnValue:false, 29 errorText:"PalmSystem Not Available. Cordova is not installed?"}); 30 } 31 }}; 32 } 33 34 /** 35 * sound interface 36 */ 37 var Sound = function () { 38 }; 39 40 function checkErrorCodeNText(result, errorCode, errorText) { 41 42 if (result.errorCode === undefined || result.errorCode === null ) { 43 result.errorCode = errorCode; 44 } 45 if (result.errorText ===undefined || result.errorText === null) { 46 result.errorText = errorText; 47 } 48 } 49 50 /** 51 * Gets sound information 52 * @class Sound 53 * @param {Function} successCallback success callback function. 54 * @param {Function} errorCallback failure callback function. 55 * @return {Object} 56 * <div align=left> 57 * <table class="hcap_spec" width=400> 58 * <thead><tr><th>Property</th><th>Type</th><th>Description</th></tr></thead> 59 * <tbody> 60 * <tr><th>level</th><th>Number</th><th>volume level (0~100)</th></tr> 61 * <tr class="odd"><th>muted</th><th>Boolean</th><th>true: mute on / false: mute off </th></tr> 62 * <tr><th>externalSpeaker</th><th>Boolean</th><th>true : enabled / false : disabled </th></tr> 63 * </tbody> 64 * </table> 65 * </div> 66 * 67 * @example 68 * // Javascript code 69 * function getSoundStatus () { 70 * function successCb(cbObject) { 71 * console.log("cbObject : " + JSON.stringify(cbObject)); 72 * 73 * console.log("level : " + cbObject.level); 74 * console.log("muted : " + cbObject.muted); 75 * console.log("externalSpeaker : " + cbObject.externalSpeaker); 76 * 77 * // Do something 78 * ... 79 * } 80 * 81 * function failureCb(cbObject) { 82 * var errorCode = cbObject.errorCode; 83 * var errorText = cbObject.errorText; 84 * console.log ("Error Code [" + errorCode + "]: " + errorText); 85 * } 86 * 87 * var sound = new Sound(); 88 * sound.getSoundStatus(successCb, failureCb); 89 * } 90 * @since 1.0 91 * @see 92 * <a href="Sound%23setVolumeLevel.html">Sound.setVolumeLevel()</a>, 93 * <a href="Sound%23setExternalSpeaker.html">Sound.setExternalSpeaker()</a><br> 94 */ 95 Sound.prototype.getSoundStatus = function (successCallback, errorCallback) { 96 97 log("getSoundStatus: "); 98 99 service.Request("luna://com.webos.audio/", { 100 method : "getVolume", 101 onSuccess : function(result) { 102 log("getSoundStatus: On Success"); 103 104 if (result.returnValue === true) { 105 var cbObj = {}; 106 cbObj.level = result.volume; 107 cbObj.muted = result.muted; 108 109 service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", { 110 method : "get", 111 parameters : { 112 category : "commercial", 113 keys : ["enableSpeaker"] 114 }, 115 onSuccess : function(result) { 116 log("getSoundStatus: On Success 2"); 117 118 if (result.returnValue === true) { 119 cbObj.externalSpeaker = (result.settings.enableSpeaker === "on" ? true : false ); 120 121 if (successCallback && typeof successCallback === 'function') { 122 successCallback(cbObj); 123 } 124 } 125 }, 126 onFailure : function(result) { 127 log("getSoundStatus: On Failure 2"); 128 delete result.returnValue; 129 if (errorCallback && typeof errorCallback === 'function') { 130 checkErrorCodeNText(result, "SGSS", "Sound.getSoundStatus returns failure."); 131 errorCallback(result); 132 } 133 } 134 }); 135 136 } 137 }, 138 onFailure : function(result) { 139 log("getSoundStatus: On Failure"); 140 delete result.returnValue; 141 if (errorCallback && typeof errorCallback === 'function') { 142 checkErrorCodeNText(result, "SGSS", "Sound.getSoundStatus returns failure."); 143 errorCallback(result); 144 } 145 } 146 }); 147 148 log("Sound.getSoundStatus Done"); 149 150 }; 151 152 /** 153 * Sets volume level 154 * @class Sound 155 * @param {Function} successCallback success callback function. 156 * @param {Function} errorCallback failure callback function. 157 * @param {Object} options 158 * <div align=left> 159 * <table class="hcap_spec" width=400> 160 * <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead> 161 * <tbody> 162 * <tr><th>level</th><th>Number</th><th>volume level (0~100)</th><th>required</th></tr> 163 * </tbody> 164 * </table> 165 * </div> 166 * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br> 167 * If an error occurs, failure callback function is called with failure callback object as a parameter.</p> 168 * @example 169 * // Javascript code 170 * function setVolumeLevel () { 171 * var options = { 172 * level : 15 173 * }; 174 * 175 * function successCb() { 176 * // Do something 177 * } 178 * 179 * function failureCb(cbObject) { 180 * var errorCode = cbObject.errorCode; 181 * var errorText = cbObject.errorText; 182 * console.log ("Error Code [" + errorCode + "]: " + errorText); 183 * } 184 * 185 * var sound = new Sound(); 186 * sound.setVolumeLevel(successCb, failureCb, options); 187 * } 188 * @since 1.0 189 * @see 190 * <a href="Sound%23getSoundStatus.html">Sound.getSoundStatus()</a><br> 191 */ 192 Sound.prototype.setVolumeLevel = function (successCallback, errorCallback, options) { 193 194 log("setVolumeLevel: " + JSON.stringify(options)); 195 196 if (typeof options.level !== 'number' || isNaN(options.level) || 197 options.level < 0 || options.level > 100) { 198 199 if (errorCallback && typeof errorCallback === 'function') { 200 var result = {}; 201 checkErrorCodeNText(result, "SSVL", "Sound.setVolumeLevel returns failure. out of range or invalid parameter type."); 202 errorCallback(result); 203 } 204 205 return; 206 } 207 208 service.Request("luna://com.webos.audio/", { 209 method : "setVolume", 210 parameters : { 211 volume : options.level 212 }, 213 onSuccess : function(result) { 214 log("setVolumeLevel: On Success"); 215 216 if (result.returnValue === true) { 217 if (successCallback && typeof successCallback === 'function') { 218 successCallback(); 219 } 220 } 221 }, 222 onFailure : function(result) { 223 log("setVolumeLevel: On Failure"); 224 delete result.returnValue; 225 if (errorCallback && typeof errorCallback === 'function') { 226 checkErrorCodeNText(result, "SSVL", "Sound.setVolumeLevel returns failure."); 227 errorCallback(result); 228 } 229 } 230 }); 231 232 log("Sound.setVolumeLevel Done"); 233 234 }; 235 236 /** 237 * Enable or disable external speaker. 238 * If this method is successfully executed, change the external speaker status. 239 * @class Sound 240 * @param {Function} successCallback success callback function. 241 * @param {Function} errorCallback failure callback function. 242 * @param {Object} options 243 * <div align=left> 244 * <table class="hcap_spec" width=400> 245 * <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead> 246 * <tbody> 247 * <th>externalSpeaker</th><th>Boolean</th><th>true : enabled / false : disabled </th><th>required</th></tr> 248 * </tbody> 249 * </table> 250 * </div> 251 * @return <p>If the method is successfully executed, call the success callback function without a parameter.</br> 252 * If an error occurs, failure callback function is called with failure callback object as a parameter.</p> 253 * @example 254 * // Javascript code 255 * function setExternalSpeaker () { 256 * var options = { 257 * externalSpeaker : true 258 * }; 259 * 260 * function successCb() { 261 * // Do something 262 * } 263 * 264 * function failureCb(cbObject) { 265 * var errorCode = cbObject.errorCode; 266 * var errorText = cbObject.errorText; 267 * console.log ("Error Code [" + errorCode + "]: " + errorText); 268 * } 269 * 270 * var sound = new Sound(); 271 * sound.setExternalSpeaker(successCb, failureCb, options); 272 * } 273 * @since 1.0 274 * @see 275 * <a href="Sound%23getSoundStatus.html">Sound.getSoundStatus()</a><br> 276 */ 277 Sound.prototype.setExternalSpeaker = function (successCallback, errorCallback, options) { 278 279 log("setExternalSpeaker: " + JSON.stringify(options)); 280 281 if (typeof options.externalSpeaker !== 'boolean') { 282 283 if (errorCallback && typeof errorCallback === 'function') { 284 var result = {}; 285 checkErrorCodeNText(result, "SSVL", "Sound.setExternalSpeaker returns failure. out of range or invalid parameter type."); 286 errorCallback(result); 287 } 288 289 return; 290 } 291 292 var enable = null; 293 switch (options.externalSpeaker) { 294 case true : 295 enable = "on"; 296 break; 297 case false : 298 enable = "off"; 299 break; 300 } 301 302 service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", { 303 method : "set", 304 parameters : { 305 category : "commercial", 306 settings : {"enableSpeaker":enable} 307 }, 308 onSuccess : function() { 309 log("setExternalSpeaker: On Success"); 310 if (typeof successCallback === 'function') { 311 successCallback(); 312 } 313 }, 314 onFailure : function(result) { 315 log("setExternalSpeaker: On Failure"); 316 delete result.returnValue; 317 if (typeof errorCallback === 'function') { 318 checkErrorCodeNText(result, "SSES", "Sound.setExternalSpeaker returns failure."); 319 errorCallback(result); 320 } 321 } 322 }); 323 324 log("Sound.setExternalSpeaker Done"); 325 326 }; 327 328 /** 329 * mute on/off 330 * @class Sound 331 * @param {Function} successCallback success callback function. 332 * @param {Function} errorCallback failure callback function. 333 * @param {Object} options 334 * <div align=left> 335 * <table class="hcap_spec" width=400> 336 * <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead> 337 * <tbody> 338 * <tr><th>muted</th><th>Boolean</th><th>true: mute on / false: mute off </th><th>required</th></tr> 339 * </tbody> 340 * </table> 341 * </div> 342 * @return <p>If the method is successfully executed, call the success callback function without a parameter.</br> 343 * If an error occurs, failure callback function is called with failure callback object as a parameter.</p> 344 * @example 345 * // Javascript code 346 * function setMuted () { 347 * var options = { 348 * muted : true 349 * }; 350 * 351 * function successCb() { 352 * // Do something 353 * } 354 * 355 * function failureCb(cbObject) { 356 * var errorCode = cbObject.errorCode; 357 * var errorText = cbObject.errorText; 358 * console.log ("Error Code [" + errorCode + "]: " + errorText); 359 * } 360 * 361 * var sound = new Sound(); 362 * sound.setMuted(successCb, failureCb, options); 363 * } 364 * @since 1.0 365 * @see 366 * <a href="Sound%23getSoundStatus.html">Sound.getSoundStatus()</a><br> 367 */ 368 Sound.prototype.setMuted = function (successCallback, errorCallback, options) { 369 370 log("setMuted: " + JSON.stringify(options)); 371 372 if (typeof options.muted !== 'boolean') { 373 374 if (errorCallback && typeof errorCallback === 'function') { 375 var result = {}; 376 checkErrorCodeNText(result, "SSVL", "Sound.setMuted returns failure. out of range or invalid parameter type."); 377 errorCallback(result); 378 } 379 380 return; 381 } 382 383 service.Request("luna://com.webos.audio/", { 384 method : "setMuted", 385 parameters : { 386 muted : options.muted 387 }, 388 onSuccess : function(result) { 389 log("setMuted: On Success"); 390 391 if (result.returnValue === true) { 392 if (successCallback && typeof successCallback === 'function') { 393 successCallback(); 394 } 395 } 396 }, 397 onFailure : function(result) { 398 log("setMuted: On Failure"); 399 delete result.returnValue; 400 if (errorCallback && typeof errorCallback === 'function') { 401 checkErrorCodeNText(result, "SSM", "Sound.setMuted returns failure."); 402 errorCallback(result); 403 } 404 } 405 }); 406 407 log("Sound.setMuted Done"); 408 409 }; 410 411 module.exports = Sound; 412 }); 413 414 Sound = cordova.require('cordova/plugin/sound'); // jshint ignore:line 415 416