Newsgroup: it.comp.lang.javascript
Titolo: youtube api

Salve!
Scusate tutti i messaggi che sto postando ma non sono ferratissimo con 
js! :(
Sto litigando con le api di youtube!
stavo vedendo uno script fatto da una persona che potete visualizzare qua:
http://icant.co.uk/sandbox/youtube-captioning.html
nel codice si vede:
<html lang="en">
  <head>
    ...
    <script src="
http://swfobject.googlecode.com/svn/tags/rc3/swfobject/
src/swfobject.js" type="text/javascript"></script>
    ...
  </head>
  <body>
  <div>
    ...
    <div id="ytapiplayer">
      You need Flash player 8+ and JavaScript enabled to view this video.
    </div>
    <script type="text/javascript">
      var params = {allowScriptAccess:'always'};
      var atts = {id:'myytplayer'};
swfobject.embedSWF("
http://www.youtube.com/v/
T4y6M2AeQ00&amp;border=0&amp;enablejsapi=1&amp;playerapiid=ytplayer", 
"ytapiplayer", "425", "356", "8", null, null, params, atts);
    function onYouTubePlayerReady(playerId) {
      ytplayer = document.getElementById('myytplayer');
      ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');
      ytplayer.addEventListener('onError', 'onPlayerError');
      out = document.createElement('dl');
      var form = document.createElement('form');
      form.action = 'gettranscript.php';
      form.method = 'post';
      form.appendChild(out);
      var sub = document.createElement('input');
      sub.type = 'submit';
      sub.value = 'give me the transcript';
      form.appendChild(sub);
      var urlstore = document.createElement('input');
      urlstore.type = 'hidden';
 
urlstore.name
 = 'url';
      urlstore.value = ytplayer.src || ytplayer.data;
      form.appendChild(urlstore);
      ytplayer.parentNode.insertBefore(form,ytplayer.nextSibling);
    }
    function onytplayerStateChange(newState) {
      if(newState === 2){
        var stamp = document.createElement('dt');
        var time = ytplayer.getCurrentTime();
        var timestamp = parseInt(time/60);
        var secs = parseInt(time%60);
        timestamp += ':';
        timestamp += secs < 10 ?'0'+secs : secs;
        stamp.innerHTML = '<a href="javascript:ytplayer.seekTo('+time+', 
true)">' + timestamp + '</a>';
        var caption = document.createElement('dd');
        var info = document.createElement('input');
        info.type = 'text'
 
info.name
 = 'time' + time;
        caption.appendChild(info);
        var kill = document.createElement('a');
        kill.onclick = ytCaptionKill;
        kill.appendChild(document.createTextNode('x'));
        caption.appendChild(kill);
        out.appendChild(stamp);
        out.appendChild(caption);
      }
    }
    function ytCaptionKill(){
      var parent = this.parentNode;
      var timestamp = parent.previousSibling;
      timestamp.parentNode.removeChild(timestamp);
      parent.parentNode.removeChild(parent);
    }
    function onPlayerError(errorCode) {
      alert("An error occurred: "+ errorCode);
    }
    </script>
  </body>
