|
1 |
|
2 <!DOCTYPE html |
|
3 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
4 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="copyright" content="(C) Copyright 2009"/><meta name="DC.rights.owner" content="(C) Copyright 2009"/><meta name="DC.Type" content="mobileconcept"/><meta name="DC.Title" content="Media Management Service API example"/><meta name="DC.Relation" scheme="URI" content="GUID-2FC0C5D3-CE2A-45BE-B7E9-04A62A099E86"/><meta name="DC.Relation" scheme="URI" content="GUID-7C69DDA4-16F1-4A8F-BDB2-4CB0015B4E81"/><meta name="DC.Relation" scheme="URI" content="GUID-65AAF569-D347-462B-B59A-9D7CA184AB9C"/><meta name="DC.Relation" scheme="URI" content="GUID-E5684FF0-28C9-4721-9421-6B9432087086"/><meta name="DC.Format" content="XHTML"/><meta name="DC.Identifier" content="GUID-B7297E44-2CCE-4098-807B-551684CF608E"/><title>Media Management Service API example </title><script type="text/javascript"> |
|
5 function initPage() {} |
|
6 </script><link href="../PRODUCT_PLUGIN/book.css" rel="stylesheet" type="text/css"/><link href="css/s60/style.css" rel="stylesheet" type="text/css" media="all"/></head><body onload="initPage();"><div class="body"><div class="contentLeft prTxt"><h1 class="pageHeading" id="GUID-B7297E44-2CCE-4098-807B-551684CF608E">Media Management Service API example</h1><div> |
|
7 <p/> |
|
8 <p>This section presents the full source code of a working sample widget |
|
9 for the <a href="GUID-E3D26890-581A-427F-8AFF-16081528A8B8.html#GUID-E3D26890-581A-427F-8AFF-16081528A8B8">Media Management |
|
10 Service</a>. The widget is programmed to retrieve audio file information. |
|
11 You can download the <code>wgz</code> package for this widget from |
|
12 section <a href="GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2.html#GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2">Example widgets</a>.</p> |
|
13 <p>For general information about creating widgets, see section <a href="GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A.html#GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A">Widget component files</a>.</p> |
|
14 <p>For widget development and debugging purposes, this example writes its |
|
15 output to <code>c:\data\jslog_widget.log</code> using <code>console.info</code>. |
|
16 For instructions on how to enable logging in the Web browser for S60, see |
|
17 section <a href="GUID-B584CA90-543B-4AED-B134-A3A616259DB9.html#GUID-B584CA90-543B-4AED-B134-A3A616259DB9">JavaScript console</a>.</p> |
|
18 <div><h3>Info.plist</h3> |
|
19 <pre class="codeblock" id="GUID-4107599A-19E9-43B1-B549-2CF7ABD9103C"><?xml version="1.0" encoding="UTF-8"?> |
|
20 <!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd"> |
|
21 <plist version="1.0"> |
|
22 <dict> |
|
23 <key>DisplayName</key> |
|
24 <string>MediaManagementSample</string> |
|
25 <key>Identifier</key> |
|
26 <string>com.nokia.widget.sapi.MediaManagementSample</string> |
|
27 <key>Version</key> |
|
28 <string>1.0</string> |
|
29 <key>MainHTML</key> |
|
30 <string>mediamanagement-sample.html</string> |
|
31 </dict> |
|
32 </plist></pre> |
|
33 </div> |
|
34 <div><h3>mediamanagement-sample.html</h3> |
|
35 <pre class="codeblock" id="GUID-5E5742F2-1DD9-47AA-8B8F-55E38334B9EE"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|
36 <html> |
|
37 <head> |
|
38 <script type="text/javascript" src="js/common.js"></script> |
|
39 <script type="text/javascript" src="js/mediamanagement-sample.js"></script> |
|
40 </head> |
|
41 <body id="docBody" bgcolor="#ddeeff" onload="setup()" style=width:100%;height:100%;> |
|
42 <form name="frm"> |
|
43 <h3>MediaManagement API Sample Widget</h3> |
|
44 <input type="button" onclick="getListAsync('img1')" value="GetListAsync"><img id="img1" src="pic/blank.png" width="25" height="25" align="center"> |
|
45 <br> |
|
46 <input type="button" onclick="cancelGetListAsync('img2')" value="CancelGetListAsync"><img id="img2" src="pic/blank.png" width="25" height="25" align="center"> |
|
47 <hr> |
|
48 <div class="mediaman" id="mediaman" bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto></div> |
|
49 </form> |
|
50 </body> |
|
51 </html></pre> |
|
52 </div> |
|
53 <div><h3>common.js</h3> |
|
54 <pre class="codeblock" id="GUID-535B332D-25E9-4CE5-827E-AD41E837F7B5">// common.js |
|
55 // |
|
56 // This file contains some utility functions. |
|
57 |
|
58 // Check the error code and show the information to users |
|
59 function checkError(message, resultList, divId, imgId) |
|
60 { |
|
61 var errCode = resultList.ErrorCode; |
|
62 var msg = ""; |
|
63 |
|
64 if (errCode) { |
|
65 msg = message + "<BR>" + "Failed Error: " + errCode + "<BR>"; |
|
66 if(resultList.ErrorMessage != undefined) |
|
67 msg += "Error Message: " + resultList.ErrorMessage; |
|
68 showIMG(imgId,"no"); |
|
69 } else { |
|
70 showIMG(imgId,"yes"); |
|
71 } |
|
72 |
|
73 //print error message |
|
74 if(divId != null && divId != undefined) |
|
75 document.getElementById(divId).innerHTML = msg; |
|
76 console.info(msg); |
|
77 |
|
78 return errCode; |
|
79 } |
|
80 |
|
81 // Build the message by reading a iteratorable list in a recursive manner |
|
82 function showIterableList(iterator) |
|
83 { |
|
84 var msg = ""; |
|
85 try |
|
86 { |
|
87 iterator.reset(); |
|
88 var item; |
|
89 while (( item = iterator.getNext()) != undefined ){ |
|
90 msg += showObject(item); |
|
91 } |
|
92 } |
|
93 catch(e) |
|
94 { |
|
95 alert('<showIterableList> ' + e); |
|
96 } |
|
97 return msg; |
|
98 } |
|
99 |
|
100 // Build the message by reading a JS object in a recursive manner |
|
101 function showObject(obj) |
|
102 { |
|
103 var txt = ""; |
|
104 try { |
|
105 if ( typeof obj != 'object' ) |
|
106 return "" + obj + '<BR/>'; |
|
107 else { |
|
108 for(var key in obj) { |
|
109 txt += key + ":"; |
|
110 txt += showObject( obj[key] ); |
|
111 txt += '<BR/>'; |
|
112 } |
|
113 txt += '<BR/>'; |
|
114 } |
|
115 } |
|
116 catch (e) |
|
117 { |
|
118 alert("showObject: " + e); |
|
119 } |
|
120 return txt; |
|
121 } |
|
122 |
|
123 // Show the image to indicate the test result |
|
124 function showIMG(imgId, isOK) |
|
125 { |
|
126 if(imgId == null || imgId == undefined) |
|
127 return; |
|
128 |
|
129 if(isOK == "yes") |
|
130 document.getElementById(imgId).src = "pic/yes.png"; |
|
131 else if(isOK == "no") |
|
132 document.getElementById(imgId).src = "pic/no.png"; |
|
133 else |
|
134 document.getElementById(imgId).src = "pic/blank.png"; |
|
135 } |
|
136 |
|
137 // Show elements in object by using 'alert' |
|
138 function testObject(obj) |
|
139 { |
|
140 var msg = ""; |
|
141 for(var key in obj) { |
|
142 msg = msg + ":" + key + "=" + obj[key]; |
|
143 } |
|
144 alert(msg); |
|
145 } |
|
146 |
|
147 // Test whether the input is numeric |
|
148 function IsNumeric(sText) |
|
149 { |
|
150 var ValidChars = "0123456789."; |
|
151 var IsNumber=true; |
|
152 var Char; |
|
153 |
|
154 for (i = 0; i < sText.length && IsNumber == true; i++) |
|
155 { |
|
156 Char = sText.charAt(i); |
|
157 if (ValidChars.indexOf(Char) == -1) |
|
158 { |
|
159 IsNumber = false; |
|
160 } |
|
161 } |
|
162 return IsNumber; |
|
163 }</pre> |
|
164 </div> |
|
165 <div><h3>mediamanagement-sample.js</h3> |
|
166 <pre class="codeblock" id="GUID-4AB2559C-EA8A-42B9-8731-FF808464A037">// mediamanagement-sample.js |
|
167 // |
|
168 // In this sample the media objects and meta-data from the S60 Media |
|
169 // Gallery will be listed; Also, async operation will be canceled. |
|
170 // |
|
171 //SAPI Error Codes |
|
172 // 0 - Success |
|
173 // 1000 - InvalidServiceArgument |
|
174 // 1001 - UnknownArgumentName |
|
175 // 1002 - BadArgumentType |
|
176 // 1003 - MissingArgument |
|
177 // 1004 - ServiceNotSupported |
|
178 // 1005 - ServiceInUse |
|
179 // 1006 - ServiceNotReady |
|
180 // 1007 - NoMemory |
|
181 // 1008 - HardwareNotAvailable |
|
182 // 1009 - ServerBusy |
|
183 // 1010 - EntryExists |
|
184 // 1011 - AccessDenied |
|
185 // 1012 - NotFound |
|
186 // 1013 - UnknownFormat |
|
187 // 1014 - GeneralError |
|
188 // 1015 - CancelSuccess |
|
189 // 1016 - ServiceTimedOut |
|
190 // 1017 - PathNotFound |
|
191 |
|
192 |
|
193 // Declare the service object |
|
194 var so; |
|
195 |
|
196 // imgid for callback1 function |
|
197 var imgid_callback1; |
|
198 |
|
199 // id of the div used to display information |
|
200 const DIV_ID = 'mediaman'; |
|
201 |
|
202 |
|
203 // Called from onload() |
|
204 function setup() |
|
205 { |
|
206 try { |
|
207 so = device.getServiceObject("Service.MediaManagement", "IDataSource"); |
|
208 console.info("setup: so: %s", so); |
|
209 } |
|
210 catch(e) { |
|
211 alert('<setup> ' +e); |
|
212 } |
|
213 } |
|
214 |
|
215 // Get List Async |
|
216 // This method retrieves a list of media objects and meta-data from the S60 Media Gallery. |
|
217 // In order to test this method make sure that there are media objects in the media Gallery. |
|
218 function getListAsync(imgId) |
|
219 { |
|
220 // Setup input params using dot syntax |
|
221 var criteria = new Object(); |
|
222 criteria.Type = 'FileInfo'; |
|
223 criteria.Filter = new Object(); |
|
224 criteria.Filter.FileType = 'Sound'; |
|
225 criteria.Sort = new Object(); |
|
226 criteria.Sort.Key = 'FileSize'; |
|
227 |
|
228 // set the image id for callback function |
|
229 imgid_callback1 = imgId; |
|
230 |
|
231 try { |
|
232 // Media Management supports asynchronous call |
|
233 var result = so.IDataSource.GetList(criteria, callback); |
|
234 if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgId)) |
|
235 showIMG(imgId,""); |
|
236 } |
|
237 catch (e) { |
|
238 showIMG(imgId,"no"); |
|
239 alert ("getListAsync: " + e); |
|
240 } |
|
241 } |
|
242 |
|
243 // This method cancels outstanding asynchronous request. |
|
244 function cancelGetListAsync(imgId) |
|
245 { |
|
246 // Setup input params using dot syntax |
|
247 var criteria = new Object(); |
|
248 criteria.Type = 'FileInfo'; |
|
249 criteria.Filter = new Object(); |
|
250 criteria.Filter.FileType = 'Sound'; |
|
251 criteria.Sort = new Object(); |
|
252 criteria.Sort.Key = 'FileSize'; |
|
253 |
|
254 // set the image id for callback function |
|
255 imgid_callback1 = imgId; |
|
256 |
|
257 try { |
|
258 // Media Management supports asynchronous call |
|
259 var result = so.IDataSource.GetList(criteria, callback); |
|
260 if(!checkError("IDataSource::cancelGetListAsync",result,DIV_ID,imgId)){ |
|
261 showIMG(imgId,""); |
|
262 var criteria2 = new Object(); |
|
263 criteria2.TransactionID = result.TransactionID; |
|
264 var cresult = so.IDataSource.Cancel(criteria2); |
|
265 checkError("IDataSource::cancelGetListAsync",cresult,DIV_ID,imgId); |
|
266 } |
|
267 } |
|
268 catch (e) { |
|
269 showIMG(imgId,"no"); |
|
270 alert ("cancelGetListAsync: " + e); |
|
271 } |
|
272 } |
|
273 |
|
274 // This is the asynchronous callback handler |
|
275 function callback(transId, eventCode, result) |
|
276 { |
|
277 console.info("callback: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode); |
|
278 if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgid_callback1)) { |
|
279 document.getElementById(DIV_ID).innerHTML = showIterableList(result.ReturnValue); |
|
280 } |
|
281 }</pre> |
|
282 </div> |
|
283 </div></div></div><div class="footer"><hr/><div class="copy">© Nokia 2009.</div></div></body></html> |