4

以下代码的网址:https ://github.com/amankapur007/openflix_torrent

我正在尝试将https://github.com/TorrentStream/TorrentStream-Android转换为颤振插件。我能够开始流式传输并且也能够准备它,但是在那之后什么都没有发生,下载和流式传输都没有开始(我现在不担心流式传输,而且我希望开始文件下载)。

请在下面找到代码

package com.example.openflix_torrent;

import android.os.Environment;

import androidx.annotation.NonNull;

import com.github.se_bastiaan.torrentstream.StreamStatus;
import com.github.se_bastiaan.torrentstream.Torrent;
import com.github.se_bastiaan.torrentstream.TorrentOptions;
import com.github.se_bastiaan.torrentstream.TorrentStream;
import com.github.se_bastiaan.torrentstream.listeners.TorrentListener;

import java.io.File;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** OpenflixTorrentPlugin */
public class OpenflixTorrentPlugin implements FlutterPlugin, MethodCallHandler {
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "openflix_torrent");
    channel.setMethodCallHandler(new OpenflixTorrentPlugin());
  }

  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "openflix_torrent");
    channel.setMethodCallHandler(new OpenflixTorrentPlugin());
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      init();
      try {
        Thread.sleep(1000);
        start();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    } else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
  }

  TorrentStream torrentStream;
  public void init(){
    TorrentOptions torrentOptions = new  TorrentOptions.Builder()
            .autoDownload(true)
            .saveLocation(Environment.getDownloadCacheDirectory())
            .removeFilesAfterStop(false)
            .build();
    this.torrentStream =  TorrentStream.init(torrentOptions);
    torrentStream.addListener(new TorrentListener() {

      @Override
      public void onStreamPrepared(Torrent torrent) {
        System.out.println("onStreamPrepared");
      }

      @Override
      public void onStreamStarted(Torrent torrent) {
        System.out.println("onStreamStarted");
      }

      @Override
      public void onStreamError(Torrent torrent, Exception e) {
        System.out.println("onStreamError"+ e.getMessage());
      }

      @Override
      public void onStreamReady(Torrent torrent) {
        System.out.println("onStreamReady");
      }

      @Override
      public void onStreamProgress(Torrent torrent, StreamStatus status) {
        System.out.println("onStreamProgress");
      }

      @Override
      public void onStreamStopped() {
        System.out.println("onStreamStopped");
      }
    });
    System.out.println("Initialized");
  }

  public void start(){
    this.torrentStream.startStream("https://yts.lt/torrent/download/FF495923151031A547AE14C1CA9F0DFF8EA26A0B");
    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Started");
  }

}

请找到输出

输出

注意:init() 和 start() 在运行时被调用。

请在 github 上找到上述代码。git@github.com:amankapur007/openflix_torrent.git

4

0 回答 0