This tutorial describes AllShare API usage and includes information about sample widget application.
Note
This tutorial is compatible with Samsung SDK 3.5 only. Please visit AllSHare Tutorial in order to see the tutorial written for SDK 4.5 version and higher.
Device is parent class for each media provider. Application can get next information about Device: iconURL, unique Id, device name.
Sample
var htmlStr = '',
mediaProvider = providerArray[0],
mediaProviderName = mediaProvider.getName(),
mediaProviderIcon,
mediaProviderID;
htmlStr += mediaProviderName + '<br />';
mediaProviderIcon = mediaProvider.getIcon();
if (mediaProviderIcon) {
htmlStr += '<img src="' + mediaProviderIcon + '" /><br />';
}
mediaProviderID = mediaProvider.getID();
htmlStr += mediaProviderID;
MediaProvider is child of Device. MediaProvider has next functions:
Sample
mediaProvider = providerArray[0];
if (mediaProvider.getMediaProviderType() == DLNA_MEDIA_SERVER) {
alert("It is DLNA media server");
} else {
alert("Local file system? Network file system? Ftp? What may be else?");
}
Application can ask for list of currently aviailable DMS devices using getMediaProviders().
Sample
var providerArray = serviceapis.mediasharing.mediaproviderfinder.getMediaProviders(),
i = 0;
if (providerArray) {
for (i = 0; i < providerArray.length; i++) {
alert("providersName:" + providerArray[i].getName());
}
}
Other way to access device, request it by known deviceID.
Sample
var deviceID, // this variable should have deviceID value of some device
mediaProvider = serviceapis.mediasharing.mediaproviderfinder.getMediaProvider(deviceID);
Each DLNA device notify network when it appear or when it will disappear. CP may use this events to handle list of available devices. Application can set callback handler to obtain such events.
Sample
var providerDiscoveryCallback = {
onmediaprovideradded : function (mediaProvider) {
alert("new device appear : " + mediaProvider.getName());
// adding new provider into providers list
},
onmediaproviderremoved : function (mediaProvider) {
alert("device disappear : " + mediaProvider.getName());
// removing provider from providers list
// use getID() to distinguish removed device in device list
}
}
serviceapis.mediasharing.mediaproviderfinder.setMediaProviderFinderCallback(providerDiscoveryCallback);
Item is parent type for any media items.
ItemType included definition for next values:
To distinguish folder item use code below:
Sample
var itemType = item.getType();
if (itemType.item == itemType.ITEM_FOLDER) {
// it is folder
}
FolderItem is child of Item.
AudioItem is child of Item
ImageItem is child of Item
VideoItem is child of Item
Next example shows how to access and print media item information.
Sample
function printItem(item) {
if (item) {
alert("title: " + item.getTitle());
alert("date: " + item.getDate());
alert("uri: " + item.getURI());
var itemType = item.getType();
if (itemType) {
alert("ITEM_FOLDER: " + itemType.ITEM_FOLDER);
alert("ITEM_AUDIO: " + itemType.ITEM_AUDIO);
alert("ITEM_IMAGE: " + itemType.ITEM_IMAGE);
alert("ITEM_VIDEO: " + itemType.ITEM_VIDEO);
alert("ITEM_UNKNOWN: " + itemType.ITEM_UNKNOWN);
switch(itemType.type) {
case itemType.ITEM_FOLDER:
alert("ITEM_FOLDER");
alert("isRootFolder: " + item.isRootFolder());
break;
case itemType.ITEM_AUDIO:
alert("ITEM_AUDIO");
alert("getAlbumArt: " + item.getAlbumArt());
alert("getAlbumTitle: " + item.getAlbumTitle());
alert("getArtist: " + item.getArtist());
alert("getDuration: " + item.getDuration());
break;
case itemType.ITEM_IMAGE:
alert("ITEM_IMAGE");
alert("getThumbnail: " + item.getThumbnail());
alert("getResolution: " + item.getResolution());
alert("getLongitude: " + item.getLongitude());
alert("getLatitude: " + item.getLatitude());
break;
case itemType.ITEM_VIDEO:
alert("ITEM_VIDEO");
alert("getThumbnail: " + item.getThumbnail());
alert("getResolution: " + item.getResolution());
alert("getDuration: " + item.getDuration());
break;
case itemType.ITEM_UNKNOWN:
alert("ITEM_UNKNOWN");
break;
default:
alert("unexpected value");
break;
}
} else {
alert("item.getType() == null");
}
} else {
alert("item == null");
}
}
The “search” is searching for items using keyword parameter. Searching is doing for whole contents of media provider. Usage is similar to browseItems(), it is asynchronous request. The function use callbacks to inform about success reading or error cases.
Sample
var searchHelper = {}; // helper object, what will implement search functionality
searchHelper.resultItems = null; // this variable will keep finded items
searchHelper.showFolderContents = function () { // this function executed when folder contents readed fully
for (i = 0; i < resultItems.length; i++) {
showMediaItemInfo(resultItems[i]);
}
}
searchHelper.searchItemsInner = function (mediaProvider, keyword) { // this function doing request for items
var self = this;
alert("searchHelper.searchItemsInner begin");
// this function is callback, it called when next portion of items readed
var searchItemsSuccessCallback = function (sc_itemList, sc_requestedStartIndex, sc_keyword, sc_fEndOfItems, sc_mediaProvider) {
alert("success callback");
if (sc_itemList) {
alert("itemsList.length : " + sc_itemList.length);
alert("requestedStartIndex : " + sc_requestedStartIndex);
alert("keyword : " + sc_keyword);
alert("fEndOfItems : " + sc_fEndOfItems);
// copy result into 'resultItems' and request for another portion if needed
self.resultItems = self.resultItems.concat(sc_itemList);
alert("add items into resultItems, new length :" + self.resultItems.length);
// all items readed
self.showFolderContents();
}
}
// this function is callback, it called when error happend during items request
var searchItemsErrorCallback = function (serviceAPIError, requestedProvider) {
alert("serviceAPIError:" + serviceAPIError);
}
self.pendingOperation = null; // this variable will keep PendingOperation object, it can be used to cancel asynchronous request
try {
var startIndex = 0;
// zero value means read all elements of result
var requestCount = 0;
self.pendingOperation = mediaProvider.searchItems(searchItemsSuccessCallback, searchItemsErrorCallback,
keyword, startIndex, requestCount);
} catch (e) {
alert("searchItems exception: " + e);
self.pendingOperation = null;
}
}
// this function search items by "keyword" in "mediaProvider"
searchHelper.search = function (mediaProvider, keyword) {
self = this;
self.resultItems = [];
self.searchItemsInner(mediaProvider, keyword);
}
// calling part
var mediaProvider = providerArray[0]; // select first device from devices list
searchHelper.searchFolder(mediaProvider, "Jackson");
Application can access contents of media items through contents URL. For next examples, next html code used:
<html>
<body>
<div id="div_id_item">Item viewer</div>
</body>
</html>
Samsung TV browser included support for html5. Video and Audio playing will use <video> and <audio> tags of html5.
Browser has embedded support for displaying images.
Sample
function showImageViewer (url) {
var viewerHtml = "<table border=1><tr><td>";
var url = self.currentItem.getURI();
if (url) {
viewerHtml += "<img src=" + url + " height=400 width=600>";
}
viewerHtml += "</td></tr></table>";
var element = document.getElementById("div_id_item");
if (element) {
element.innerHTML = viewerHtml;
}
}
Tag <audio> can be used for audio playing.
Sample
function showAudioPlayer (url) {
var viewerHtml = "";
var playerId = "playerId";
var url = self.currentItem.getURI();
if (url) {
viewerHtml += "URL : " + url + "<br>";
viewerHtml += "<audio id=\"" + playerId + "\" src=\"";
viewerHtml += url + "\" controls=\"controls\"> Browser does not support the audio tag</audio>";
}
var element = document.getElementById("div_id_item");
if (element) {
element.innerHTML = viewerHtml;
}
}
AVPlay API can be used for video playing.
Code example
function showImage(url) {
var playerId = PLAYER_ID;
var videourl = self.currentItem.getURI();
var playSuccessCB = {
console.log("playing the video is successfully.");
};
function successCB(avplayObj) {
avplayObj.init();
avplayObj.open(videourl);
avplayObj.play(
playSuccessCB,
function (error) {
console.error(error.message);
},
5
);
}
function errorCB(error) {
console.log("Cannot get avplay object : " + error.name);
}
try {
webapis.avplay.getAVPlay(successCB, errorCB);
} catch (error) {
console.log(error.name);
}
}
Note
The files needed for the sample application are: here.
This is screen shot of Sample App, in contents browsing mode:
Figure 1. Sample app screenshot.