webtor-io/embed-sdk-js

Not working!

Alok-Singh-ops opened this issue · 4 comments

It works only when we statically give the link of torrent file to the src. When src attribute is updated with javascript video doesn't load.

Embed SDK doesn't provide "live" player update.
Consider advanced-usage for your scenario. It makes possible to load player dynamically.

@vintikzzz Thanks For replying. I am new to developing. I used advanced-usage but on passing variable to magnet property console shows error 'failed to fetch torrent code=2' . Help over this issue

I am pasting the code

`

<title>Webtor Player SDK Example</title> <style> html, body { margin: 0; padding: 0; overflow: hidden; width: 100%; height: 100%; } #player { height: 80%; } #controls { padding: 1rem; } #controls i { padding-top: 0.3rem; display: block; } .control { padding-right: 1rem; } #files { padding: 0.5rem; } #files a { padding: 0.5rem; } </style> Search
<div id="controls">
  <span class="control">
    External controls*:
    <button id="play">Play</button>
    <button id="pause">Pause</button>
    <button id="moveto1min">Move to 1 min</button>
    <button id="movetostart">Move to start</button>
  </span>
  <span class="control">
    Player status: <span id="player-status">NONE</span>
  </span>
  <span class="control">
    Current time: <span id="current-time">0</span> sec
  </span>
  <span class="duration"> Duration: <span id="duration">0</span> sec </span>
  <br />
  <i>* - works only after first click on play button in the player</i>
</div>
<div id="player"></div>
<div id="files"></div>
<script>
  const form = document.querySelector("form");
  const input = document.querySelector("input");

  let uri;

  form.addEventListener("click", (e) => {
    async function get(search) {
      const data = await fetch(
        `https://yts.mx/api/v2/list_movies.json/?query_term=${search}`
      );
      const res = await data.json();
      return res;
    }
    get(input.value).then((res) => {
      res.data.movies.forEach((element) => {
        uri = element.torrents[0].hash;
      });
    });
    input.value = "";
    e.preventDefault();

    console.log(uri);
    window.webtor = window.webtor || [];
    window.webtor.push({
      id: "player",
      baseUrl: "https://webtor.io",
      // baseUrl: 'http://192.168.0.100:4000',
      magnet: `magnet:?xt=urn:btih:${uri}`,
      // magnet: 'magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F',
      width: "100%",
      height: "100%",
      features: {
        continue: false,
        // title:       false,
        // p2pProgress: false,
        // subtitles:   false,
        // settings:    false,
        // fullscreen:  false,
        // playpause:   false,
        // currentTime: false,
        // timeline:    false,
        // duration:    false,
        // volume:      false,
        // chromecast:  false,
      },
      on: function (e) {
        if (e.name == window.webtor.TORRENT_FETCHED) {
          console.log("Torrent fetched!", e.data.files);
          var p = e.player;
          var files = document.getElementById("files");
          for (const f of e.data.files) {
            if (!f.name.endsWith(".mp4")) continue;
            var a = document.createElement("a");
            a.setAttribute("href", f.path);
            a.innerText = f.name;
            files.appendChild(a);
            a.addEventListener("click", function (e) {
              e.preventDefault();
              p.open(e.target.getAttribute("href"));
              return false;
            });
          }
        }
        if (e.name == window.webtor.TORRENT_ERROR) {
          console.log("Torrent error!");
        }
        if (e.name == window.webtor.INITED) {
          var p = e.player;
          document
            .getElementById("play")
            .addEventListener("click", function (ev) {
              p.play();
            });
          document
            .getElementById("pause")
            .addEventListener("click", function (ev) {
              p.pause();
            });
          document
            .getElementById("moveto1min")
            .addEventListener("click", function (ev) {
              p.setPosition(60);
            });
          document
            .getElementById("movetostart")
            .addEventListener("click", function (ev) {
              p.setPosition(0);
            });
        }
        if (e.name == window.webtor.PLAYER_STATUS) {
          document.getElementById("player-status").innerHTML = e.data;
        }
        if (e.name == window.webtor.OPEN) {
          console.log(e.data);
        }
        if (e.name == window.webtor.CURRENT_TIME) {
          document.getElementById("current-time").innerHTML = parseInt(
            e.data
          );
        }
        if (e.name == window.webtor.DURATION) {
          document.getElementById("duration").innerHTML = parseInt(e.data);
        }
        if (e.name == window.webtor.OPEN_SUBTITLES) {
          console.log(e.data);
        }
      },
    });
  });
</script>
<script
  src="https://cdn.jsdelivr.net/npm/@webtor/player-sdk-js@0.2.12/dist/index.min.js"
  charset="utf-8"
></script>
`

It would be really helpful

@vintikzzz Thanks It is working now