Media Management Service API example

This section presents the full source code of a working sample widget for the Media Management Service. The widget is programmed to retrieve audio file information. You can download the wgz package for this widget from section Example widgets.

For general information about creating widgets, see section Widget component files.

For widget development and debugging purposes, this example writes its output to c:\data\jslog_widget.log using console.info. For instructions on how to enable logging in the Web browser for S60, see section JavaScript console.

Info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd">
<plist version="1.0">
<dict>
  <key>DisplayName</key>
  <string>MediaManagementSample</string>
  <key>Identifier</key>
  <string>com.nokia.widget.sapi.MediaManagementSample</string>
  <key>Version</key>
  <string>1.0</string>
  <key>MainHTML</key>
  <string>mediamanagement-sample.html</string>
</dict>
</plist>

mediamanagement-sample.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script type="text/javascript" src="js/common.js"></script>
    <script type="text/javascript" src="js/mediamanagement-sample.js"></script>
  </head>
  <body id="docBody" bgcolor="#ddeeff" onload="setup()" style=width:100%;height:100%;>
    <form name="frm">
      <h3>MediaManagement API Sample Widget</h3>
      <input type="button" onclick="getListAsync('img1')" value="GetListAsync"><img id="img1" src="pic/blank.png" width="25" height="25" align="center">
      <br>
      <input type="button" onclick="cancelGetListAsync('img2')" value="CancelGetListAsync"><img id="img2" src="pic/blank.png" width="25" height="25" align="center">
      <hr>
      <div class="mediaman" id="mediaman" bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto></div>
    </form>
  </body>
</html>

common.js

// common.js
//
// This file contains some utility functions.

// Check the error code and show the information to users
function checkError(message, resultList, divId, imgId)
{
  var errCode = resultList.ErrorCode;
  var msg = "";

  if (errCode) {
    msg = message + "<BR>" + "Failed Error: " + errCode + "<BR>";
    if(resultList.ErrorMessage != undefined)
      msg += "Error Message: " + resultList.ErrorMessage;
    showIMG(imgId,"no");
  } else {
    showIMG(imgId,"yes");
  }

  //print error message
  if(divId != null && divId != undefined)
    document.getElementById(divId).innerHTML = msg;
  console.info(msg);

  return errCode;
}

// Build the message by reading a iteratorable list in a recursive manner
function showIterableList(iterator)
{
  var msg = "";
  try
  {
    iterator.reset();
    var item;
    while (( item = iterator.getNext()) != undefined ){
      msg += showObject(item);
    }
  }
  catch(e)
  {
    alert('<showIterableList> ' + e);
  }
  return msg;
}

// Build the message by reading a JS object in a recursive manner
function showObject(obj)
{
  var txt = "";
  try {
    if ( typeof obj != 'object' )
      return "" + obj + '<BR/>';
    else {
      for(var key in obj) {
        txt +=  key + ":";
        txt += showObject( obj[key] );
        txt += '<BR/>';
      }
      txt += '<BR/>';
    }
  }
  catch (e)
  {
    alert("showObject: " + e);
  }
  return txt;
}

// Show the image to indicate the test result
function showIMG(imgId, isOK)
{
  if(imgId == null || imgId == undefined)
    return;

  if(isOK == "yes")
    document.getElementById(imgId).src = "pic/yes.png";
  else if(isOK == "no")
    document.getElementById(imgId).src = "pic/no.png";
  else
    document.getElementById(imgId).src = "pic/blank.png";
}

// Show elements in object by using 'alert'
function testObject(obj)
{
  var msg = "";
  for(var key in obj) {
    msg = msg + ":" + key + "=" + obj[key];
  }
  alert(msg);
}

// Test whether the input is numeric
function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

  for (i = 0; i < sText.length && IsNumber == true; i++)
  {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
      IsNumber = false;
    }
  }
  return IsNumber; 
}

mediamanagement-sample.js

// mediamanagement-sample.js
//
// In this sample the media objects and meta-data from the S60 Media
// Gallery will be listed; Also, async operation will be canceled.
//
//SAPI Error Codes
// 0    - Success
// 1000 - InvalidServiceArgument
// 1001 - UnknownArgumentName
// 1002 - BadArgumentType
// 1003 - MissingArgument
// 1004 - ServiceNotSupported
// 1005 - ServiceInUse
// 1006 - ServiceNotReady
// 1007 - NoMemory
// 1008 - HardwareNotAvailable
// 1009 - ServerBusy
// 1010 - EntryExists
// 1011 - AccessDenied
// 1012 - NotFound
// 1013 - UnknownFormat
// 1014 - GeneralError
// 1015 - CancelSuccess
// 1016 - ServiceTimedOut
// 1017 - PathNotFound


// Declare the service object
var so;

// imgid for callback1 function
var imgid_callback1;

// id of the div used to display information
const DIV_ID = 'mediaman';


// Called from onload()
function setup()
{
  try {
    so = device.getServiceObject("Service.MediaManagement", "IDataSource");
    console.info("setup: so: %s", so);
  }
  catch(e) {
    alert('<setup> ' +e);
  }
}

// Get List Async
// This method retrieves a list of media objects and meta-data from the S60 Media Gallery.
// In order to test this method make sure that there are media objects in the media Gallery. 
function getListAsync(imgId)
{
  // Setup input params using dot syntax
  var criteria = new Object();
  criteria.Type = 'FileInfo';
  criteria.Filter = new Object();
  criteria.Filter.FileType = 'Sound';
  criteria.Sort = new Object();
  criteria.Sort.Key = 'FileSize';

  // set the image id for callback function
  imgid_callback1 = imgId;

  try {
    // Media Management supports asynchronous call
    var result = so.IDataSource.GetList(criteria, callback);
    if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgId))
      showIMG(imgId,"");
  }
  catch (e) {
    showIMG(imgId,"no");
    alert ("getListAsync: " + e);
  }
}

// This method cancels outstanding asynchronous request.
function cancelGetListAsync(imgId)
{
  // Setup input params using dot syntax
  var criteria = new Object();
  criteria.Type = 'FileInfo';
  criteria.Filter = new Object();
  criteria.Filter.FileType = 'Sound';
  criteria.Sort = new Object();
  criteria.Sort.Key = 'FileSize';

  // set the image id for callback function
  imgid_callback1 = imgId;

  try {
    // Media Management supports asynchronous call
    var result = so.IDataSource.GetList(criteria, callback);
    if(!checkError("IDataSource::cancelGetListAsync",result,DIV_ID,imgId)){
      showIMG(imgId,"");
      var criteria2 = new Object(); 
      criteria2.TransactionID = result.TransactionID;
      var cresult = so.IDataSource.Cancel(criteria2);
      checkError("IDataSource::cancelGetListAsync",cresult,DIV_ID,imgId);
    }
  }
  catch (e) {
    showIMG(imgId,"no");
    alert ("cancelGetListAsync: " + e);
  }
}

// This is the asynchronous callback handler
function callback(transId, eventCode, result)
{
  console.info("callback: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
  if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgid_callback1)) {
    document.getElementById(DIV_ID).innerHTML = showIterableList(result.ReturnValue);
  }
}