</html>
Io volevo fare la stessa cosa, ma vorrei caricare tutto dopo un evento 
che ad esempio al click del mouse quindi onclick="javascript:caricaPlayer
()";
e la funzione è:
function caricaPlayer(){
var vid = document.createElement('div');
vid.id
='videoForm';
vid.setAttribute('style','float:left; margin: 4px;');
var env_var = document.createElement('input');
env_var.type = 'hidden';
env_var.name
 = 'objuri';
var script_header = document.createElement('script');
script_header.type = 'text/javascript';
script_header.src = '
http://swfobject.googlecode.com/svn/tags/rc3/
swfobject/src/swfobject.js';
var script_header2 = document.createElement('script');
script_header2.type = 'text/javascript';
script_header2.src = '
http://lamp.local/madcow_bk/videoCMD.js
';
var div_video = document.createElement('div');  
div_video.id
 = 'ytapiplayer';
div_video.innerHTML = \" You need Flash player 8+ and JavaScript enabled 
to view this video.\";
document.documentElement.getElementsByTagName('HEAD')[0].appendChild
(script_header);
vid.appendChild(div_video);
vid.appendChild(script_header2);
vid.appendChild(env_var);
vid.innerHTML += \"<div>Player state: <span id='playerstate'>--</span></
div>\";
vid.innerHTML += \"<br /><a href='javascript:void(0);' onclick='play
();'>Play</a> | <a href='javascript:void(0);' onclick='pause();'>Pause</
a> | <a href='javascript:void(0);' onclick='stop();'>Stop</a> | <a 
href='javascript:void(0);' onclick='mute();'>Mute</a> | <a 
href='javascript:void(0);' onclick='unMute();'>Unmute</a> |<br /><br />\";
document.body.appendChild(vid);
}
videoCMD.js contiene tutte le funzioni del player e lo script che 
dovrebbe iniziali8zzarlo così come sul doc html che vi ho mostrato 
all'inizio:
var params = {allowScriptAccess:"always"};
var atts = {id:"myytplayer"};
swfobject.embedSWF("
http://www.youtube.com/v/
T4y6M2AeQ00&amp;border=0&amp;enablejsapi=1&amp;playerapiid=ytplayer", 
"ytapiplayer", "425", "356", "8", null, null, params, atts);
	function updateHTML(elmId, value) {
          document.getElementById(elmId).innerHTML = value;
        }
        function setytplayerState(newState) {
          updateHTML('playerstate', newState);
        }
        function onYouTubePlayerReady(playerId) {
          ytplayer = document.getElementById('myytplayer');
          setInterval(updateytplayerInfo, 250);
          updateytplayerInfo();
          ytplayer.addEventListener('onStateChange', 
'onytplayerStateChange');
          ytplayer.addEventListener('onError', 'onPlayerError');
        }
        function onytplayerStateChange(newState) {
          setytplayerState(newState);
        }
        function onPlayerError(errorCode) {
          alert('An error occurred: '+ errorCode);
        }
        function updateytplayerInfo() {
          updateHTML('bytesloaded', getBytesLoaded());
          updateHTML('bytestotal', getBytesTotal());
          updateHTML('videoduration', getDuration());
          updateHTML('videotime', getCurrentTime());
          updateHTML('startbytes', getStartBytes());
        }
        // functions for the api calls
        function play() {
          if (ytplayer) {
            ytplayer.playVideo();
          }
        }
        function pause() {
          if (ytplayer) {
            ytplayer.pauseVideo();
          }
        }
        function stop() {
          if (ytplayer) {
            ytplayer.stopVideo();
          }
        }
        function getPlayerState() {
          if (ytplayer) {
            return ytplayer.getPlayerState();
          }
        }
        function seekTo(seconds) {
          if (ytplayer) {
            ytplayer.seekTo(seconds, true);
          }
        }
        function getBytesLoaded() {
          if (ytplayer) {
            return ytplayer.getVideoBytesLoaded();
          }
        }
        function getBytesTotal() {
          if (ytplayer) {
            return ytplayer.getVideoBytesTotal();
          }
        }
        function getCurrentTime() {
          if (ytplayer) {
            return ytplayer.getCurrentTime();
          }
        }
        function getDuration() {
          if (ytplayer) {
            return ytplayer.getDuration();
          }
        }
        function getStartBytes() {
          if (ytplayer) {
            return ytplayer.getVideoStartBytes();
          }
        }
        function mute() {
          if (ytplayer) {
            ytplayer.mute();
          }
        }
        function unMute() {
          if (ytplayer) {
            ytplayer.unMute();
          }
        }
le scritte dei comandi compaiono, ma se clicco ad esempio su play nella 
console degli errori su firebugs leggo 
ytplayer is not defined
if (ytplayer) {
come se il player non stratasse che è stranissimo in quanto, io appena 
clicco compare la scritta contenuta nel div che conterrà il player: 
"You need Flash player 8+ and JavaScript enabled to view this video." ma 
poi scompare come se avesse caricato qualcosa, ma del video nessuna 
traccia!
Qualcuno sa aiutarmi?
Grazie
Mk

mmm ... potrei anche accontentarmi di sapere se è una cosa fattibile o 
meno! :P
mk