目次

2008年11月16日日曜日

Orb APIで音楽ファイルを再生してみよう

なんだかしらないけど、昨日一日がんばって出来なかったstream.sdpというAPIが急に呼び出せるようになってテンションがあがりまくりました。

https://api.orb.com/orb/data/(セッショID)/(メディアID)/stream.sdp
昨日さんざこれをうっても反応してなかったくせに、ふっとできましたw
テンションがぐっとあがりました。

補足:
ログイン&セッションIDをサンプルのものはアラートで表示していたので、コピペできないのが難点でした。なので、自分で、結果からセッションIDを抜き出してHTMLの部分に表示するプログラムを作っちゃいました。

<html>
<head>
<meta name = "viewport" content = "width = device-width">
<!-- We use the Prototype library. Get it at: http://prototypejs.org -->
<script src="prototype.js" type="text/javascript"></script>
</head>
<body>
<script language="javascript">
// End point url of the Orb Api.エンド ポイントURIとは、ローカル又はリモートの資源及びサービスを特定するためのアドレスです
var orbApiUrl = 'http://api.orb.com/orb/';
// API key. If you want to run this sample, paste your API key below.ここにはAPIKey入れます
var apiKey = '3o7hgfvmof7jb';
// To go around Javascript's cross domain restriction, we use a local proxy.
var proxyUrl = 'ajaxProxy.php';

/*
* Authenticate
*/
function authenticate() {
var login = $('login').value;
var password = $('password').value;
// We make an ajax request to the login method, through the local proxy.
var url = orbApiUrl + 'xml/session.login?apiKey=' + apiKey +
'&l=' + encodeURIComponent(login) +
'&password=' + encodeURIComponent(password);
new Ajax.Request(proxyUrl + '?url=' + escape(url), {
method: 'get',
onSuccess: authenticateSuccessCallback,
onFailure: orbApiFailureCallback
});
};

/*
* authenticateSuccessCallback
* Called when the login ajax request was successful.
*/
function authenticateSuccessCallback(transport) {
// Access the XML dom object within the response.
var dom = transport.responseXML.documentElement;
// Check that the Orb response is OK.
if (checkOrbResponseStatus(dom)) {
// Retrieve the session id from the XML dom object.
sessionId = dom.getElementsByTagName('orbSessionId')[0].firstChild.nodeValue;
//セッションIDの表示
  // document.write(sessionId,"がセッションIDです。");
$('results').innerHTML = sessionId + '<br/>' +"セッションIDです";
// Hide the login panel and display the search panel on the page.
Element.hide('loginPanel');
Element.show('mainPanel');
}
};

/*
* orbApiFailureCallback
* Called when an ajax request failed.
* (this is not a failure from the API method itself).
*/
function orbApiFailureCallback(transport) {
alert('Error contacting Orb server (' + transport.status + '): ' + transport.responseText);
};

/*
* checkOrbResponseStatus
* This is a generic function that looks into the XML response sent by Orb to see if the status code is 0 (OK).
*/
function checkOrbResponseStatus(dom) {
var error = '';
try {
// Get the XML 'status' node.
var statusNode = dom.getElementsByTagName('status')[0];
// Get the XML 'status' node's 'code' attribute.
var statusCode = statusNode.attributes.getNamedItem('code').value;
if (statusCode != '0') {
// Code different from 0 means there was a problem.
var statusDesc = statusNode.attributes.getNamedItem('desc').value;
// Get the description corresponding to the error.
// WARNING: the description is just for development purposes.
// The text it contains is not intended to be displayed to the end-user.
// You should provide your own text/error messages depending on the error codes.
error = 'Error (' + statusCode + ') : ' + statusDesc;
}
} catch (e) {
error = 'Unknown error.';
}
if (error !== '') {
alert(error);
}
// Return true (boolean) if there was no error.
return (error === '');
}


</script>

<div id="loginPanel" style="border:1px;">
Login: <input type="text" id="login" value="" />
<br/>
Password: <input type="password" id="password" value="" />
<br/>
<input type="button" onclick="authenticate();" value="Login"/>
</div>


<div id="mainPanel" style="display:none;">
You are logged in!ログインしましたよよよよよよ。
<div id="results">
</div>
</div>
</body>
</html>

0 件のコメント: