Ionic 4 的 Webtorrent client/player

Webtorrent client/player for Ionic 4

我想将 Webtorrent 实现到 Ionic 4 应用程序中。我已经设法完美地播放 .m3u8 流。我现在需要的只是我不太熟悉的 webtorrent 部分。

我正在为 .m3u8 流使用 <video src='url.m3u8'></video> 标签,它似乎在 Ionic 4 中工作正常。我希望能够下载 torrent 视频文件stream/play Ionic 上的视频使用 <video> 标签或 video-player 组件。

拜托,我需要一些帮助。我一直在尝试我所知道的和我可以在网上找到的一切,但到目前为止没有任何帮助。任何帮助将不胜感激。


先谢谢了。




edited: Sun, May 26th, 2019 at 7:59:02 PM

这是我在尝试实施时遇到的错误。任何人都知道可能是什么问题。

这是我的代码的快照。

Picture Here

告诉我你们的想法。

先谢谢了。

好的,经过几天尝试不同的方法,我找到了解决方案。真的很容易顺便说一句。

这是一个 picture


这是我使用的代码。

    import { WebTorrent } from 'webtorrent';

    declare var WebTorrent: WebTorrent;

        ....

    playVideo() {

    const client = WebTorrent();
    const magnetURL = 'https://webtorrent.io/torrents/sintel.torrent';

    client.add(magnetURL, function (torrent) {
      // document.getElementById('hash').textContent = 'Client downloading: ' + torrent.infoHash;
      torrent.files.forEach(function (file) {
        torrent.on('download', function (bytes) {
          document.getElementById('download').textContent = 'just downloaded: ' + bytesToSize(bytes);
          document.getElementById('tdownload').textContent = 'total downloaded: ' + bytesToSize(torrent.downloaded);
          document.getElementById('sdownload').textContent = 'download speed: ' + bytesToSize(torrent.downloadSpeed);
          document.getElementById('pdownload').textContent = toPercentage(torrent.progress);
        });
        torrent.files.find(function (file) {
          return file.name.endsWith('.mp4') || file.name.endsWith('.avi') || file.name.endsWith('.mkv') || file.name.endsWith('.mpeg');
        });
        file.renderTo('#video', function (err, element) {
          presentToast(magnetURL);
        });
      });
    });
    function presentToast(text: string) {
      this.presentToast(text);
    }
    function bytesToSize(bytes) {
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      if (bytes === 0) { return '0 Bytes'; }
      const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
      return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
    }

    function toPercentage(dec) {
      dec = dec.toString();
      const a = dec.split('.');
      dec = a[1];
      dec = dec.substr(0, 4);
      return dec = (dec / 100) + '%';
    }
  }

不过我有两个问题。我只能在 sintel.mp4 达到 99.89% 后播放它,但我希望能够在下载时播放它。 我的第二个问题是我只能下载和播放Sintel.torrent。我尝试使用其他磁力链接,但没有任何效果。我猜这与磁铁 url 的产生方式有关。