Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单例模式模式下,不同的下载链接只下载第一个链接 #12

Open
lsldragon opened this issue Jan 11, 2022 · 1 comment
Open

Comments

@lsldragon
Copy link

参考如下代码:
` downloadVideoButton.addActionListener(new ActionListener() {
@OverRide
public void actionPerformed(ActionEvent actionEvent) {

            int index = list.getSelectedIndex();
            String name = nameList.get(index);

            String html = "https://" + nameListURL.get(index);
            System.out.println(html);

            String html1 = GetContent.getHtml(html);

            String videoUrl = GetContent.getVideoUrl(html1);
            System.out.println(videoUrl);

            File file = new File(name);
            if (!file.exists()) {
                file.mkdir();
            }
            String filePath = file.getAbsolutePath();

            M3u8DownloadFactory.M3u8Download m3u8Download = M3u8DownloadFactory.getInstance(videoUrl);
            m3u8Download.setDir(filePath);
            m3u8Download.setFileName(name);
            m3u8Download.setThreadCount(100);
            m3u8Download.setRetryCount(20);
            m3u8Download.setTimeoutMillisecond(10000L);
            m3u8Download.setLogLevel(Constant.DEBUG);
            m3u8Download.setInterval(500L);
            m3u8Download.addListener(new DownloadListener() {
                @Override
                public void start() {
                    System.out.println("开始下载!");
                    progressBar.setValue(0);
                }

                @Override
                public void process(String downloadUrl, int finished, int sum, float percent) {
                    System.out.println("下载网址:" + downloadUrl + "\t已下载" + finished + "个\t一共" + sum + "个\t已完成" + percent + "%");
                    progressBar.setValue((int) percent);
                }

                @Override
                public void speed(String speedPerSecond) {
                    System.out.println("下载速度:" + speedPerSecond);
                }

                @Override
                public void end() {
                    System.out.println("下载完毕");
                    progressBar.setString("Video download completed!");
                }
            });
            m3u8Download.start();
        }
    });`

改变 list 的值,每次点击 downloadVideoButton 应该产生不同的 videoUrl , 但是每次下载都是第一个 videoUrl (软件不关闭情况下)。

后来看到了 工厂类的 getInstance() 方法:
public static M3u8Download getInstance(String downloadUrl) { if (m3u8Download == null) { synchronized (M3u8Download.class) { if (m3u8Download == null) m3u8Download = new M3u8Download(downloadUrl); } } return m3u8Download; }

当第二次点击 downloadVideoButton 时,m3u8Download 不为null 直接 return 了, 所以还是第一个的 videoUrl, 这就导致了每次下载只下载第一个链接。
建议不要把 downloadUrl 放在getInstance()参数里,增加 set get 方法。

@qq494257084
Copy link
Owner

qq494257084 commented Jan 14, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants