sealedtx / java-youtube-downloader

Simple, almost zero-dependency java parser for retrieving youtube video metadata
Other
426 stars 117 forks source link

Async download issue #84

Closed f4004 closed 3 years ago

f4004 commented 3 years ago

First of all I'm using this library on Kotlin. When I tried to download video with: download() method. Everthing is okay. I can watch video that downloaded with this method. But when I tried to download video with downloadAsync() method. It doesn't work. It stop sometimes during download at %60-80. And output is 0kb. I'm using async to use onFinished() method. Is there another way to get situation of downloaded or finished?

sealedtx commented 3 years ago

@f4004 Please provide code snippet which cause the problem situiation. Are you using this library on PC or Android?

f4004 commented 3 years ago

I'm using on PC. Thrown error is:

Downloaded 85% Exception in thread "main" java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask.get(FutureTask.java:205) at MainKt.main(main.kt:123) at MainKt.main(main.kt)

The error points out to this line: val file = future[20, TimeUnit.SECONDS] I've realised this error only happens when internet connection getting slower. What to do in case of internet interruption?

sealedtx commented 3 years ago

@f4004 The TimeoutException means that due to slow internet your video can't be downloaded fully in 20 seconds. If you want to use callback to get your result in onFinished() you may omit returned future - it is used only for canceling downloading or for timeouting.

f4004 commented 3 years ago

I think I don't understand. Can I use onFinished() without async? If so can you show an example?

sealedtx commented 3 years ago

@f4004 onFinished callback can't be used for sync donwload, because callback by definition is used in async actions. Here is code snippet for async download with callback:

// this will not block current thread and will execute in another thread.
video.downloadAsync(format, outputDir, new OnYoutubeDownloadListener() {
    @Override
    public void onDownloading(int progress) {
    }

    @Override
    public void onFinished(File file) {
        // called from another thread only when file is completly downloaded.
    }

    @Override
    public void onError(Throwable throwable) {
    }
});
// your program continue executing

Here is async download without callback:

int secondsToWait = 10;
Future<File> future = video.downloadAsync(format, outputDir);
// this will block current thread for at most 10 s, and throw an TimeoutException if downloading lasts longer than 10 s
File file = future.get(secondsToWait, TimeUnit.SECONDS);

As I undestand you need to use first one. Don't use future.get with timeout.

f4004 commented 3 years ago

Now it is better to understand.