4

Url for the below Code : https://github.com/amankapur007/openflix_torrent

I am trying to convert https://github.com/TorrentStream/TorrentStream-Android to the flutter plugin . I am able to start the stream and am also able to prepare it , but after that nothing happens , neither the download starts nor streaming start (Streaming i am not worried right now , moreover i want file download to start).

Please find the code below

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");
  }

}

Please find the output

Output

NOTE : init() and start() are called at the runtime .

Please find the github for the above code . git@github.com:amankapur007/openflix_torrent.git

Aman
  • 226
  • 1
  • 4
  • 17
  • Remove this try/catch block along with Thread.sleep from "start" method" and try again. Maybe there is a problem with your network_security_config? What about missing WRITE_EXTERNAL_STORAGE or INTERNET permissions? – Bartosz Wilk Jan 16 '20 at 11:30
  • Thread.sleep is done for few delay , after removing also it was not working and 2nd thing , i have added both external write and internet permission in android xml . – Aman Jan 16 '20 at 14:44
  • If you don't explicitly request for write_external_storage permission in runtime it's treated as not granted by default. You have to ask for this permission in runtime and then grant it or just go to permissions section in your app settings and grant it manually. – Bartosz Wilk Jan 17 '20 at 09:17
  • Ok will try that one , but i am still thinking why it is not giving exception? There is no harm in giving it a try – Aman Jan 17 '20 at 13:17
  • @BartoszWilk I have tried giving permissions . I have given the giuthub url . Can you have a look . https://github.com/amankapur007/openflix_torrent – Aman Jan 19 '20 at 08:26
  • Were u able to make this work? @Aman – s_o_m_m_y_e_e Sep 10 '22 at 04:04

0 Answers0