Why is this error "Uncaught Referenceerror: Sambaplayer is not defined"?

Asked

Viewed 2,643 times

2

Are you saying that "Sambaplayer" is not set.

<script>
    var xmlhttp = new XMLHttpRequest();
    var url = "https://api.sambavideos.sambatech.com/v1/medias?access_token=847584758475847874858363&pid=3434&sort=DESC&limit=5&filter=id,title,status,qualifier,description,shortDescription,categoryName,files,thumbs";

    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
      }
    };

    xmlhttp.open("GET", url, true);
    xmlhttp.send();

    myFunction(url);
    function myFunction(response) {
        var arr = JSON.parse(JSON.stringify(response));
        var i;
        var out = "<div>";

        for(i = 0; i < arr.length; i++) {
            out += "<a href='#' class='samba-playlist-trigger list-group-item active' data-mediaid=" + arr[i].id + "></a>";
        }
        out += "</div>";
        document.getElementById("id01").innerHTML = out;
    }
</script>

<script>// <![CDATA[
   var player = new SambaPlayer("player", {
   height: 270,
   width: 480,
   playlist: playlistObj,
   playerParams: {
    volume: 0,
    startOutput: '480p',
    html5: true
   },
   events: {
    "*": "eventListener"
   }
   });
// ]]></script>

<script>// <![CDATA[
  function eventListener(player){
  }

  function onClick(mediaId, evt){
    document.getElementsByClassName('samba-playlist-trigger list-group-item active')[0].className = 'samba-playlist-trigger list-group-item';
    evt.target.className = "samba-playlist-trigger list-group-item active";
  }
// ]]></script>

The html of the display page

<!DOCTYPE html>
<html lang="pt-br">
<head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <div class="container">
      <div class="row">
          <div id="id01"></div>
      </div>
   </div>

   <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

   <!-- chamada dos vídeos -->
   <script type="text/javascript" samba-player-api="player" src="http://player.sambatech.com.br/v3/samba.player.api.js"></script>
</body>
</html>
  • Hello, welcome to Stackoverflow! It seems to me an error in the library import. Could you kindly provide the HTML code of the file in which you import the Javascript dependencies?

  • Thank you, Bonifácio. I completed the html code with the so-called Javascripts as you requested.

  • Take a look here: Samba Player API

1 answer

3


As I suspected, apparently the problem was the import of the library itself.

I simulated a scenario similar to yours and detected that the error you indicated occurs if you try to reference the Sambaplayer object before importing the library. This is because Javascript is executed as it is referenced in HTML, and if you try to reference an object before the library is loaded, the error will occur.

In this case you have two options:

Move the call code to the Sambaplayer object to fall below the library call:

  <script type="text/javascript" samba-player-api="player" src="http://player.sambatech.com.br/v3/samba.player.api.js"></script>


<script type="text/javascript">
var player = new SambaPlayer("player", {
  height: 270,
  width: 480,
 playlist: null,
  playerParams: {
    volume: 0,
    startOutput: '480p',
    html5: true
  },
  events: {
    "*": "eventListener"
  }
  });

Or call the Sambaplayer object inside a Document.ready, because then the browser will wait until the document is loaded to then run this part of Javascript (and with this previously load the library):

    <script type="text/javascript">  
$(document).ready(function () {
    var player = new SambaPlayer("player", {
      height: 270,
      width: 480,
      playerParams: {
        volume: 0,
        startOutput: '480p',
        html5: true
      },
      events: {
        "*": "eventListener"
      }
    });
});
</script>
  • Now he accuses another reference error: Uncaught Referenceerror: playlistObj is not defined is directed to instruction within the: out += "<a href='#' class='samba-playlist-trigger list-group-item active' data-mediaid=" + arr[i].id + "></a>";

  • The playlistObj object has not been defined, this object probably stores the playlist information to send to the API. Since I have no knowledge of how it works, I suggest you take a look at API documentation.

  • It is already a direction. I will review this part of the code. Thank you very much, Bonifácio!

  • You’re welcome @Vandrépaulo, good luck with your work, until next time!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.